Comparing version 0.1.6 to 0.1.7
@@ -6,3 +6,3 @@ 'use strict'; | ||
var test = function(){ | ||
var foldersAndFiles = q.lsdir('/Users/vincent/Data/projects/qiao/qiao.util.file/'); | ||
var foldersAndFiles = q.lsdir('./'); | ||
console.log(foldersAndFiles); | ||
@@ -9,0 +9,0 @@ }; |
@@ -6,4 +6,4 @@ 'use strict'; | ||
var test = function(){ | ||
var folderPath = './test copy/'; | ||
var filePath = './qiao-file copy.js' | ||
var folderPath = './__tests__ copy/'; | ||
var filePath = './index copy.js' | ||
@@ -10,0 +10,0 @@ // rm folder |
211
index.js
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
/** | ||
* fs | ||
* isExists | ||
* fpath : file or folder path | ||
*/ | ||
exports.fs = require('fs'); | ||
const isExists = (fpath) => { | ||
try{ | ||
fs.accessSync(fpath); | ||
return true; | ||
}catch(e){ | ||
return false; | ||
} | ||
}; | ||
/** | ||
* path | ||
* get folders and files | ||
* @param {*} fpath | ||
* @param {*} folders | ||
* @param {*} files | ||
*/ | ||
exports.path = require('path'); | ||
const getFoldersAndFiles = (fpath, folders, files) => { | ||
fs.readdirSync(fpath).forEach(function(name){ | ||
const stat = fs.statSync(fpath + name); | ||
if(stat.isDirectory()){ | ||
folders.push({ | ||
path : fpath, | ||
name : name | ||
}); | ||
getFoldersAndFiles(fpath + name + '/', folders, files); | ||
}else { | ||
files.push({ | ||
path : fpath, | ||
name : name | ||
}); | ||
} | ||
}); | ||
}; | ||
/** | ||
* extname | ||
* get file tree | ||
* @param {*} fpath | ||
* @param {*} fileTree | ||
*/ | ||
exports.extname = require('./lib/extname.js'); | ||
const getFileTree = (fpath, fileTreeChildrens, ignore) => { | ||
fs.readdirSync(fpath).forEach(function(name){ | ||
const rpath = fpath + name; | ||
if(rpath.indexOf(ignore) > -1) return; | ||
const stat = fs.statSync(rpath); | ||
if(stat.isDirectory()){ | ||
let info = {}; | ||
info.path = rpath + '/'; | ||
info.name = ''; | ||
info.children = []; | ||
fileTreeChildrens.push(info); | ||
getFileTree(info.path, info.children); | ||
}else { | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
fileTreeChildrens.push(info); | ||
} | ||
}); | ||
}; | ||
/** | ||
* isExists | ||
* check dir | ||
* @param {*} dir | ||
* @param {*} list | ||
*/ | ||
exports.isExists = require('./lib/is-exists.js'); | ||
const checkDir = (dir, list) => { | ||
const pdir = path.dirname(dir); | ||
if(!isExists(pdir)){ | ||
list.push(pdir); | ||
checkDir(pdir, list); | ||
} | ||
}; | ||
/** | ||
* ls dir | ||
* cp | ||
* @param {*} src file or folder src path | ||
* @param {*} dest file or folder dest path | ||
* @returns | ||
*/ | ||
exports.lsdir = require('./lib/lsdir.js'); | ||
const cp = (src, dest) => { | ||
try{ | ||
const stat = fs.statSync(src); | ||
if(stat.isDirectory()){ | ||
fs.cpSync(src, dest, {recursive:true}); | ||
}else { | ||
fs.copyFileSync(src, dest); | ||
} | ||
return true; | ||
}catch(e){ | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
/** | ||
* mkdir | ||
* rm | ||
* fpath, file or folder path, folder must end with / | ||
*/ | ||
exports.mkdir = require('./lib/mkdir.js'); | ||
const rm = (fpath) => { | ||
try{ | ||
// rm file | ||
const pathStat = fs.statSync(fpath); | ||
if(!pathStat.isDirectory()){ | ||
fs.unlinkSync(fpath); | ||
return true; | ||
} | ||
// ls dir | ||
let folders = []; | ||
let files = []; | ||
getFoldersAndFiles(fpath, folders, files); | ||
folders.reverse(); | ||
// rm folder | ||
for(let i=0; i<files.length; i++) fs.unlinkSync(files[i].path + files[i].name); | ||
for(let i=0; i<folders.length; i++) fs.rmdirSync(folders[i].path + folders[i].name); | ||
fs.rmdirSync(fpath); | ||
// return | ||
return true; | ||
}catch(e){ | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
/** | ||
* rm | ||
* ls dir | ||
* dir : must end with / | ||
*/ | ||
const lsdir = (dir) => { | ||
let folders = []; | ||
let files = []; | ||
getFoldersAndFiles(dir, folders, files); | ||
return { | ||
folders : folders, | ||
files : files | ||
}; | ||
}; | ||
/** | ||
* ls tree | ||
* @param {*} dir must end with / | ||
* @param {*} ignore | ||
* @returns | ||
*/ | ||
exports.rm = require('./lib/rm.js'); | ||
const lstree = (dir, ignore) => { | ||
let root = {}; | ||
root.path = dir; | ||
root.name = ''; | ||
root.children = []; | ||
getFileTree(dir, root.children, ignore); | ||
return root; | ||
}; | ||
/** | ||
* cp | ||
* mk dir | ||
* dir : must end with / | ||
*/ | ||
const mkdir = (dir) => { | ||
try{ | ||
// check dir | ||
var dirs = []; | ||
checkDir(dir, dirs); | ||
// check dirs | ||
if(!dirs.length) return false; | ||
// mkdir | ||
dirs.reverse(); | ||
for(var i=0; i<dirs.length; i++) fs.mkdirSync(dirs[i]); | ||
return true; | ||
}catch(e){ | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
/** | ||
* extname | ||
* filePath : file path | ||
*/ | ||
exports.cp = require('./lib/cp.js'); | ||
const extname = (filePath) => { | ||
if(!filePath) return null; | ||
return path.extname(filePath.toLowerCase()); | ||
}; | ||
exports.fs = fs; | ||
exports.path = path; | ||
exports.cp = cp; | ||
exports.extname = extname; | ||
exports.isExists = isExists; | ||
exports.lsdir = lsdir; | ||
exports.lstree = lstree; | ||
exports.mkdir = mkdir; | ||
exports.rm = rm; |
{ | ||
"name": "qiao-file", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "nodejs file tool", | ||
@@ -13,7 +13,14 @@ "author": "uikoo9 <uikoo9@qq.com>", | ||
}, | ||
"scripts": {}, | ||
"scripts": { | ||
"build": "rollup --config rollup.config.js", | ||
"prepublish": "npm run build", | ||
"test": "node " | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
}, | ||
"gitHead": "8c162158905d123d82c0ca5b016b8f1d413b01c8" | ||
"devDependencies": { | ||
"rollup": "^2.70.2" | ||
}, | ||
"gitHead": "5e9e16d7d206079f9c7af85dd4996ff52ccec160" | ||
} |
@@ -49,2 +49,17 @@ # qiao-file | ||
### lstree | ||
```javascript | ||
'use strict'; | ||
var q = require('qiao-file'); | ||
var test = function(){ | ||
var fileTree = q.lstree('./', 'node_modules'); | ||
console.log(fileTree); | ||
console.log(JSON.stringify(fileTree)); | ||
}; | ||
test(); | ||
``` | ||
### mkdir | ||
@@ -106,2 +121,6 @@ ```javascript | ||
## version | ||
### 0.1.2.20220419 | ||
1. es6 | ||
2. add lstree | ||
### 0.1.1.20220417 | ||
@@ -108,0 +127,0 @@ 1. add lerna |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
13895
18
462
163
1
1