UNPKG

quick-create-app-cli

Version:

前端基础建设模版脚手架,快速搭建PC、HybridApp、微信小程序、微前端等项目

172 lines (156 loc) 3.33 kB
const { path, fsExtra, chalk, ora, inquirer, handlebars, download, execa, git } = require('./dependencies') module.exports = { /** *相对地址 * @param {*} dir * @returns */ resolvePath: function (dir) { return path.resolve(__dirname, dir) }, /** * 判断路径是否存在 * @param {*} path */ pathExists: function (path) { return fsExtra.pathExists(path) }, /** * 文件复制 * @param {*} oldPath * @param {*} newPath * @returns */ copyFileToPath: async function (oldPath, newPath) { try { await fsExtra.copy(oldPath, newPath) } catch (error) { console.error(error) return Promise.reject(error) } }, /** * 文件删除 * @param {*} path * @returns */ removeFile: async function (path) { try { await fsExtra.remove(path) } catch (error) { console.error(error) return Promise.reject(error) } }, /** * 输出json文件 * @param {*} data * @param {*} path */ outputJson: async function (path, data) { try { await fsExtra.outputJson(path, data) } catch (err) { console.error(error) return Promise.reject(error) } }, /** * 模版替换 * @param {*} meta 配置元 * @param {*} packagePath 被替换文件路径 */ replaceTemplates: async function (meta, packagePath) { try { //读取文件 const packageJson = await fsExtra.readFile(packagePath, 'utf8') //替换文件 const filesResult = await handlebars.compile(packageJson)(meta) //输出文件 await fsExtra.outputFile(packagePath, filesResult) } catch (error) { console.error(error) return Promise.reject(error) } }, /** * 创建加载动画 * @param {*} param */ createLoadSpinner: function (text) { let downloadSpinner = ora(chalk.cyan(text)) downloadSpinner.start() return downloadSpinner }, /** * 下载任务 * @param {*} url * @param {*} path(本地地址) * @returns */ downloadTask: async function (url, path) { try { await download(url, path, { extract: true }) } catch (error) { console.error(error) return Promise.reject(error) } }, /** * 克隆远程仓库 * @param {*} url * @param {*} path(本地地址) * @returns */ cloneRepository: async function (url, path, options) { try { await git.clone(url, path, options) } catch (error) { console.error(error) return Promise.reject(error) } }, /** * inquirer 询问用户指令 * @param {*} config * @returns */ askCommand: function (config) { return inquirer.prompt(config) }, /** * 执行终端命令 * @param {*} command * @param {*} cwd */ executeCommand: function (command, options, cwd) { return new Promise((resolve, reject) => { let child = execa(command, options, { cwd, stdio: ['inherit', 'pipe', 'inherit'] }) child.stdout.on('data', (buffer) => { process.stdout.write(buffer) }) child.on('close', (code) => { if (code !== 0) { reject(new Error(`command failed: ${command}`)) return } resolve() }) }) } }