update publish process

This commit is contained in:
jiaming743 2019-07-09 11:44:10 +08:00
parent 132a817933
commit 9699837d0e
4 changed files with 19 additions and 356 deletions

14
package-lock.json generated Normal file
View File

@ -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
}
}
}

View File

@ -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"
}
}

View File

@ -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')

View File

@ -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
}