❝没有脚手架,我只能通过copy拷贝代码来完成,这样繁琐又机械化的操作浪费大量时间,而且还可能在拷贝过程中,因为某个细节出错,导致项目出错,排查问题又耗时。或许你可能会想,我们不是可以用vue或者react官方的脚手架来生成模版吗?是,但是这种方式创建的模版不一定符合你内部结构化标准❞
4.可执行shell命令的工具: child_process
#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const chalk = require('chalk'); const commander = require('commander'); const inquirer = require('inquirer'); const checkDire = require('./utils/checkDire.js'); const { exec } = require('child_process'); const { version } = require('../package.json'); const { promptTypeList } = require('./config'); //堆代码 duidaima.com //version 版本号 commander.version(version, '-v, --version') .command('init <projectName>') .alias("i") .description("输入项目名称,初始化项目模版") .action(async (projectName,cmd) => { await checkDire(path.join(process.cwd(),projectName),projectName); // 检测创建项目文件夹是否存在 inquirer.prompt(promptTypeList).then(result => { const {url, gitName, val} = result.type; console.log("您选择的模版类型信息如下:" + val); console.log('项目初始化拷贝获取中...'); if(!url){ console.log(chalk.red(`${val} 该类型暂不支持...`)); process.exit(1); } exec('git clone ' + url, function (error, stdout, stderr) { if (error !== null) { console.log(chalk.red( `clone fail,${error}` )); return; } fs.rename(gitName, projectName, (err)=>{ if (err) { exec('rm -rf '+gitName, function (err, out) {}); console.log(chalk.red(`The ${projectName} project template already exist`)); } else { console.log(chalk.green(`The ${projectName} project template successfully create(项目模版创建成功)`)); } }); }); }) }); commander.parse(process.argv);这里定义的是npm包命令bin的入口文件
// utils/checkDire.js const fs = require('fs'); const chalk = require('chalk'); const path = require('path'); module.exports = function (dir,name) { let isExists = fs.existsSync(dir); if (isExists) { console.log(chalk.red( `The ${name} project already exists in directory. Please try to use another projectName` )); process.exit(1); }配置文件
// config/index.js 配置文件 /* @dest: 使用配置文件 @Author: tree */ module.exports = { promptTypeList:[{ type: 'list', message: '请选择拉取的模版类型:', name: 'type', choices: [{ name: 'mobile', value: { url: '', gitName: 'vue-web-template', val:'移动端模版' } },{ name: 'pc', value: { url: 'https://github.com/littleTreeme/vue-web-template.git', gitName: 'vue-web-template', val:'PC端模版' } }] }], };3.2 工具详解
const inquirer = require('inquirer'); const promptList = [ type: 'list', message: '请选择拉取的模版类型:', name: 'type', choices: ['mobile','pc'] ]; inquirer.prompt(promptList).then(type => { console.log(type); // 返回 mobile 或 pc })场景如下
const commander = require('commander'); commander.version(version, '-v, --version') .command('init <projectName>') .alias("i") .description("输入项目名称,初始化项目模版") .action(async (projectName,cmd) => { console.log(projectName,'你输入的<projectName>') }) commander.parse(process.argv); // command – 定义命令行指令,后面可跟上一个name,用空格隔开 // alias – 定义一个更短的命令行指令 // description – 描述,它会在help里面展示 // option – 定义参数 // action – 注册一个callback函数 // parse - 解析命令行chalk
const chalk = require('chalk'); // 报错日志用红色来显示 chalk.red(`The project already exists in directory. Please try to use another projectName`)); // 成功日志用绿色来显示 chalk.green(`The project template successfully create(项目模版创建成功)`);3.3 如何使用