-
Notifications
You must be signed in to change notification settings - Fork 774
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from jprichardson/docs-split-out-sync-methods
Docs: Split out sync methods
- Loading branch information
Showing
24 changed files
with
251 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters