Comparing version 1.0.5 to 1.0.6
482
index.js
@@ -1,26 +0,34 @@ | ||
'use strict'; | ||
"use strict"; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var readline = require('readline'); | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var readline = require("readline"); | ||
function _interopNamespaceDefault(e) { | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== "default") { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty( | ||
n, | ||
k, | ||
d.get | ||
? d | ||
: { | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
}, | ||
} | ||
}); | ||
} | ||
n.default = e; | ||
return Object.freeze(n); | ||
); | ||
} | ||
}); | ||
} | ||
n.default = e; | ||
return Object.freeze(n); | ||
} | ||
var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs); | ||
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path); | ||
var fs__namespace = /*#__PURE__*/ _interopNamespaceDefault(fs); | ||
var path__namespace = /*#__PURE__*/ _interopNamespaceDefault(path); | ||
@@ -34,9 +42,9 @@ // fs | ||
const isExists = (fpath) => { | ||
try{ | ||
fs.accessSync(fpath); | ||
return true; | ||
}catch(e){ | ||
return false; | ||
} | ||
try { | ||
fs.accessSync(fpath); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
@@ -48,23 +56,23 @@ | ||
* get folders and files | ||
* @param {*} fpath | ||
* @param {*} folders | ||
* @param {*} files | ||
* @param {*} fpath | ||
* @param {*} folders | ||
* @param {*} files | ||
*/ | ||
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 | ||
}); | ||
} | ||
}); | ||
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, | ||
}); | ||
} | ||
}); | ||
}; | ||
@@ -74,29 +82,29 @@ | ||
* get file tree | ||
* @param {*} fpath | ||
* @param {*} fileTree | ||
* @param {*} ignores | ||
* @param {*} fpath | ||
* @param {*} fileTree | ||
* @param {*} ignores | ||
*/ | ||
const getFileTree = (fpath, fileTree, ignores) => { | ||
fs.readdirSync(fpath).forEach(function(name){ | ||
const rpath = fpath + name; | ||
if(isFileTreeIgnore(rpath, ignores)) return; | ||
fs.readdirSync(fpath).forEach(function (name) { | ||
const rpath = fpath + name; | ||
if (isFileTreeIgnore(rpath, ignores)) return; | ||
const stat = fs.statSync(rpath); | ||
if(stat.isDirectory()){ | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
info.children = []; | ||
fileTree.push(info); | ||
getFileTree(rpath + '/', info.children, ignores); | ||
}else { | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
const stat = fs.statSync(rpath); | ||
if (stat.isDirectory()) { | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
info.children = []; | ||
fileTree.push(info); | ||
} | ||
}); | ||
fileTree.push(info); | ||
getFileTree(rpath + "/", info.children, ignores); | ||
} else { | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
fileTree.push(info); | ||
} | ||
}); | ||
}; | ||
@@ -106,12 +114,12 @@ | ||
* check dir | ||
* @param {*} dir | ||
* @param {*} list | ||
* @param {*} dir | ||
* @param {*} list | ||
*/ | ||
const checkDir = (dir, list) => { | ||
const pdir = path.dirname(dir); | ||
if(!isExists(pdir)){ | ||
list.push(pdir); | ||
checkDir(pdir, list); | ||
} | ||
const pdir = path.dirname(dir); | ||
if (!isExists(pdir)) { | ||
list.push(pdir); | ||
checkDir(pdir, list); | ||
} | ||
}; | ||
@@ -121,10 +129,10 @@ | ||
const isFileTreeIgnore = (rpath, ignores) => { | ||
if(!rpath || !ignores || !ignores.length) return; | ||
if (!rpath || !ignores || !ignores.length) return; | ||
let ignore = false; | ||
for(let i=0; i<ignores.length; i++){ | ||
if(rpath.indexOf(ignores[i]) > -1) ignore = true; | ||
} | ||
let ignore = false; | ||
for (let i = 0; i < ignores.length; i++) { | ||
if (rpath.indexOf(ignores[i]) > -1) ignore = true; | ||
} | ||
return ignore; | ||
return ignore; | ||
}; | ||
@@ -138,18 +146,18 @@ | ||
* @param {*} dest file or folder dest path | ||
* @returns | ||
* @returns | ||
*/ | ||
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; | ||
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; | ||
} | ||
}; | ||
@@ -159,14 +167,14 @@ | ||
* mv | ||
* @param {*} oldPath | ||
* @param {*} newPath | ||
* @param {*} oldPath | ||
* @param {*} newPath | ||
*/ | ||
const mv = (oldPath, newPath) => { | ||
try{ | ||
fs.renameSync(oldPath, newPath); | ||
try { | ||
fs.renameSync(oldPath, newPath); | ||
return true; | ||
}catch(e){ | ||
console.log(e); | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
@@ -179,28 +187,30 @@ | ||
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; | ||
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; | ||
} | ||
}; | ||
@@ -211,14 +221,14 @@ | ||
/** | ||
* ls dir | ||
* dir : must end with / | ||
*/ | ||
* ls dir | ||
* dir : must end with / | ||
*/ | ||
const lsdir = (dir) => { | ||
let folders = []; | ||
let files = []; | ||
getFoldersAndFiles(dir, folders, files); | ||
return { | ||
folders : folders, | ||
files : files | ||
}; | ||
let folders = []; | ||
let files = []; | ||
getFoldersAndFiles(dir, folders, files); | ||
return { | ||
folders: folders, | ||
files: files, | ||
}; | ||
}; | ||
@@ -229,40 +239,40 @@ | ||
* @param {*} dir must end with / | ||
* @param {*} ignores | ||
* @returns | ||
* @param {*} ignores | ||
* @returns | ||
*/ | ||
const lstree = (dir, ignores) => { | ||
let fileTree = []; | ||
getFileTree(dir, fileTree, ignores); | ||
return fileTree; | ||
let fileTree = []; | ||
getFileTree(dir, fileTree, ignores); | ||
return fileTree; | ||
}; | ||
/** | ||
* mk dir | ||
* dir : must end with / | ||
*/ | ||
* mk dir | ||
* dir : must end with / | ||
*/ | ||
const mkdir = (dir) => { | ||
try{ | ||
// check | ||
if(!dir || !dir.endsWith('/')) return false; | ||
try { | ||
// check | ||
if (!dir || !dir.endsWith("/")) return false; | ||
// is exists | ||
if(isExists(dir)) return true; | ||
// check dir | ||
var dirs = [dir]; | ||
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; | ||
} | ||
// is exists | ||
if (isExists(dir)) return true; | ||
// check dir | ||
var dirs = [dir]; | ||
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; | ||
} | ||
}; | ||
@@ -277,5 +287,5 @@ | ||
const extname = (filePath) => { | ||
if (!filePath) return null; | ||
if (!filePath) return null; | ||
return path.extname(filePath.toLowerCase()); | ||
return path.extname(filePath.toLowerCase()); | ||
}; | ||
@@ -285,19 +295,19 @@ | ||
* readFile | ||
* @param {*} filePath | ||
* @param {*} filePath | ||
* @param {*} options https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsreadfilesyncpath-options | ||
*/ | ||
const readFile = (filePath, options) => { | ||
// check | ||
if (!filePath) return; | ||
// check | ||
if (!filePath) return; | ||
try { | ||
// opt | ||
const opt = { encoding: 'utf8' }; | ||
options = options || opt; | ||
try { | ||
// opt | ||
const opt = { encoding: "utf8" }; | ||
options = options || opt; | ||
return fs.readFileSync(filePath, options); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
return fs.readFileSync(filePath, options); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
}; | ||
@@ -307,19 +317,19 @@ | ||
* readFileLineByLine | ||
* @param {*} filePath | ||
* @param {*} onLine | ||
* @param {*} onClose | ||
* @param {*} filePath | ||
* @param {*} onLine | ||
* @param {*} onClose | ||
*/ | ||
const readFileLineByLine = (filePath, onLine, onClose) => { | ||
// rl | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream(filePath, { encoding: 'utf8' }) | ||
}); | ||
// rl | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream(filePath, { encoding: "utf8" }), | ||
}); | ||
// on | ||
rl.on('line', function (line) { | ||
if (onLine) onLine(line); | ||
}); | ||
rl.on('close', function () { | ||
if (onClose) onClose(); | ||
}); | ||
// on | ||
rl.on("line", function (line) { | ||
if (onLine) onLine(line); | ||
}); | ||
rl.on("close", function () { | ||
if (onClose) onClose(); | ||
}); | ||
}; | ||
@@ -329,17 +339,21 @@ | ||
* readFileLineByLineSync | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
const readFileLineByLineSync = (filePath) => { | ||
return new Promise((resolve) => { | ||
// lines | ||
let lines = []; | ||
return new Promise((resolve) => { | ||
// lines | ||
let lines = []; | ||
readFileLineByLine(filePath, line => { | ||
lines.push(line); | ||
}, () => { | ||
resolve(lines); | ||
lines = null; | ||
}); | ||
}); | ||
readFileLineByLine( | ||
filePath, | ||
(line) => { | ||
lines.push(line); | ||
}, | ||
() => { | ||
resolve(lines); | ||
lines = null; | ||
} | ||
); | ||
}); | ||
}; | ||
@@ -349,21 +363,21 @@ | ||
* writeFile | ||
* @param {*} filePath | ||
* @param {*} fileData | ||
* @param {*} filePath | ||
* @param {*} fileData | ||
* @param {*} options https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fswritefilesyncfile-data-options | ||
*/ | ||
const writeFile = (filePath, fileData, options) => { | ||
// check | ||
if (!filePath) return; | ||
// check | ||
if (!filePath) return; | ||
try { | ||
// vars | ||
fileData = fileData || ''; | ||
options = options || {}; | ||
fs.writeFileSync(filePath, fileData, options); | ||
try { | ||
// vars | ||
fileData = fileData || ""; | ||
options = options || {}; | ||
fs.writeFileSync(filePath, fileData, options); | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
@@ -373,15 +387,15 @@ | ||
* writeFileFromLines | ||
* @param {*} filePath | ||
* @param {*} lines | ||
* @param {*} filePath | ||
* @param {*} lines | ||
*/ | ||
const writeFileFromLines = (filePath, lines) => { | ||
const f = fs.createWriteStream(filePath, { | ||
flags: 'a' | ||
}); | ||
const f = fs.createWriteStream(filePath, { | ||
flags: "a", | ||
}); | ||
for (let i = 0; i < lines.length; i++) { | ||
f.write(`${lines[i]}\r\n`); | ||
} | ||
for (let i = 0; i < lines.length; i++) { | ||
f.write(`${lines[i]}\r\n`); | ||
} | ||
f.close(); | ||
f.close(); | ||
}; | ||
@@ -388,0 +402,0 @@ |
{ | ||
"name": "qiao-file", | ||
"version": "1.0.5", | ||
"description": "nodejs file tool", | ||
"author": "uikoo9 <uikoo9@qq.com>", | ||
"homepage": "https://code.insistime.com/qiao-file", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"module": "src/index.js", | ||
"sideEffets": false, | ||
"files": [ | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup --config _config.rollup.js", | ||
"eslint": "eslint . --ext .js -c _config.eslint.js", | ||
"eslintfix": "eslint . --ext .js --fix -c _config.eslint.js", | ||
"prepublish": "npm run build && npm run eslintfix" | ||
}, | ||
"gitHead": "baba1fabfbcb292ba2217ef7448fee37a6676f40" | ||
"name": "qiao-file", | ||
"version": "1.0.6", | ||
"description": "nodejs file tool", | ||
"author": "uikoo9 <uikoo9@qq.com>", | ||
"homepage": "https://code.insistime.com/qiao-file", | ||
"license": "MIT", | ||
"main": "index.js", | ||
"module": "src/index.js", | ||
"sideEffets": false, | ||
"files": [ | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/uikoo9/qiao-monorepo.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/uikoo9/qiao-monorepo/issues" | ||
}, | ||
"scripts": { | ||
"build": "rollup --config _config.rollup.js" | ||
}, | ||
"gitHead": "10e87c4bb8a4dd7bde6ea4aac9ee50aa2ecb9fc2" | ||
} |
234
README.md
# qiao-file | ||
[![npm version](https://img.shields.io/npm/v/qiao-file.svg?style=flat-square)](https://www.npmjs.org/package/qiao-file) | ||
[![npm downloads](https://img.shields.io/npm/dm/qiao-file.svg?style=flat-square)](https://npm-stat.com/charts.html?package=qiao-file) | ||
nodejs下文件相关封装 | ||
nodejs 下文件相关封装 | ||
## cmd | ||
### cp | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var folderPath = './test/'; | ||
var filePath = './qiao-file.js' | ||
var test = function () { | ||
var folderPath = "./test/"; | ||
var filePath = "./qiao-file.js"; | ||
// cp folder | ||
q.cp(folderPath, './test1'); | ||
// cp file | ||
q.cp(filePath, './1.js'); | ||
// cp folder | ||
q.cp(folderPath, "./test1"); | ||
// cp file | ||
q.cp(filePath, "./1.js"); | ||
}; | ||
@@ -29,13 +32,14 @@ | ||
### mv | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var oldPath = './test'; | ||
var newPath = './test1' | ||
var test = function () { | ||
var oldPath = "./test"; | ||
var newPath = "./test1"; | ||
var res = q.mv(oldPath, newPath); | ||
console.log(res); | ||
var res = q.mv(oldPath, newPath); | ||
console.log(res); | ||
}; | ||
@@ -47,16 +51,17 @@ | ||
### rm | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var folderPath = 'd:/test1/'; | ||
var filePath = 'd:/test.png' | ||
var test = function () { | ||
var folderPath = "d:/test1/"; | ||
var filePath = "d:/test.png"; | ||
// rm folder | ||
q.rm(folderPath); | ||
// rm file | ||
q.rm(filePath); | ||
// rm folder | ||
q.rm(folderPath); | ||
// rm file | ||
q.rm(filePath); | ||
}; | ||
@@ -68,11 +73,13 @@ | ||
## dir | ||
### lsdir | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var foldersAndFiles = q.lsdir('z:/workspaces/qiao.plugin.coder/'); | ||
console.log(foldersAndFiles); | ||
var test = function () { | ||
var foldersAndFiles = q.lsdir("z:/workspaces/qiao.plugin.coder/"); | ||
console.log(foldersAndFiles); | ||
}; | ||
@@ -84,10 +91,11 @@ | ||
### lstree | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var fileTree = q.lstree('./', ['node_modules']); | ||
console.log(JSON.stringify(fileTree)); | ||
var test = function () { | ||
var fileTree = q.lstree("./", ["node_modules"]); | ||
console.log(JSON.stringify(fileTree)); | ||
}; | ||
@@ -99,11 +107,12 @@ | ||
### mkdir | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var folder = 'd:/test1/test2/test3/test.js'; | ||
q.mkdir(folder); | ||
var test = function () { | ||
var folder = "d:/test1/test2/test3/test.js"; | ||
q.mkdir(folder); | ||
}; | ||
@@ -115,13 +124,15 @@ | ||
## file | ||
### extname | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var filePath = 'd:/test1/test2/test.js'; | ||
var s = q.extname(filePath); | ||
console.log(s); | ||
var test = function () { | ||
var filePath = "d:/test1/test2/test.js"; | ||
var s = q.extname(filePath); | ||
console.log(s); | ||
}; | ||
@@ -133,12 +144,13 @@ | ||
### readFile | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var filePath = './index.js'; | ||
var s = q.readFile(filePath); | ||
console.log(s); | ||
var test = function () { | ||
var filePath = "./index.js"; | ||
var s = q.readFile(filePath); | ||
console.log(s); | ||
}; | ||
@@ -150,12 +162,13 @@ | ||
### readFileLineByLine | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
// q | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
// run | ||
async function test(){ | ||
var filePath = './rm.js'; | ||
q.readFileLineByLine(filePath, onLine, onClose); | ||
async function test() { | ||
var filePath = "./rm.js"; | ||
q.readFileLineByLine(filePath, onLine, onClose); | ||
} | ||
@@ -165,9 +178,9 @@ test(); | ||
// on line | ||
function onLine(line){ | ||
console.log(line); | ||
function onLine(line) { | ||
console.log(line); | ||
} | ||
// on close | ||
function onClose(){ | ||
console.log('close'); | ||
function onClose() { | ||
console.log("close"); | ||
} | ||
@@ -177,17 +190,18 @@ ``` | ||
### readFileLineByLineSync | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
// q | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
// run | ||
async function test(){ | ||
var filePath = './rm.js'; | ||
var lines = await q.readFileLineByLineSync(filePath); | ||
console.log(lines); | ||
async function test() { | ||
var filePath = "./rm.js"; | ||
var lines = await q.readFileLineByLineSync(filePath); | ||
console.log(lines); | ||
// clear | ||
filePath = null; | ||
lines = null; | ||
// clear | ||
filePath = null; | ||
lines = null; | ||
} | ||
@@ -198,10 +212,11 @@ test(); | ||
### writeFile | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var filePath = './1.js'; | ||
q.writeFile(filePath, '2'); | ||
var test = function () { | ||
var filePath = "./1.js"; | ||
q.writeFile(filePath, "2"); | ||
}; | ||
@@ -213,20 +228,21 @@ | ||
### writeFileFromLines | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
// q | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
// run | ||
async function test(){ | ||
var filePath = './rm.js'; | ||
var destPath = '../dist/rm.js'; | ||
var lines = await q.readFileLineByLineSync(filePath); | ||
q.writeFileFromLines(destPath, lines); | ||
async function test() { | ||
var filePath = "./rm.js"; | ||
var destPath = "../dist/rm.js"; | ||
var lines = await q.readFileLineByLineSync(filePath); | ||
q.writeFileFromLines(destPath, lines); | ||
// clear | ||
filePath = null; | ||
destPath = null; | ||
lines = null; | ||
}; | ||
// clear | ||
filePath = null; | ||
destPath = null; | ||
lines = null; | ||
} | ||
test(); | ||
@@ -236,13 +252,15 @@ ``` | ||
## is | ||
### isExists | ||
```javascript | ||
'use strict'; | ||
"use strict"; | ||
var q = require('qiao-file'); | ||
var q = require("qiao-file"); | ||
var test = function(){ | ||
var fpath = 'z:/workspaces/qiao.plugin.coder/lib/qiao.plugin.coder.js'; | ||
var s = q.isExists(fpath); | ||
console.log(s); | ||
var test = function () { | ||
var fpath = "z:/workspaces/qiao.plugin.coder/lib/qiao.plugin.coder.js"; | ||
var s = q.isExists(fpath); | ||
console.log(s); | ||
}; | ||
@@ -254,6 +272,9 @@ | ||
## version | ||
### 0.1.5.20221008 | ||
1. 1.0.0 | ||
### 0.1.4.20220707 | ||
1. write file from lines | ||
@@ -264,2 +285,3 @@ 2. read file line by line | ||
### 0.1.3.20220420 | ||
1. lstree path name | ||
@@ -270,2 +292,3 @@ 2. mv | ||
### 0.1.2.20220419 | ||
1. es6 | ||
@@ -276,11 +299,15 @@ 2. add lstree | ||
### 0.1.1.20220417 | ||
1. add lerna | ||
### 0.1.0.20220317 | ||
1. add cp | ||
### 0.0.9.20191204 | ||
1. add funding | ||
### 0.0.8.20191011 | ||
1. exports.fs | ||
@@ -290,5 +317,7 @@ 2. exports.path | ||
### 0.0.7.20190122 | ||
1. modify md | ||
### 0.0.6.20190117 | ||
1. extname to lower case | ||
@@ -299,5 +328,7 @@ 2. add lsdir | ||
### 0.0.5.20190107 | ||
1. nodejs file tool | ||
### 0.0.4.20181127 | ||
1. modify method name | ||
@@ -307,5 +338,7 @@ 2. add .js | ||
### 0.0.3.20181122 | ||
1. npm audit | ||
### 0.0.2.20181017 | ||
1. isExists | ||
@@ -317,2 +350,3 @@ 2. extname | ||
### 0.0.1.20181016 | ||
1. init |
// fs | ||
export * as fs from 'fs'; | ||
export * as fs from "fs"; | ||
// path | ||
export * as path from 'path'; | ||
export * as path from "path"; | ||
// other | ||
export * from './lib/cmd.js'; | ||
export * from './lib/dir.js'; | ||
export * from './lib/file.js'; | ||
export * from './lib/is.js'; | ||
export * from "./lib/cmd.js"; | ||
export * from "./lib/dir.js"; | ||
export * from "./lib/file.js"; | ||
export * from "./lib/is.js"; |
// fs | ||
import fs from 'fs'; | ||
import fs from "fs"; | ||
// util | ||
import { getFoldersAndFiles } from './util.js'; | ||
import { getFoldersAndFiles } from "./util.js"; | ||
@@ -11,18 +11,18 @@ /** | ||
* @param {*} dest file or folder dest path | ||
* @returns | ||
* @returns | ||
*/ | ||
export 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; | ||
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; | ||
} | ||
}; | ||
@@ -32,14 +32,14 @@ | ||
* mv | ||
* @param {*} oldPath | ||
* @param {*} newPath | ||
* @param {*} oldPath | ||
* @param {*} newPath | ||
*/ | ||
export const mv = (oldPath, newPath) => { | ||
try{ | ||
fs.renameSync(oldPath, newPath); | ||
try { | ||
fs.renameSync(oldPath, newPath); | ||
return true; | ||
}catch(e){ | ||
console.log(e); | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
@@ -52,28 +52,30 @@ | ||
export 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; | ||
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; | ||
} | ||
}; |
// fs | ||
import fs from 'fs'; | ||
import fs from "fs"; | ||
// is | ||
import { isExists } from './is.js'; | ||
import { isExists } from "./is.js"; | ||
// util | ||
import { | ||
checkDir, | ||
getFileTree, | ||
getFoldersAndFiles | ||
} from './util.js'; | ||
import { checkDir, getFileTree, getFoldersAndFiles } from "./util.js"; | ||
/** | ||
* ls dir | ||
* dir : must end with / | ||
*/ | ||
* ls dir | ||
* dir : must end with / | ||
*/ | ||
export const lsdir = (dir) => { | ||
let folders = []; | ||
let files = []; | ||
getFoldersAndFiles(dir, folders, files); | ||
return { | ||
folders : folders, | ||
files : files | ||
}; | ||
let folders = []; | ||
let files = []; | ||
getFoldersAndFiles(dir, folders, files); | ||
return { | ||
folders: folders, | ||
files: files, | ||
}; | ||
}; | ||
@@ -32,40 +28,40 @@ | ||
* @param {*} dir must end with / | ||
* @param {*} ignores | ||
* @returns | ||
* @param {*} ignores | ||
* @returns | ||
*/ | ||
export const lstree = (dir, ignores) => { | ||
let fileTree = []; | ||
getFileTree(dir, fileTree, ignores); | ||
return fileTree; | ||
let fileTree = []; | ||
getFileTree(dir, fileTree, ignores); | ||
return fileTree; | ||
}; | ||
/** | ||
* mk dir | ||
* dir : must end with / | ||
*/ | ||
* mk dir | ||
* dir : must end with / | ||
*/ | ||
export const mkdir = (dir) => { | ||
try{ | ||
// check | ||
if(!dir || !dir.endsWith('/')) return false; | ||
try { | ||
// check | ||
if (!dir || !dir.endsWith("/")) return false; | ||
// is exists | ||
if(isExists(dir)) return true; | ||
// check dir | ||
var dirs = [dir]; | ||
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; | ||
} | ||
}; | ||
// is exists | ||
if (isExists(dir)) return true; | ||
// check dir | ||
var dirs = [dir]; | ||
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; | ||
} | ||
}; |
// fs | ||
import fs from 'fs'; | ||
import fs from "fs"; | ||
// path | ||
import path from 'path'; | ||
import path from "path"; | ||
// readline | ||
import readline from 'readline'; | ||
import readline from "readline"; | ||
@@ -15,5 +15,5 @@ /** | ||
export const extname = (filePath) => { | ||
if (!filePath) return null; | ||
if (!filePath) return null; | ||
return path.extname(filePath.toLowerCase()); | ||
return path.extname(filePath.toLowerCase()); | ||
}; | ||
@@ -23,19 +23,19 @@ | ||
* readFile | ||
* @param {*} filePath | ||
* @param {*} filePath | ||
* @param {*} options https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fsreadfilesyncpath-options | ||
*/ | ||
export const readFile = (filePath, options) => { | ||
// check | ||
if (!filePath) return; | ||
// check | ||
if (!filePath) return; | ||
try { | ||
// opt | ||
const opt = { encoding: 'utf8' }; | ||
options = options || opt; | ||
try { | ||
// opt | ||
const opt = { encoding: "utf8" }; | ||
options = options || opt; | ||
return fs.readFileSync(filePath, options); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
return fs.readFileSync(filePath, options); | ||
} catch (e) { | ||
console.log(e); | ||
return; | ||
} | ||
}; | ||
@@ -45,19 +45,19 @@ | ||
* readFileLineByLine | ||
* @param {*} filePath | ||
* @param {*} onLine | ||
* @param {*} onClose | ||
* @param {*} filePath | ||
* @param {*} onLine | ||
* @param {*} onClose | ||
*/ | ||
export const readFileLineByLine = (filePath, onLine, onClose) => { | ||
// rl | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream(filePath, { encoding: 'utf8' }) | ||
}); | ||
// rl | ||
const rl = readline.createInterface({ | ||
input: fs.createReadStream(filePath, { encoding: "utf8" }), | ||
}); | ||
// on | ||
rl.on('line', function (line) { | ||
if (onLine) onLine(line); | ||
}); | ||
rl.on('close', function () { | ||
if (onClose) onClose(); | ||
}); | ||
// on | ||
rl.on("line", function (line) { | ||
if (onLine) onLine(line); | ||
}); | ||
rl.on("close", function () { | ||
if (onClose) onClose(); | ||
}); | ||
}; | ||
@@ -67,17 +67,21 @@ | ||
* readFileLineByLineSync | ||
* @param {*} filePath | ||
* @returns | ||
* @param {*} filePath | ||
* @returns | ||
*/ | ||
export const readFileLineByLineSync = (filePath) => { | ||
return new Promise((resolve) => { | ||
// lines | ||
let lines = []; | ||
return new Promise((resolve) => { | ||
// lines | ||
let lines = []; | ||
readFileLineByLine(filePath, line => { | ||
lines.push(line); | ||
}, () => { | ||
resolve(lines); | ||
lines = null; | ||
}); | ||
}); | ||
readFileLineByLine( | ||
filePath, | ||
(line) => { | ||
lines.push(line); | ||
}, | ||
() => { | ||
resolve(lines); | ||
lines = null; | ||
} | ||
); | ||
}); | ||
}; | ||
@@ -87,21 +91,21 @@ | ||
* writeFile | ||
* @param {*} filePath | ||
* @param {*} fileData | ||
* @param {*} filePath | ||
* @param {*} fileData | ||
* @param {*} options https://nodejs.org/dist/latest-v16.x/docs/api/fs.html#fswritefilesyncfile-data-options | ||
*/ | ||
export const writeFile = (filePath, fileData, options) => { | ||
// check | ||
if (!filePath) return; | ||
// check | ||
if (!filePath) return; | ||
try { | ||
// vars | ||
fileData = fileData || ''; | ||
options = options || {}; | ||
fs.writeFileSync(filePath, fileData, options); | ||
try { | ||
// vars | ||
fileData = fileData || ""; | ||
options = options || {}; | ||
fs.writeFileSync(filePath, fileData, options); | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
return true; | ||
} catch (e) { | ||
console.log(e); | ||
return false; | ||
} | ||
}; | ||
@@ -111,15 +115,15 @@ | ||
* writeFileFromLines | ||
* @param {*} filePath | ||
* @param {*} lines | ||
* @param {*} filePath | ||
* @param {*} lines | ||
*/ | ||
export const writeFileFromLines = (filePath, lines) => { | ||
const f = fs.createWriteStream(filePath, { | ||
flags: 'a' | ||
}); | ||
const f = fs.createWriteStream(filePath, { | ||
flags: "a", | ||
}); | ||
for (let i = 0; i < lines.length; i++) { | ||
f.write(`${lines[i]}\r\n`); | ||
} | ||
for (let i = 0; i < lines.length; i++) { | ||
f.write(`${lines[i]}\r\n`); | ||
} | ||
f.close(); | ||
}; | ||
f.close(); | ||
}; |
// fs | ||
import fs from 'fs'; | ||
import fs from "fs"; | ||
@@ -9,9 +9,9 @@ /** | ||
export const isExists = (fpath) => { | ||
try{ | ||
fs.accessSync(fpath); | ||
return true; | ||
}catch(e){ | ||
return false; | ||
} | ||
}; | ||
try { | ||
fs.accessSync(fpath); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; |
// fs | ||
import fs from 'fs'; | ||
import fs from "fs"; | ||
// path | ||
import path from 'path'; | ||
import path from "path"; | ||
// is exists | ||
import { isExists } from './is.js'; | ||
import { isExists } from "./is.js"; | ||
/** | ||
* get folders and files | ||
* @param {*} fpath | ||
* @param {*} folders | ||
* @param {*} files | ||
* @param {*} fpath | ||
* @param {*} folders | ||
* @param {*} files | ||
*/ | ||
export 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 | ||
}); | ||
} | ||
}); | ||
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, | ||
}); | ||
} | ||
}); | ||
}; | ||
@@ -37,29 +37,29 @@ | ||
* get file tree | ||
* @param {*} fpath | ||
* @param {*} fileTree | ||
* @param {*} ignores | ||
* @param {*} fpath | ||
* @param {*} fileTree | ||
* @param {*} ignores | ||
*/ | ||
export const getFileTree = (fpath, fileTree, ignores) => { | ||
fs.readdirSync(fpath).forEach(function(name){ | ||
const rpath = fpath + name; | ||
if(isFileTreeIgnore(rpath, ignores)) return; | ||
fs.readdirSync(fpath).forEach(function (name) { | ||
const rpath = fpath + name; | ||
if (isFileTreeIgnore(rpath, ignores)) return; | ||
const stat = fs.statSync(rpath); | ||
if(stat.isDirectory()){ | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
info.children = []; | ||
fileTree.push(info); | ||
getFileTree(rpath + '/', info.children, ignores); | ||
}else{ | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
const stat = fs.statSync(rpath); | ||
if (stat.isDirectory()) { | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
info.children = []; | ||
fileTree.push(info); | ||
} | ||
}); | ||
fileTree.push(info); | ||
getFileTree(rpath + "/", info.children, ignores); | ||
} else { | ||
let info = {}; | ||
info.path = fpath; | ||
info.name = name; | ||
fileTree.push(info); | ||
} | ||
}); | ||
}; | ||
@@ -69,12 +69,12 @@ | ||
* check dir | ||
* @param {*} dir | ||
* @param {*} list | ||
* @param {*} dir | ||
* @param {*} list | ||
*/ | ||
export const checkDir = (dir, list) => { | ||
const pdir = path.dirname(dir); | ||
if(!isExists(pdir)){ | ||
list.push(pdir); | ||
checkDir(pdir, list); | ||
} | ||
const pdir = path.dirname(dir); | ||
if (!isExists(pdir)) { | ||
list.push(pdir); | ||
checkDir(pdir, list); | ||
} | ||
}; | ||
@@ -84,10 +84,10 @@ | ||
const isFileTreeIgnore = (rpath, ignores) => { | ||
if(!rpath || !ignores || !ignores.length) return; | ||
if (!rpath || !ignores || !ignores.length) return; | ||
let ignore = false; | ||
for(let i=0; i<ignores.length; i++){ | ||
if(rpath.indexOf(ignores[i]) > -1) ignore = true; | ||
} | ||
let ignore = false; | ||
for (let i = 0; i < ignores.length; i++) { | ||
if (rpath.indexOf(ignores[i]) > -1) ignore = true; | ||
} | ||
return ignore; | ||
}; | ||
return ignore; | ||
}; |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
671
332
20227