-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
76 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,69 @@ | ||
const fs = require('fs'); | ||
const fsPromises = require('fs').promises; | ||
const pt = require('path'); | ||
const fsFun = {}; | ||
|
||
//(读取类) | ||
['mkdir', 'rmdir', 'readdir', 'readFile', 'copyFile', 'unlink', 'exists', 'stat'].forEach(function (item) { | ||
fsFun[item] = function (pathname, copypath) { | ||
return new Promise(function (resolve, reject) { | ||
let arg = [function (err, data) { | ||
if (item === 'exists') { | ||
return resolve(err); | ||
} | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(data || true); | ||
}]; | ||
item === 'readFile' ? arg.unshift(copypath || 'utf8') : null; | ||
item === 'copyFile' ? arg.unshift(copypath || '') : null; | ||
fs[item](pathname, ...arg) | ||
}); | ||
// 是否存在 | ||
const exists = async path => { | ||
try { | ||
await fsPromises.stat(path); | ||
return true; | ||
} catch(err) { | ||
if(err.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
}); | ||
} | ||
|
||
//(写入类) | ||
['writeFile', 'appendFile'].forEach(function (item) { | ||
fsFun[item] = function (pathname, content, charset='utf8') { | ||
if (typeof content !== 'string') { | ||
content = JSON.stringify(content) | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
fs[item](pathname, content, charset, function(err, data){ | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(data || ''); | ||
}); | ||
}); | ||
// 是否文件 | ||
const isFile = async path => { | ||
try { | ||
const stats = await fsPromises.stat(path); | ||
return stats.isFile(); | ||
} catch(err) { | ||
if(err.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
}); | ||
|
||
//(判断类) | ||
fsFun.isFileSync = (path) => {return fs.existsSync(path) && fs.statSync(path).isFile();} | ||
fsFun.isDirSync = (path) => {return fs.existsSync(path) && fs.statSync(path).isDirectory();} | ||
} | ||
const isFileSync = path => { | ||
return fs.existsSync(path) && fs.statSync(path).isFile(); | ||
} | ||
|
||
['isFile', 'isDir'].forEach(function (item) { | ||
fsFun[item] = function (pathname) { | ||
return new Promise(function (resolve, reject) { | ||
fsFun.exists(pathname).then((result) => { | ||
if (!result) { | ||
resolve(result); | ||
} else { | ||
fsFun.stat(pathname).then((result) => { | ||
resolve(item === 'isFile' ? result.isFile() : result.isDirectory()); | ||
}).catch((error) => { | ||
reject(error); | ||
}); | ||
} | ||
}).catch((error) => { | ||
reject(error); | ||
}); | ||
}); | ||
// 是否目录 | ||
const isDir = async path => { | ||
try { | ||
const stats = await fsPromises.stat(path); | ||
return stats.isDirectory(); | ||
} catch(err) { | ||
if(err.code === 'ENOENT') { | ||
return false; | ||
} | ||
throw err; | ||
} | ||
}); | ||
} | ||
const isDirSync = path => { | ||
return fs.existsSync(path) && fs.statSync(path).isDirectory(); | ||
} | ||
|
||
//(多级目录生成) | ||
fsFun.mkdirs = async function (dirname) { | ||
if(await fsFun.isDir(dirname)) { | ||
// 生成多级目录 | ||
const mkdirs = async function (dirname) { | ||
if(await isDir(dirname)) { | ||
return true; | ||
} else { | ||
if(await fsFun.mkdirs(pt.dirname(dirname))) { | ||
await fsFun.mkdir(dirname); | ||
if(await mkdirs(pt.dirname(dirname))) { | ||
await fsPromises.mkdir(dirname); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
module.exports = fsFun; | ||
module.exports = { | ||
exists, | ||
isFile, | ||
isFileSync, | ||
isDir, | ||
isDirSync, | ||
mkdirs | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters