fs-extra是fs的一個擴展,提供了很是多的便利API,而且繼承了fs全部方法和爲fs方法添加了promise的支持。html
它應該是 fs 的替代品。node
我厭倦了包括mkdirp,rimraf以及ncp在個人大部分項目中。npm
使用前,必須瞭解第三方庫給現有庫解決了哪些問題,不要爲了使用而使用。json
fs-extra模擬了相似如Linux下的命令:api
root$ rm -rf /
root$ mv tmpDir tmpNewDir
root$ mkdir -p one/two
root$ cp -r tmp tmpNew
...
複製代碼
這很方便不是嗎?這才讓我深深地愛上了它!promise
npm install fs-extra -S
複製代碼
應該老是fs-extra代替fs使用,全部fs方法都附在fs-extra,fs若是未傳遞迴調,則全部方法都將返回promise。bash
再也不須要這個異步
const fs = require('fs');
複製代碼
你如今能夠這樣作async
const fs = require('fs-extra');
複製代碼
若是你但願明確表示你在使用fs-extra,能夠將fs標識符改成fse函數
const fse = require('fs-extra');
複製代碼
你能夠保留二者使用,但它是多餘的,由於 fs-extra 繼承了fs
const fs = require('fs');
const fse = require('fs-extra');
複製代碼
大多數方法默認爲異步,若是未傳遞迴調,則全部異步方法將返回一個promise。
一個典型的例子:
const fs = require('fs-extra')
// 異步方法,返回promise
fs.copy('/tmp/myfile', '/tmp/mynewfile')
.then(() => console.log('success!'))
.catch(err => console.error(err))
// 異步方法,回調函數
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
if (err) return console.error(err)
console.log('success!')
})
// 同步方法,注意必須使用try catch包裹着才能捕獲錯誤
try {
fs.copySync('/tmp/myfile', '/tmp/mynewfile')
console.log('success!')
} catch (err) {
console.error(err)
}
// Async/Await:
async function copyFiles () {
try {
await fs.copy('/tmp/myfile', '/tmp/mynewfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
copyFiles()
複製代碼
下面的全部方法都是fs-extra擴展方法
Async/異步
Sync/同步
複製文件或目錄,目錄能夠包含內容,相似 cp -r
例子:
const fs = require('fs-extra')
// With a callback:
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
if (err) return console.error(err)
console.log('success!')
}) // copies file
fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
if (err) return console.error(err)
console.log('success!')
}) // copies directory, even if it has subdirectories or files
// With Promises:
fs.copy('/tmp/myfile', '/tmp/mynewfile')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example () {
try {
await fs.copy('/tmp/myfile', '/tmp/mynewfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
example()
複製代碼
使用過濾函數
const fs = require('fs-extra')
const filterFunc = (src, dest) => {
// your logic here
// it will be copied if return true
}
fs.copy('/tmp/mydir', '/tmp/mynewdir', { filter: filterFunc }, err => {
if (err) return console.error(err)
console.log('success!')
})
複製代碼
確保目錄爲空。若是目錄不爲空,則刪除目錄內容。若是該目錄不存在,則建立該目錄。目錄自己不會被刪除。
別名: emptydir()
例子:
const fs = require('fs-extra')
// assume this directory has a lot of files and folders
// With a callback:
fs.emptyDir('/tmp/some/dir', err => {
if (err) return console.error(err)
console.log('success!')
})
// With Promises:
fs.emptyDir('/tmp/some/dir')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example () {
try {
await fs.emptyDir('/tmp/some/dir')
console.log('success!')
} catch (err) {
console.error(err)
}
}
example()
複製代碼
確保文件存在。若是請求建立的文件位於不存在的目錄中,則會建立這些目錄。若是該文件已存在,則不進行修改。
別名: createFile()
例子:
const fs = require('fs-extra')
const file = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.ensureFile(file, err => {
console.log(err) // => null
// file has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureFile(file)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (f) {
try {
await fs.ensureFile(f)
console.log('success!')
} catch (err) {
console.error(err)
}
}
example(file)
複製代碼
若是目錄結構不存在,則建立它,若是目錄存在,則不進行建立,相似mkdir -p。
別名: mkdirs(), mkdirp()
例子:
const fs = require('fs-extra')
const dir = '/tmp/this/path/does/not/exist'
// With a callback:
fs.ensureDir(dir, err => {
console.log(err) // => null
// dir has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureDir(dir)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (directory) {
try {
await fs.ensureDir(directory)
console.log('success!')
} catch (err) {
console.error(err)
}
}
example(dir)
複製代碼
確保連接存在。若是目錄結構不存在,則建立它。
例子
const fs = require('fs-extra')
const srcpath = '/tmp/file.txt'
const dstpath = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.ensureLink(srcpath, dstpath, err => {
console.log(err) // => null
// link has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureLink(srcpath, dstpath)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (src, dest) {
try {
await fs.ensureLink(src, dest)
console.log('success!')
} catch (err) {
console.error(err)
}
}
example(srcpath, dstpath)
複製代碼
確保符號連接存在。若是目錄結構不存在,則建立它。
const fs = require('fs-extra')
const srcpath = '/tmp/file.txt'
const dstpath = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.ensureSymlink(srcpath, dstpath, err => {
console.log(err) // => null
// symlink has now been created, including the directory it is to be placed in
})
// With Promises:
fs.ensureSymlink(srcpath, dstpath)
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (src, dest) {
try {
await fs.ensureSymlink(src, dest)
console.log('success!')
} catch (err) {
console.error(err)
}
}
example(srcpath, dstpath)
複製代碼
移動文件或目錄,甚至跨設備。 相似 mv
例子:
const fs = require('fs-extra')
const srcpath = '/tmp/file.txt'
const dstpath = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.move(srcpath, dstpath, err => {
if (err) return console.error(err)
console.log('success!')
})
// With Promises:
fs.move(srcpath, dstpath, {
overwrite: true
})
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (src, dest) {
try {
await fs.move(srcpath, dstpath)
console.log('success!')
} catch (err) {
console.error(err)
}
}
example(srcpath, dstpath)
複製代碼
幾乎與writeFile(即它覆蓋)相同,除了若是父目錄不存在,則建立它。file必須是文件路徑(不容許使用緩衝區或文件描述符)。
例子:
const fs = require('fs-extra')
const file = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.outputFile(file, 'hello!', err => {
console.log(err) // => null
fs.readFile(file, 'utf8', (err, data) => {
if (err) return console.error(err)
console.log(data) // => hello!
})
})
// With Promises:
fs.outputFile(file, 'hello!')
.then(() => fs.readFile(file, 'utf8'))
.then(data => {
console.log(data) // => hello!
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (f) {
try {
await fs.outputFile(f, 'hello!')
const data = await fs.readFile(f, 'utf8')
console.log(data) // => hello!
} catch (err) {
console.error(err)
}
}
example(file)
複製代碼
幾乎相同writeJson,除了若是目錄不存在,它就被建立了。
別名: outputJSON()
例子:
const fs = require('fs-extra')
const file = '/tmp/this/path/does/not/exist/file.json'
// With a callback:
fs.outputJson(file, {name: 'JP'}, err => {
console.log(err) // => null
fs.readJson(file, (err, data) => {
if (err) return console.error(err)
console.log(data.name) // => JP
})
})
// With Promises:
fs.outputJson(file, {name: 'JP'})
.then(() => fs.readJson(file))
.then(data => {
console.log(data.name) // => JP
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (f) {
try {
await fs.outputJson(f, {name: 'JP'})
const data = await fs.readJson(f)
console.log(data.name) // => JP
} catch (err) {
console.error(err)
}
}
example(file)
複製代碼
經過檢查文件系統來測試給定路徑是否存在。相似fs.exists
例子:
const fs = require('fs-extra')
const file = '/tmp/this/path/does/not/exist/file.txt'
// With a callback:
fs.pathExists(file, (err, exists) => {
console.log(err) // => null
console.log(exists) // => false
})
// Promise usage:
fs.pathExists(file)
.then(exists => console.log(exists)) // => false
// With async/await:
async function example (f) {
const exists = await fs.pathExists(f)
console.log(exists) // => false
}
example(file)
複製代碼
讀取JSON文件,而後將其解析爲對象
別名: readJSON()
例子:
const fs = require('fs-extra')
// With a callback:
fs.readJson('./package.json', (err, packageObj) => {
if (err) console.error(err)
console.log(packageObj.version) // => 0.1.3
})
// With Promises:
fs.readJson('./package.json')
.then(packageObj => {
console.log(packageObj.version) // => 0.1.3
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example () {
try {
const packageObj = await fs.readJson('./package.json')
console.log(packageObj.version) // => 0.1.3
} catch (err) {
console.error(err)
}
}
example()
複製代碼
刪除文件或目錄。該目錄能夠包含內容, 相似 rm -rf
例子:
const fs = require('fs-extra')
// remove file
// With a callback:
fs.remove('/tmp/myfile', err => {
if (err) return console.error(err)
console.log('success!')
})
fs.remove('/home/jprichardson', err => {
if (err) return console.error(err)
console.log('success!') // I just deleted my entire HOME directory.
})
// With Promises:
fs.remove('/tmp/myfile')
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example (src, dest) {
try {
await fs.remove('/tmp/myfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
example()
複製代碼
將對象寫入JSON文件, 幾乎與outputJson相同,除了必須保證目錄存在外。
別名: writeJSON()
例子:
const fs = require('fs-extra')
// With a callback:
fs.writeJson('./package.json', {name: 'fs-extra'}, err => {
if (err) return console.error(err)
console.log('success!')
})
// With Promises:
fs.writeJson('./package.json', {name: 'fs-extra'})
.then(() => {
console.log('success!')
})
.catch(err => {
console.error(err)
})
// With async/await:
async function example () {
try {
await fs.writeJson('./package.json', {name: 'fs-extra'})
console.log('success!')
} catch (err) {
console.error(err)
}
}
example()
複製代碼
毋庸置疑fs-extra用在生產環境絕對是不錯的選擇,可是想要玩轉黑科技仍是要多多瞭解fs模塊,畢竟它纔是老大。