UNPKG

book-cliiii

Version:

Command line interface for front end project

130 lines (112 loc) 3.66 kB
#!/usr/bin/env node /* eslint-disable global-require */ // const fs = require('fs'); const path = require('path'); // const debug = require('debug')('init:book'); // const validateProjectName = require('validate-npm-package-name'); // 校验有效 npm包名 // const semver = require('semver'); // npm的语义版本器 // const NpmApi = require('npm-api'); const execa = require('execa'); const inquirer = require('inquirer'); // const exit = require('./utils/exit'); const chalk = require('chalk'); const { stopSpinner } = require('./utils/spinner'); // const checkNpmPkg = require('./utils/checkNpmPkg'); const Generator = require('./utils/generator'); const { error } = require('./utils/logger'); const Install = require('./utils/install'); const repository = new Install(); // const REPOSITORY_FOLDER = '.book-repository'; // const npm = new NpmApi(); async function init(tplName, projectName = '', options = {}) { // package 默认前缀 book-cli-project // tplName = `book-cli-project-${tplName}`; // 查询tpl包存在 // TODO // const npmRepo = npm.repo(tplName); // let packages; // try { // packages = await npmRepo.package('all'); // if (packages.error) { // throw new Error(packages.error); // } // } catch (err) { // stopSpinner(false); // debug(`Could not find npm package for ${tplName}`, err); // // return false; // } // 安装模板 - 返回模板安装绝对路径地址 const generatorPath = repository.requireModule(tplName, ''); // 初始化模板 generator const generator = new Generator(projectName); // 读取配置 // const prompts = require('./mock/prompts'); // eslint-disable-next-line import/no-dynamic-require const prompts = require(`${generatorPath}/prompts`); let answer; try { answer = await inquirer.prompt(prompts); } catch (err) { console.log(err); } // 读取模板 generator 文件并执行 // require('./mock/generator/index')(generator, { ...answer, projectName }); // eslint-disable-next-line import/no-dynamic-require require(`${generatorPath}/generator/index`)(generator, { ...answer, projectName, }); let result = { data: true, msg: '创建成功', }; // 执行 generate try { await generator.generate(); } catch (err) { console.log(`${chalk.res(result.msg)}!`); result = { data: false, msg: '创建失败!', }; } console.log(`${chalk.yellow(result.msg)}!`); // 询问是否安装依赖包 const installPrompts = require('./installPrompts'); let installAnswer; try { installAnswer = await inquirer.prompt(installPrompts); } catch (err) { console.log(`${chalk.red(err)}!`); stopSpinner(false); } // 安装依赖包 if (installAnswer.install) { console.log(chalk.yellow('稍等片刻,项目安装中~~~')); execa.sync('npm', [ 'config', 'set', '@jd:registry=http://registry.m.jd.com', ]); execa.sync('npm', [ 'config', 'set', '@jdpop:registry=http://registry.m.jd.com', ]); execa.sync('npm', [ 'config', 'set', '@jdbk:registry=http://registry.m.jd.com', ]); execa.sync('npm', ['install', '--prefix', path.resolve(projectName)]); console.log(chalk.green('安装完成!撒花~')); console.log(`${chalk.yellow('You can chose command start your coding!')}\n` + `cd ${projectName}\n` + 'npm run serve\n'); } } module.exports = (...args) => init(...args).catch((err) => { stopSpinner(false); // do not persist error(err); if (!process.env.BOOK_CLI_TEST) { process.exit(1); } });