Node 本身有 fs 模块,但并不是很好用,fs-extra 是其替代品。fs-extra 所有异步API都支持 callback 和 promise 两种调用方式,使用起来很方便。
增
复制 copy
复制文件或文件夹,当复制文件夹时,dest 参数不能是目录名
fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {if (err) return console.error(err)console.log('success!')}) // copies filefs.copy('/tmp/mydir', '/tmp/mynewdir', err => {if (err) return console.error(err)console.log('success!')}) // copies directory, even if it has subdirectories or files
移动 move
移动文件或文件夹,当复制文件夹时,dest 参数不能是目录名
fs.move(src, dest).then(() => {console.log('success!')}).catch(err => {console.error(err)})// set overrite:truefs.move('/tmp/somedir', '/tmp/may/already/exist/somedir', { overwrite: true }, err => {if (err) return console.error(err)console.log('success!')})
生成文件 outputFile
const file = '/tmp/this/path/does/not/exist/file.txt'fs.outputFile(file, 'hello!').then(() => fs.readFile(file, 'utf8')).then(data => {console.log(data) // => hello!}).catch(err => {console.error(err)})
生成文件 outputJson/readJson/writeJson
writeJson 和 outputJson 几乎一致,但 outputJson 如果 file 不存在会自动创建一个,更好用
const file = '/tmp/this/path/does/not/exist/file.json'fs.outputJson(file, {name: 'JP'}).then(() => fs.readJson(file)).then(data => {console.log(data.name) // => JP}).catch(err => {console.error(err)})
删
删除目录 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()
删除文件或目录 remove
如果目录名不存在,则什么也不做,不像 emptyDir 会创建目录
fs.remove('/tmp/myfile').then(() => {console.log('success!')}).catch(err => {console.error(err)})
查
目录是否存在 ensureDir
fs.ensureDir(dir).then(() => {console.log('success!')}).catch(err => {console.error(err)})
文件是否存在 ensureFile
fs.ensureFile(file).then(() => {console.log('success!')}).catch(err => {console.error(err)})
链接(硬/软)是否存在 ensureLink/ensureSymlink
const srcPath = '/tmp/file.txt'const destPath = '/tmp/this/path/does/not/exist/file.txt'fs.ensureLink(srcPath, destPath).then(() => {console.log('success!')}).catch(err => {console.error(err)})fs.ensureSymlink(srcPath, destPath).then(() => {console.log('success!')}).catch(err => {console.error(err)})
路径是否存在
const file = '/tmp/this/path/does/not/exist/file.txt'fs.pathExists(file).then(exists => console.log(exists)) // => false
graceful-fs
此外,fs-extra 集成了 graceful-fs,并向外暴露了 graceful-fs 的方法,也就是说 graceful-fs 的方法也是可以直接在 fs-extra 里使用的:
const api = ['access','appendFile','chmod','chown','close','copyFile','fchmod','fchown','fdatasync','fstat','fsync','ftruncate','futimes','lchmod','lchown','link','lstat','mkdir','mkdtemp','open','opendir','readdir','readFile','readlink','realpath','rename','rm','rmdir','stat','symlink','truncate','unlink','utimes','writeFile'].filter(key => {// Some commands are not available on some systems. Ex:// fs.opendir was added in Node.js v12.12.0// fs.rm was added in Node.js v14.14.0// fs.lchown is not available on at least some Linuxreturn typeof fs[key] === 'function'})
