Comparing version 1.0.1 to 1.0.2
43
index.js
@@ -1,40 +0,7 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { emptyDir,rmDir } = require('./lib/handleDir.js'); | ||
const { getAllFiles } = require('./lib/getAllFiles.js'); | ||
const obj = {}; | ||
module.exports = Object.freeze({ emptyDir,rmDir,getAllFiles }); | ||
function emptyDir(dir){ | ||
const files = fs.readdirSync(dir, {withFileTypes:true}); | ||
//说明:withFileTypes:true, arr的元素是dirent对象,dirent可以判断:本资源是目录还是文件 | ||
if(files.length<1){ | ||
return; | ||
}; | ||
files.forEach((dirent,index)=>{ | ||
if(dirent.isFile()){ //判断本资源是不是文件 | ||
fs.unlinkSync(dir+'/'+dirent.name); | ||
}else{ | ||
emptyDir(dir+'/'+dirent.name); | ||
fs.rmdirSync(dir+'/'+dirent.name); | ||
} | ||
}); | ||
} | ||
obj.emptyDir = dirPath => { | ||
const bool = fs.existsSync(dirPath); //判断目录是否存在 | ||
if(!bool) return false; | ||
emptyDir(dirPath); //清空目录的子孙目录和所有文件 | ||
return true; | ||
}; | ||
obj.rmDir = dirPath => { | ||
const bool = fs.existsSync(dirPath); //判断目录是否存在 | ||
if(!bool) return false; | ||
emptyDir(dirPath); //清空目录的子孙目录和所有文件 | ||
fs.rmdirSync(dirPath); //删除本目录 | ||
return true; | ||
} | ||
module.exports = Object.freeze(obj); | ||
let arr = getAllFiles('./'); | ||
console.log(arr) |
{ | ||
"name": "yu-node", | ||
"version": "1.0.1", | ||
"description": "仅用于node", | ||
"version": "1.0.2", | ||
"description": "仅用于node.js", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -11,6 +11,6 @@ # yu-node | ||
``` | ||
const { emptyDir, rmDir } = require('yu-node'); | ||
const { emptyDir,rmDir,getAllFiles } = require('yu-node'); | ||
``` | ||
**`emptyDir(dirPath)`:清空目录,完成返回true,否则返回false** | ||
### emptyDir(dirPath)`:清空目录,完成返回true,否则返回false | ||
``` | ||
@@ -20,5 +20,11 @@ let bool = emptyDir('./目录a'); //true 或 false | ||
**`rmDir(dirPath)`:清空并删除目录,完成返回true,否则返回false** | ||
### `rmDir(dirPath)`:清空并删除目录,完成返回true,否则返回false | ||
``` | ||
let bool = rmDir('./目录a'); //true 或 false | ||
``` | ||
### `getAllFiles(dirName)`:获取目录下的所有文件名 | ||
``` | ||
let arr = getAllFiles('./目录名'); | ||
//[ 'D:\\demo\\node-babel\\src\\foo\\a.css','D:\\demo\\node-babel\\src\\foo\\b.js',] | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3207
5
57
28
2