From bfc7cc7c574bedecaf7a7aa2b322e01c471a3b77 Mon Sep 17 00:00:00 2001 From: jiaming743 <743192023@qq.com> Date: Wed, 3 Jul 2019 18:34:58 +0800 Subject: [PATCH] for prepublish --- publish.js | 3 + publish/plugin/fs.js | 293 ++++++++++++++++++++++++++++++++++++++++ publish/plugin/print.js | 21 +++ 3 files changed, 317 insertions(+) create mode 100644 publish.js create mode 100644 publish/plugin/fs.js create mode 100644 publish/plugin/print.js diff --git a/publish.js b/publish.js new file mode 100644 index 0000000..4171859 --- /dev/null +++ b/publish.js @@ -0,0 +1,3 @@ +const { start: step1 } = require('./publish/copyAndAbstractLess') + +step1() diff --git a/publish/plugin/fs.js b/publish/plugin/fs.js new file mode 100644 index 0000000..aa15bcb --- /dev/null +++ b/publish/plugin/fs.js @@ -0,0 +1,293 @@ +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.warn(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.warn('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.warn('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 +} + +module.exports = { + readDir, + stat, + mkdir, + clearDir, + emptyDir, + unlinkDirFileByExtname, + copyDir, + fileForEach +} \ No newline at end of file diff --git a/publish/plugin/print.js b/publish/plugin/print.js new file mode 100644 index 0000000..953704e --- /dev/null +++ b/publish/plugin/print.js @@ -0,0 +1,21 @@ +const print = { + log (info) { + console.log(info) + }, + warn (info) { + console.log('\033[31;33m' + info) + }, + error (info) { + console.log('\033[31;30m' + info) + }, + tip (info) { + console.log('\033[40;32m' + info) + }, + success (info) { + console.log('\033[42;30m' + info) + } +} + +module.exports = { + print +} \ No newline at end of file