diff --git a/README.md b/README.md index 445b0418..cee43993 100644 --- a/README.md +++ b/README.md @@ -85,31 +85,38 @@ try { Methods ------- + +### Async + - [copy](docs/copy.md) -- [copySync](docs/copy.md) - [emptyDir](docs/emptyDir.md) -- [emptyDirSync](docs/emptyDir.md) - [ensureFile](docs/ensureFile.md) -- [ensureFileSync](docs/ensureFile.md) - [ensureDir](docs/ensureDir.md) -- [ensureDirSync](docs/ensureDir.md) - [ensureLink](docs/ensureLink.md) -- [ensureLinkSync](docs/ensureLink.md) - [ensureSymlink](docs/ensureSymlink.md) -- [ensureSymlinkSync](docs/ensureSymlink.md) - [mkdirs](docs/ensureDir.md) -- [mkdirsSync](docs/ensureDir.md) - [move](docs/move.md) - [outputFile](docs/outputFile.md) -- [outputFileSync](docs/outputFile.md) - [outputJson](docs/outputJson.md) -- [outputJsonSync](docs/outputJson.md) - [readJson](docs/readJson.md) -- [readJsonSync](docs/readJson.md) - [remove](docs/remove.md) -- [removeSync](docs/remove.md) - [writeJson](docs/writeJson.md) -- [writeJsonSync](docs/writeJson.md) + +### Sync + +- [copySync](docs/copy-sync.md) +- [emptyDirSync](docs/emptyDir-sync.md) +- [ensureFileSync](docs/ensureFile-sync.md) +- [ensureDirSync](docs/ensureDir-sync.md) +- [ensureLinkSync](docs/ensureLink-sync.md) +- [ensureSymlinkSync](docs/ensureSymlink-sync.md) +- [mkdirsSync](docs/ensureDir-sync.md) +- [moveSync](docs/move-sync.md) +- [outputFileSync](docs/outputFile-sync.md) +- [outputJsonSync](docs/outputJson-sync.md) +- [readJsonSync](docs/readJson-sync.md) +- [removeSync](docs/remove-sync.md) +- [writeJsonSync](docs/writeJson-sync.md) **NOTE:** You can still use the native Node.js methods. They are copied over to `fs-extra`. diff --git a/docs/copy-sync.md b/docs/copy-sync.md new file mode 100644 index 00000000..b6de1a3c --- /dev/null +++ b/docs/copy-sync.md @@ -0,0 +1,35 @@ +# copySync(src, dest, [options]) + +Copy a file or directory. The directory can have contents. Like `cp -r`. + +## Options: +- overwrite (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior. +- errorOnExist (boolean): when `overwrite` is `false` and the destination exists, throw an error. Default is `false`. +- dereference (boolean): dereference symlinks, default is `false`. +- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`. +- filter: Function to filter copied files. Return `true` to include, `false` to exclude. This can also be a RegExp, however this is deprecated (See [issue #239](https://github.com/jprichardson/node-fs-extra/issues/239) for background). + +## Example: + +```js +const fs = require('fs-extra') + +// copy file +fs.copySync('/tmp/myfile', '/tmp/mynewfile') + +// copy directory, even if it has subdirectories or files +fs.copySync('/tmp/mydir', '/tmp/mynewdir') +``` + +**Using filter function** + +```js +const fs = require('fs-extra') + +const filterFunc = (src, dest) => { + // your logic here + // it will be copied if return true +} + +fs.copySync('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc }) +``` diff --git a/docs/copy.md b/docs/copy.md index dac76295..ce5c4134 100644 --- a/docs/copy.md +++ b/docs/copy.md @@ -2,8 +2,6 @@ Copy a file or directory. The directory can have contents. Like `cp -r`. -**Sync:** `copySync()` - ## Options: - overwrite (boolean): overwrite existing file or directory, default is `true`. _Note that the copy operation will silently fail if you set this to `false` and the destination exists._ Use the `errorOnExist` option to change this behavior. - errorOnExist (boolean): when `overwrite` is `false` and the destination exists, throw an error. Default is `false`. diff --git a/docs/emptyDir-sync.md b/docs/emptyDir-sync.md new file mode 100644 index 00000000..603c2792 --- /dev/null +++ b/docs/emptyDir-sync.md @@ -0,0 +1,14 @@ +# emptyDirSync(dir) + +Ensures that a directory is empty. Deletes directory contents if the directory is not empty. If the directory does not exist, it is created. The directory itself is not deleted. + +**Alias:** `emptydirSync()` + +## Example: + +```js +const fs = require('fs-extra') + +// assume this directory has a lot of files and folders +fs.emptyDirSync('/tmp/some/dir') +``` diff --git a/docs/emptyDir.md b/docs/emptyDir.md index d5987e74..a5263fbc 100644 --- a/docs/emptyDir.md +++ b/docs/emptyDir.md @@ -4,8 +4,6 @@ Ensures that a directory is empty. Deletes directory contents if the directory i **Alias:** `emptydir()` -**Sync:** `emptyDirSync()`, `emptydirSync()` - ## Example: ```js diff --git a/docs/ensureDir-sync.md b/docs/ensureDir-sync.md new file mode 100644 index 00000000..4d3e39fe --- /dev/null +++ b/docs/ensureDir-sync.md @@ -0,0 +1,15 @@ +# ensureDirSync(dir) + +Ensures that the directory exists. If the directory structure does not exist, it is created. Like `mkdir -p`. + +**Aliases:** `mkdirsSync()`, `mkdirpSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const dir = '/tmp/this/path/does/not/exist' +fs.ensureDirSync(dir) +// dir has now been created, including the directory it is to be placed in +``` diff --git a/docs/ensureFile-sync.md b/docs/ensureFile-sync.md new file mode 100644 index 00000000..b9f9685e --- /dev/null +++ b/docs/ensureFile-sync.md @@ -0,0 +1,15 @@ +# ensureFileSync(file) + +Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is **NOT MODIFIED**. + +**Alias:** `createFileSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/this/path/does/not/exist/file.txt' +fs.ensureFileSync(file) +// file has now been created, including the directory it is to be placed in +``` diff --git a/docs/ensureFile.md b/docs/ensureFile.md index b94d0e27..4ec975f3 100644 --- a/docs/ensureFile.md +++ b/docs/ensureFile.md @@ -4,8 +4,6 @@ Ensures that the file exists. If the file that is requested to be created is in **Alias:** `createFile()` -**Sync:** `createFileSync()`,`ensureFileSync()` - ## Example: ```js diff --git a/docs/ensureLink-sync.md b/docs/ensureLink-sync.md new file mode 100644 index 00000000..7620d3cf --- /dev/null +++ b/docs/ensureLink-sync.md @@ -0,0 +1,14 @@ +# ensureLinkSync(srcpath, dstpath) + +Ensures that the link exists. If the directory structure does not exist, it is created. + +## Example: + +```js +const fs = require('fs-extra') + +const srcpath = '/tmp/file.txt' +const dstpath = '/tmp/this/path/does/not/exist/file.txt' +fs.ensureLinkSync(srcpath, dstpath) +// link has now been created, including the directory it is to be placed in +``` diff --git a/docs/ensureLink.md b/docs/ensureLink.md index 69cbb247..8207ae4b 100644 --- a/docs/ensureLink.md +++ b/docs/ensureLink.md @@ -2,8 +2,6 @@ Ensures that the link exists. If the directory structure does not exist, it is created. -**Sync:** `ensureLinkSync()` - ## Example: ```js diff --git a/docs/ensureSymlink-sync.md b/docs/ensureSymlink-sync.md new file mode 100644 index 00000000..3f8c6ea0 --- /dev/null +++ b/docs/ensureSymlink-sync.md @@ -0,0 +1,14 @@ +# ensureSymlinkSync(srcpath, dstpath, [type]) + +Ensures that the symlink exists. If the directory structure does not exist, it is created. + +## Example: + +```js +const fs = require('fs-extra') + +const srcpath = '/tmp/file.txt' +const dstpath = '/tmp/this/path/does/not/exist/file.txt' +fs.ensureSymlinkSync(srcpath, dstpath) +// symlink has now been created, including the directory it is to be placed in +``` diff --git a/docs/ensureSymlink.md b/docs/ensureSymlink.md index 8240e1fa..a5e35174 100644 --- a/docs/ensureSymlink.md +++ b/docs/ensureSymlink.md @@ -2,8 +2,6 @@ Ensures that the symlink exists. If the directory structure does not exist, it is created. -**Sync:** `ensureSymlinkSync()` - ## Example: ```js diff --git a/docs/move-sync.md b/docs/move-sync.md new file mode 100644 index 00000000..8f392e3f --- /dev/null +++ b/docs/move-sync.md @@ -0,0 +1,20 @@ +# moveSync(src, dest, [options]) + +Moves a file or directory, even across devices. + +## Options: +- overwrite (boolean): overwrite existing file or directory, default is `false` + +## Example: + +```js +const fs = require('fs-extra') + +fs.moveSync('/tmp/somefile', '/tmp/does/not/exist/yet/somefile') +``` + +```js +const fs = require('fs-extra') + +fs.moveSync('/tmp/somedir', '/tmp/may/already/existed/somedir', { overwrite: true }) +``` diff --git a/docs/move.md b/docs/move.md index c7dc77ed..82c42a84 100644 --- a/docs/move.md +++ b/docs/move.md @@ -16,3 +16,13 @@ fs.move('/tmp/somefile', '/tmp/does/not/exist/yet/somefile', err => { console.log('success!') }) ``` + +```js +const fs = require('fs-extra') + +fs.move('/tmp/somedir', '/tmp/may/already/existed/somedir', { overwrite: true }, err => { + if (err) return console.error(err) + + console.log('success!') +}) +``` diff --git a/docs/outputFile-sync.md b/docs/outputFile-sync.md new file mode 100644 index 00000000..d5543329 --- /dev/null +++ b/docs/outputFile-sync.md @@ -0,0 +1,15 @@ +# outputFileSync(file, data, [options]) + +Almost the same as `writeFileSync` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFileSync()`](https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options). + +## Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/this/path/does/not/exist/file.txt' +fs.outputFileSync(file, 'hello!') + +const data = fs.readFileSync(file, 'utf8') +console.log(data) // => hello! +``` diff --git a/docs/outputFile.md b/docs/outputFile.md index df320df2..637a11bf 100644 --- a/docs/outputFile.md +++ b/docs/outputFile.md @@ -2,8 +2,6 @@ Almost the same as `writeFile` (i.e. it [overwrites](http://pages.citebite.com/v2o5n8l2f5reb)), except that if the parent directory does not exist, it's created. `file` must be a file path (a buffer or a file descriptor is not allowed). `options` are what you'd pass to [`fs.writeFile()`](https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback). -**Sync:** `outputFileSync()` - ## Example: ```js diff --git a/docs/outputJson-sync.md b/docs/outputJson-sync.md new file mode 100644 index 00000000..fe2d7027 --- /dev/null +++ b/docs/outputJson-sync.md @@ -0,0 +1,18 @@ +# outputJsonSync(file, data, [options]) + +Almost the same as [`writeJsonSync`](writeJson-sync.md), except that if the directory does not exist, it's created. +`options` are what you'd pass to [`jsonFile.writeFileSync()`](https://github.com/jprichardson/node-jsonfile#writefilesyncfilename-obj-options). + +**Alias:** `outputJSONSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/this/path/does/not/exist/file.json' +fs.outputJsonSync(file, {name: 'JP'}) + +const data = fs.readJsonSync(file) +console.log(data.name) // => JP +``` diff --git a/docs/outputJson.md b/docs/outputJson.md index 0d419025..8cce8bd9 100644 --- a/docs/outputJson.md +++ b/docs/outputJson.md @@ -12,7 +12,7 @@ Almost the same as [`writeJson`](writeJson.md), except that if the directory doe ```js const fs = require('fs-extra') -const file = '/tmp/this/path/does/not/exist/file.txt' +const file = '/tmp/this/path/does/not/exist/file.json' fs.outputJson(file, {name: 'JP'}, err => { console.log(err) // => null diff --git a/docs/readJson-sync.md b/docs/readJson-sync.md new file mode 100644 index 00000000..cd762379 --- /dev/null +++ b/docs/readJson-sync.md @@ -0,0 +1,30 @@ +# readJsonSync(file, [options]) + +Reads a JSON file and then parses it into an object. `options` are the same +that you'd pass to [`jsonFile.readFileSync`](https://github.com/jprichardson/node-jsonfile#readfilesyncfilename-options). + +**Alias:** `readJSONSync()` + +## Example: + +```js +const fs = require('fs-extra') + +const packageObj = fs.readJsonSync('./package.json') +console.log(packageObj.version) // => 2.0.0 +``` + +--- + +`readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example: + +```js +const fs = require('fs-extra') + +const file = '/tmp/some-invalid.json' +const data = '{not valid JSON' +fs.writeFileSync(file, data) + +const obj = fs.readJsonSync(file, { throws: false }) +console.log(obj) // => null +``` diff --git a/docs/readJson.md b/docs/readJson.md index 25a0f973..59108c83 100644 --- a/docs/readJson.md +++ b/docs/readJson.md @@ -5,8 +5,6 @@ that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-js **Alias:** `readJSON()` -**Sync:** `readJsonSync()`, `readJSONSync()` - ## Example: ```js diff --git a/docs/remove-sync.md b/docs/remove-sync.md new file mode 100644 index 00000000..1925f246 --- /dev/null +++ b/docs/remove-sync.md @@ -0,0 +1,14 @@ +# removeSync(dir) + +Removes a file or directory. The directory can have contents. Like `rm -rf`. + +## Example: + +```js +const fs = require('fs-extra') + +// remove file +fs.removeSync('/tmp/myfile') + +fs.removeSync('/home/jprichardson') // I just deleted my entire HOME directory. +``` diff --git a/docs/remove.md b/docs/remove.md index c96c47b8..136cdd93 100644 --- a/docs/remove.md +++ b/docs/remove.md @@ -2,8 +2,6 @@ Removes a file or directory. The directory can have contents. Like `rm -rf`. -**Sync:** `removeSync()` - ## Example: ```js diff --git a/docs/writeJson-sync.md b/docs/writeJson-sync.md new file mode 100644 index 00000000..1d7022d2 --- /dev/null +++ b/docs/writeJson-sync.md @@ -0,0 +1,17 @@ +# writeJsonSync(file, object, [options]) + +Writes an object to a JSON file. `options` are the same that +you'd pass to [`jsonFile.writeFileSync()`](https://github.com/jprichardson/node-jsonfile#writefilesyncfilename-obj-options). + +**Alias:** `writeJSONSync()` + +## Example: + +```js +const fs = require('fs-extra') + +fs.writeJsonSync('./package.json', {name: 'fs-extra'}) +``` +--- + +**See also:** [`outputJsonSync()`](outputJson-sync.md) diff --git a/docs/writeJson.md b/docs/writeJson.md index 8f00106b..d76bbfdd 100644 --- a/docs/writeJson.md +++ b/docs/writeJson.md @@ -5,8 +5,6 @@ you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-json **Alias:** `writeJSON()` -**Sync:** `writeJsonSync()`, `writeJSONSync()` - ## Example: ```js