Skip to content

Commit

Permalink
Merge pull request #382 from jprichardson/docs-split-out-sync-methods
Browse files Browse the repository at this point in the history
Docs: Split out sync methods
  • Loading branch information
jprichardson committed Mar 8, 2017
2 parents 1697574 + 10756de commit b5c72dc
Show file tree
Hide file tree
Showing 24 changed files with 251 additions and 31 deletions.
31 changes: 19 additions & 12 deletions README.md
Expand Up @@ -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`.
Expand Down
35 changes: 35 additions & 0 deletions 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 })
```
2 changes: 0 additions & 2 deletions docs/copy.md
Expand Up @@ -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`.
Expand Down
14 changes: 14 additions & 0 deletions 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')
```
2 changes: 0 additions & 2 deletions docs/emptyDir.md
Expand Up @@ -4,8 +4,6 @@ Ensures that a directory is empty. Deletes directory contents if the directory i

**Alias:** `emptydir()`

**Sync:** `emptyDirSync()`, `emptydirSync()`

## Example:

```js
Expand Down
15 changes: 15 additions & 0 deletions 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
```
15 changes: 15 additions & 0 deletions 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
```
2 changes: 0 additions & 2 deletions docs/ensureFile.md
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions 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
```
2 changes: 0 additions & 2 deletions docs/ensureLink.md
Expand Up @@ -2,8 +2,6 @@

Ensures that the link exists. If the directory structure does not exist, it is created.

**Sync:** `ensureLinkSync()`

## Example:

```js
Expand Down
14 changes: 14 additions & 0 deletions 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
```
2 changes: 0 additions & 2 deletions docs/ensureSymlink.md
Expand Up @@ -2,8 +2,6 @@

Ensures that the symlink exists. If the directory structure does not exist, it is created.

**Sync:** `ensureSymlinkSync()`

## Example:

```js
Expand Down
20 changes: 20 additions & 0 deletions 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 })
```
10 changes: 10 additions & 0 deletions docs/move.md
Expand Up @@ -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!')
})
```
15 changes: 15 additions & 0 deletions 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!
```
2 changes: 0 additions & 2 deletions docs/outputFile.md
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions 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
```
2 changes: 1 addition & 1 deletion docs/outputJson.md
Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions 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
```
2 changes: 0 additions & 2 deletions docs/readJson.md
Expand Up @@ -5,8 +5,6 @@ that you'd pass to [`jsonFile.readFile`](https://github.com/jprichardson/node-js

**Alias:** `readJSON()`

**Sync:** `readJsonSync()`, `readJSONSync()`

## Example:

```js
Expand Down
14 changes: 14 additions & 0 deletions 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.
```
2 changes: 0 additions & 2 deletions docs/remove.md
Expand Up @@ -2,8 +2,6 @@

Removes a file or directory. The directory can have contents. Like `rm -rf`.

**Sync:** `removeSync()`

## Example:

```js
Expand Down
17 changes: 17 additions & 0 deletions 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)
2 changes: 0 additions & 2 deletions docs/writeJson.md
Expand Up @@ -5,8 +5,6 @@ you'd pass to [`jsonFile.writeFile()`](https://github.com/jprichardson/node-json

**Alias:** `writeJSON()`

**Sync:** `writeJsonSync()`, `writeJSONSync()`

## Example:

```js
Expand Down

0 comments on commit b5c72dc

Please sign in to comment.