Browse Source

Docs: Split out sync methods

ci/travis-osximage
Mani Maghsoudlou 8 years ago
parent
commit
02c2a629b8
  1. 31
      README.md
  2. 35
      docs/copy-sync.md
  3. 2
      docs/copy.md
  4. 14
      docs/emptyDir-sync.md
  5. 2
      docs/emptyDir.md
  6. 15
      docs/ensureDir-sync.md
  7. 15
      docs/ensureFile-sync.md
  8. 2
      docs/ensureFile.md
  9. 14
      docs/ensureLink-sync.md
  10. 2
      docs/ensureLink.md
  11. 14
      docs/ensureSymlink-sync.md
  12. 2
      docs/ensureSymlink.md
  13. 20
      docs/move-sync.md
  14. 10
      docs/move.md
  15. 15
      docs/outputFile-sync.md
  16. 2
      docs/outputFile.md
  17. 18
      docs/outputJson-sync.md
  18. 2
      docs/outputJson.md
  19. 29
      docs/readJson-sync.md
  20. 2
      docs/readJson.md
  21. 14
      docs/remove-sync.md
  22. 2
      docs/remove.md
  23. 17
      docs/writeJson-sync.md
  24. 2
      docs/writeJson.md

31
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`.

35
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
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`.

14
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
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

15
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
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
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

14
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
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

14
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
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

20
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
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!')
})
```

15
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
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

18
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
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

29
docs/readJson-sync.md

@ -0,0 +1,29 @@
# 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 = path.join('/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
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

14
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
docs/remove.md

@ -2,8 +2,6 @@
Removes a file or directory. The directory can have contents. Like `rm -rf`.
**Sync:** `removeSync()`
## Example:
```js

17
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
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

Loading…
Cancel
Save