UNPKG

yyy-cli

Version:

development theme command line tool

122 lines (107 loc) 3.8 kB
const clear = require("clear"), chalk = require("chalk"), figlet = require("figlet"), log = console.log, fs = require("fs"), download = require('download-git-repo'); decompress = require("decompress"), { spawn } = require("child_process"), read = require("readline"), TEMP_DIR = "./dev-temp"; let [timeCounter, timer] = [0, null]; exports.log = log; exports.clear = clear; exports.chalk = chalk; exports.showInfo = (msg) => { clear(); makeInfo(msg); }; const makeInfo = (msg) => { log(chalk.redBright(figlet.textSync(msg, { horizontalLayout: "full" }))); }; exports.successInfo = makeInfo; /* 下载制定GitHub zip文件 */ const rewriteFile = (path, fileName, reg, value) => { return new Promise(async (resolve, reject) => { let sourceFile = `${path}/source-${fileName}`, distFile = `${path}/${fileName}`; await fs.copyFileSync(distFile, sourceFile); await fs.writeFileSync(distFile, ""); read.createInterface({ input: fs.createReadStream(sourceFile), output: process.stdout, terminal: false, }) .on("line", (val) => { if (val.indexOf("<title>") > -1) { val = val.replace(reg, (a, b) => a.replace(b, value)); } fs.appendFileSync(distFile, val + "\n"); }) .on("close", async () => { await fs.unlinkSync(sourceFile); resolve(); }); }); }; exports.downloadZipFile = async (config) => { // if (!fs.existsSync(TEMP_DIR)) await fs.mkdirSync(TEMP_DIR); // timerFunc("start", "down develop package ... ..."); // let path = `http://github.com/JesonStone/development-theme/raw/master/theme/${config.package}.zip#master`; // download(path, TEMP_DIR, { clone: true }, err => { // log(err ? err : `Successfully created project ${TEMP_DIR}`); // }); // 拉取项目仓库文件 await new Promise((resolve, reject) => { let path=`https://github.com:JesonStone/development-theme/raw/master/theme/${config.package}.zip#master`; download(path, TEMP_DIR, { clone: true }, err => { if (err) { return reject(err); } console.log(`Successfully created project ${TEMP_DIR}`) resolve(); }); }) }; /* 超时方法 */ let timerFunc = (type, str) => { if (type === "start") { type = "loading"; timeCounter = 0; } if (type === "loading") { timer = setTimeout(() => { timeCounter += 1000; log(chalk(str) + chalk(`${timeCounter / 1000}s`)); timerFunc(type, str); }, 1000); } else { if (timer) clearTimeout(timer); timer = null; } }; /* 删除临时目录文件 */ let removeDir = (path) => { return new Promise((resolve, reject) => { let files = []; /* 如果存在文件,读取文件 */ if (fs.existsSync(path)) { files = fs.readdirSync(path); /* 遍历所有文件 */ files.forEach(function (file, index) { var curPath = path + "/" + file; /* 如果是文件夹,则递归删除,否则直接删除文件 */ if (fs.statSync(curPath).isDirectory()) { removeDir(curPath); } else { fs.unlinkSync(curPath); } }); /* 删除主目录 */ fs.rmdirSync(path); } setTimeout(() => { resolve(true); }); }); };