diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2c86182 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,14 @@ +{ + "name": "@jiaminghi/data-view", + "version": "2.3.2", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@jiaminghi/fs": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/@jiaminghi/fs/-/fs-0.0.0.tgz", + "integrity": "sha512-hCVpiw5IBBAB2H73gKDxd0Xp5e7cnpqdpgLnrkXgVK29aIoowVUS1qklNP8kPohAxz6967aGp7yW+N4XQtrSWA==", + "dev": true + } + } +} diff --git a/package.json b/package.json index 1f66155..e6003e3 100644 --- a/package.json +++ b/package.json @@ -25,5 +25,8 @@ "dependencies": { "@jiaminghi/charts": "*" }, - "homepage": "https://github.com/jiaming743/DataV#readme" + "homepage": "https://github.com/jiaming743/DataV#readme", + "devDependencies": { + "@jiaminghi/fs": "0.0.1" + } } diff --git a/publish/index.js b/publish/index.js index 6e332a0..1716a91 100644 --- a/publish/index.js +++ b/publish/index.js @@ -1,4 +1,4 @@ -const { copyDir, fileForEach, readFile, writeFile, unlinkDirFileByExtname, dirForEach } = require('./plugin/fs') +const { copyDir, fileForEach, readFile, writeFile, unlinkDirFileByExtname, dirForEach } = require('@jiaminghi/fs') const print = require('./plugin/print') const path = require('path') const doExec = require('./plugin/exec') diff --git a/publish/plugin/fs.js b/publish/plugin/fs.js deleted file mode 100644 index b52794e..0000000 --- a/publish/plugin/fs.js +++ /dev/null @@ -1,354 +0,0 @@ -const fs = require('fs') -const path = require('path') - -function readDir (src) { - return new Promise(resolve => { - fs.readdir(src, (err, paths) => { - if (err) { - console.error(err) - - resolve(false) - } - - resolve(paths) - }) - }) -} - -function stat (src) { - return new Promise(resolve => { - fs.stat(src, (err, stats) => { - if (err) { - console.error(err) - - resolve(false) - } - - resolve(stats) - }) - }) -} - -function mkdir (src) { - return new Promise(resolve => { - fs.mkdir(src, err => { - if (err) { - console.error(err) - - resolve(false) - } - - resolve(true) - }) - }) -} - -function access (src, mode = fs.constants.F_OK) { - return new Promise(resolve => { - fs.access(src, mode, err => { - if (err) { - resolve(false) - - return - } - - resolve(true) - }) - }) -} - -function unlink (src) { - return new Promise(resolve => { - fs.unlink(src, err => { - if (err) { - console.error(err) - - resolve(false) - } - - resolve(true) - }) - }) -} - -function rmDir (src) { - return new Promise(resolve => { - fs.rmdir(src, err => { - if (err) { - resolve(false) - } else { - resolve(true) - } - }) - }) -} - -async function clearDir (src) { - const isExists = await access(src) - - if (!isExists) { - await mkdir(src) - - return true - } - - return await emptyDir(src) -} - -async function emptyDir (src) { - const paths = await readDir(src) - if (!paths) { - console.error('Exception in emptyDir: paths!') - - return false - } - - for (let i = 0; i < paths.length; i++) { - const fullSrc = src + '/' + paths[i] - const stats = await stat(fullSrc) - - if (!stats) { - console.error('Exception in emptyDir: stats!') - - return false - } - - if (stats.isFile()) { - const isUnlink = await unlink(fullSrc) - - if (!isUnlink) { - console.error('Exception in emptyDir: isUnlink!') - - return false - } - } else if (stats.isDirectory()) { - const isEmpty = await emptyDir(fullSrc) - - if (!isEmpty) { - console.error('Exception in emptyDir: isEmpty!') - - return false - } - - if (!await rmDir(fullSrc)) { - console.error('Exception in emptyDir: rmDir!') - - return false - } - } - } - - return true -} - -async function unlinkDirFileByExtname (src, extnames = []) { - const paths = await readDir(src) - - if (!paths) { - console.error('Exception in unlinkDirFileByExtname: paths!') - - return false - } - - for (let i = 0; i < paths.length; i++) { - const fullSrc = src + '/' + paths[i] - const stats = await stat(fullSrc) - - if (!stats) { - console.error('Exception in unlinkDirFileByExtname: stats!') - - return false - } - - if (stats.isFile()) { - const cxtname = path.extname(fullSrc) - if (extnames.findIndex(name => name === cxtname) === -1) continue - - const isUnlink = await unlink(fullSrc) - if (!isUnlink) { - console.error('Exception in unlinkDirFileByExtname: isUnlink!') - - return false - } - } else if (stats.isDirectory()) { - const recursive = await unlinkDirFileByExtname(fullSrc, extnames) - - if (!recursive) { - console.error('Exception in unlinkDirFileByExtname: recursive!') - - return false - } - } - } - - return true -} - -async function copyDir (src, target) { - if (!src || !target) { - console.error('copyDir missing parameters!') - - return false - } - - const isClear = await clearDir(target) - - if (!isClear) { - console.error('Exception in copyDir: isClear!') - - return false - } - - const paths = await readDir(src) - if (!paths) { - console.error('Exception in copyDir: paths!') - - return false - } - - for (let i = 0; i < paths.length; i++) { - const fullSrc = src + '/' + paths[i] - const fullTarget = target + '/' + paths[i] - const stats = await stat(fullSrc) - - if (!stats) { - console.error('Exception in copyDir: stats!') - - return false - } - - if (stats.isFile()) { - fs.createReadStream(fullSrc).pipe(fs.createWriteStream(fullTarget)) - } else if (stats.isDirectory()) { - const isMkdir = await mkdir(fullTarget) - - if (!isMkdir) { - console.error('Exception in copyDir: isMkdir!') - - return false - } - - const isCopy = await copyDir(fullSrc, fullTarget) - - if (!isCopy) { - console.error('Exception in copyDir: isCopy!') - - return false - } - } - } - - return true -} - -async function fileForEach (src, callback) { - if (!src || !callback) { - console.error('fileForEach missing parameters!') - - return false - } - - const paths = await readDir(src) - if (!paths) { - console.error('Exception in fileForEach: paths!') - - return false - } - - for (let i = 0; i < paths.length; i++) { - const fullSrc = src + '/' + paths[i] - const stats = await stat(fullSrc) - - if (!stats) { - console.error('Exception in fileForEach: stats!') - - return false - } - - if (stats.isFile()) { - await callback(fullSrc) - } else if (stats.isDirectory()) { - const recursive = await fileForEach(fullSrc, callback) - - if (!recursive) { - console.error('Exception in fileForEach: recursive!') - - return false - } - } - } - - return true -} - -async function readFile (src, encoding = 'utf8') { - return new Promise(resolve => { - fs.readFile(src, encoding, (err, data) => { - if (err) { - console.error(err) - - resolve(false) - } else { - resolve(data) - } - }) - }) -} - -async function writeFile (src, string, encoding = 'utf8') { - return new Promise(resolve => { - fs.writeFile(src, string, encoding, err => { - if (err) { - console.error(err) - - resolve(false) - } else { - resolve(true) - } - }) - }) -} - -async function dirForEach (src, callback) { - if (!src || !callback) { - console.error('dirForEach missing parameters!') - - return false - } - - const paths = await readDir(src) - if (!paths) { - console.error('Exception in dirForEach: paths!') - - return false - } - - for (let i = 0; i < paths.length; i++) { - const fullSrc = src + '/' + paths[i] - const stats = await stat(fullSrc) - - if (!stats) { - console.error('Exception in dirForEach: stats!') - - return false - } - - if (stats.isDirectory()) await callback(fullSrc) - } - - return true -} - -module.exports = { - readDir, - stat, - mkdir, - clearDir, - emptyDir, - unlinkDirFileByExtname, - copyDir, - fileForEach, - readFile, - writeFile, - dirForEach -} \ No newline at end of file