Node.js: fs-extra
=================
[![build status](https://api.travis-ci.org/jprichardson/node-fs-extra.svg)](http://travis-ci.org/jprichardson/node-fs-extra)
[![windows Build status](https://img.shields.io/appveyor/ci/jprichardson/node-fs-extra/master.svg?label=windows%20build)](https://ci.appveyor.com/project/jprichardson/node-fs-extra/branch/master)
[![downloads per month](http://img.shields.io/npm/dm/fs-extra.svg)](https://www.npmjs.org/package/fs-extra)
[![Coverage Status](https://img.shields.io/coveralls/jprichardson/node-fs-extra.svg)](https://coveralls.io/r/jprichardson/node-fs-extra)
`fs-extra` adds file system methods that aren't included in the native `fs` module. It is a drop in replacement for `fs`.
**NOTE (2016-01-13):** Node v0.10 will be unsupported AFTER Ubuntu LTS releases their next version AND [Amazon Lambda
upgrades](http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html) its Node.js runtime from v0.10.
I anticipate this will happen around late spring / summer 2016. Please prepare accordingly. After this, we'll make a strong push
for a 1.0.0 release.
Why?
----
I got tired of including `mkdirp`, `rimraf`, and `cp -r` in most of my projects.
Installation
------------
npm install --save fs-extra
Usage
-----
`fs-extra` is a drop in replacement for native `fs`. All methods in `fs` are unmodified and attached to `fs-extra`.
You don't ever need to include the original `fs` module again:
```js
var fs = require('fs') // this is no longer necessary
```
you can now do this:
```js
var fs = require('fs-extra')
```
or if you prefer to make it clear that you're using `fs-extra` and not `fs`, you may want
to name your `fs` variable `fse` like so:
```js
var fse = require('fs-extra')
```
you can also keep both, but it's redundant:
```js
var fs = require('fs')
var fse = require('fs-extra')
```
Sync vs Async
-------------
Most methods are async by default (they take a callback with an `Error` as first argument).
Sync methods on the other hand will throw if an error occurs.
Example:
```js
var fs = require('fs-extra')
fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
if (err) return console.error(err)
console.log("success!")
});
try {
fs.copySync('/tmp/myfile', '/tmp/mynewfile')
console.log("success!")
} catch (err) {
console.error(err)
}
```
Methods
-------
- [copy](#copy)
- [copySync](#copy)
- [createOutputStream](#createoutputstreamfile-options)
- [emptyDir](#emptydirdir-callback)
- [emptyDirSync](#emptydirdir-callback)
- [ensureFile](#ensurefilefile-callback)
- [ensureFileSync](#ensurefilefile-callback)
- [ensureDir](#ensuredirdir-callback)
- [ensureDirSync](#ensuredirdir-callback)
- [ensureLink](#ensurelinksrcpath-dstpath-callback)
- [ensureLinkSync](#ensurelinksrcpath-dstpath-callback)
- [ensureSymlink](#ensuresymlinksrcpath-dstpath-type-callback)
- [ensureSymlinkSync](#ensuresymlinksrcpath-dstpath-type-callback)
- [mkdirs](#mkdirsdir-callback)
- [mkdirsSync](#mkdirsdir-callback)
- [move](#movesrc-dest-options-callback)
- [outputFile](#outputfilefile-data-options-callback)
- [outputFileSync](#outputfilefile-data-options-callback)
- [outputJson](#outputjsonfile-data-options-callback)
- [outputJsonSync](#outputjsonfile-data-options-callback)
- [readJson](#readjsonfile-options-callback)
- [readJsonSync](#readjsonfile-options-callback)
- [remove](#removedir-callback)
- [removeSync](#removedir-callback)
- [walk](#walk)
- [writeJson](#writejsonfile-object-options-callback)
- [writeJsonSync](#writejsonfile-object-options-callback)
**NOTE:** You can still use the native Node.js methods. They are copied over to `fs-extra`.
### copy()
**copy(src, dest, [options], callback)**
Copy a file or directory. The directory can have contents. Like `cp -r`.
Options:
- clobber (boolean): overwrite existing file or directory
- preserveTimestamps (boolean): will set last modification and access times to the ones of the original source files, default is `false`.
- filter: Function or RegExp to filter copied files. If function, return true to include, false to exclude. If RegExp, same as function, where `filter` is `filter.test`.
Sync: `copySync()`
Example:
```js
var fs = require('fs-extra')
fs.copy('/tmp/myfile', '/tmp/mynewfile', function (err) {
if (err) return console.error(err)
console.log("success!")
}) // copies file
fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) {
if (err) return console.error(err)
console.log('success!')
}) // copies directory, even if it has subdirectories or files
```
### createOutputStream(file, [options])
Exactly like `createWriteStream`, but if the directory does not exist, it's created.
Examples:
```js
var fs = require('fs-extra')
// if /tmp/some does not exist, it is created
var ws = fs.createOutputStream('/tmp/some/file.txt')
ws.write('hello\n')
```
Note on naming: you'll notice that fs-extra has some methods like `fs.outputJson`, `fs.outputFile`, etc that use the
word `output` to denote that if the containing directory does not exist, it should be created. If you can think of a
better succinct nomenclature for these methods, please open an issue for discussion. Thanks.
### emptyDir(dir, [callback])
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: `emptydir()`
Sync: `emptyDirSync()`, `emptydirSync()`
Example:
```js
var fs = require('fs-extra')
// assume this directory has a lot of files and folders
fs.emptyDir('/tmp/some/dir', function (err) {
if (!err) console.log('success!')
})
```
### ensureFile(file, callback)
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: `createFile()`
Sync: `createFileSync()`,`ensureFileSync()`
Example:
```js
var fs = require('fs-extra')
var file = '/tmp/this/path/does/not/exist/file.txt'
fs.ensureFile(file, function (err) {
console.log(err) // => null
// file has now been created, including the directory it is to be placed in
})
```
### ensureDir(dir, callback)
Ensures that the directory exists. If the directory structure does not exist, it is created.
Sync: `ensureDirSync()`
Example:
```js
var fs = require('fs-extra')
var dir = '/tmp/this/path/does/not/exist'
fs.ensureDir(dir, function (err) {
console.log(err) // => null
// dir has now been created, including the directory it is to be placed in
})
```
### ensureLink(srcpath, dstpath, callback)
Ensures that the link exists. If the directory structure does not exist, it is created.
Sync: `ensureLinkSync()`
Example:
```js
var fs = require('fs-extra')
var srcpath = '/tmp/file.txt'
var dstpath = '/tmp/this/path/does/not/exist/file.txt'
fs.ensureLink(srcpath, dstpath, function (err) {
console.log(err) // => null
// link has now been created, including the directory it is to be placed in
})
```
### ensureSymlink(srcpath, dstpath, [type], callback)
Ensures that the symlink exists. If the directory structure does not exist, it is created.
Sync: `ensureSymlinkSync()`
Example:
```js
var fs = require('fs-extra')
var srcpath = '/tmp/file.txt'
var dstpath = '/tmp/this/path/does/not/exist/file.txt'
fs.ensureSymlink(srcpath, dstpath, function (err) {
console.log(err) // => null
// symlink has now been created, including the directory it is to be placed in
})
```
### mkdirs(dir, callback)
Creates a directory. If the parent hierarchy doesn't exist, it's created. Like `mkdir -p`.
Alias: `mkdirp()`
Sync: `mkdirsSync()` / `mkdirpSync()`
Examples:
```js
var fs = require('fs-extra')
fs.mkdirs('/tmp/some/long/path/that/prob/doesnt/exist', function (err) {
if (err) return console.error(err)
console.log("success!")
})
fs.mkdirsSync('/tmp/another/path')
```
### move(src, dest, [options], callback)
Moves a file or direct
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
atom插件linter和linter-gcc (587个子文件)
.babelrc 65B
.babelrc 32B
.babelrc 32B
.babelrc 32B
.babelrc 32B
.babelrc 32B
range.bnf 629B
benchmark-native.c 613B
missing_include.c 93B
comment.c 93B
error.c 77B
semver.cmd 182B
shjs.cmd 180B
rimraf.cmd 174B
missing_include.cpp 94B
comment.cpp 94B
file.cpp 93B
file.cpp 93B
error.cpp 78B
.editorconfig 414B
.editorconfig 414B
.eslintignore 37B
.eslintignore 24B
.eslintignore 24B
.eslintignore 18B
.eslintignore 17B
.eslintignore 17B
.eslintignore 10B
.eslintignore 10B
.eslintignore 6B
.eslintignore 5B
.eslintignore 4B
.eslintignore 4B
.flowconfig 148B
.flowconfig 130B
.flowconfig 85B
.flowconfig 85B
.flowconfig 85B
.flowconfig 85B
.flowconfig 85B
.gitattributes 68B
.gitattributes 68B
.gitattributes 68B
.gitattributes 68B
bench.gnu 6KB
semver.js 32KB
validate-spec.js 31KB
minimatch.js 25KB
minimatch.js 25KB
index.js 21KB
index.js 21KB
glob.js 19KB
linter-registry-spec.js 19KB
select-list-view.test.js 15KB
message-registry-spec.js 15KB
helpers-spec.js 13KB
test.js 12KB
sync.js 12KB
tmp.js 11KB
indie-delegate-spec.js 10KB
select-list-view.js 10KB
index.js 10KB
index.js 9KB
polyfills.js 9KB
old.js 8KB
rimraf.js 8KB
component-helpers.js 8KB
exec.js 7KB
main.js 7KB
helpers.js 7KB
component-helpers.js 7KB
main.js 7KB
named-js-regexp.js 7KB
patch.js 7KB
graceful-fs.js 7KB
helpers.js 6KB
helpers.js 6KB
chmod.js 6KB
common.js 6KB
utility.js 6KB
ncp.js 6KB
cp.js 6KB
patch.js 6KB
linter-registry.js 6KB
helpers.js 6KB
index.js 5KB
update-props.js 5KB
uuid.js 5KB
helpers.js 5KB
dirs.js 5KB
common.js 5KB
config-spec.js 5KB
index.js 5KB
index.js 5KB
message-registry.js 5KB
helpers.js 5KB
rm.js 4KB
commands.js 4KB
update-props.js 4KB
view.js 4KB
共 587 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
蜗妞慢慢爬
- 粉丝: 17
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功