1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #!/usr/bin/env node
const program = require('commander') const path = require('path') const vfs = require('vinyl-fs') const chalk = require('chalk') const fs = require('fs')
function pathMapObj(type, param) { return { 'index': { input: './demo/index.vue', output: path.resolve(`./src/entries/${type}/pages/${param}`) }, 'actions': { input: './demo/action/*', output: path.resolve(`./src/entries/${type}/store/${param}`) }, 'api': { input: './demo/api.js', output: path.resolve(`./src/api/${type}`) } } }
function readAndWrite(input, output, type, needRename, moduleName) { vfs.src([input]) .pipe(vfs.dest(output, {})) .on('end', function () { console.log(chalk.green(`initializing ${type} file finished......`)) if (needRename) { fs.renameSync(`${output}/api.js`, `${output}/${moduleName}.js`) } }) .resume() } program.version('1.0.0') .option('-t --teacher [name]', 'init the teacher component') .option('-s --student [name]', 'init the student component') .option('-m --manager [name]', 'init the manager component') .parse(process.argv) let param = program.teacher || program.student || program.manager if (param) { let type = null; if (program.teacher) { type = 'teacher' } else if (program.student) { type = 'student' } else if (program.manager) { type = 'manager' } if (param.toString() === 'true') { console.log(chalk.red('格式错误!缺少组件名参数')) return } console.log(`The ${chalk.blue(param)} component will be inited on ${chalk.yellow(__dirname)}`) console.log(chalk.yellow('initializing specified component......'))\
readAndWrite(pathMapObj(type, param).index.input, pathMapObj(type, param).index.output, 'index') readAndWrite(pathMapObj(type, param).actions.input, pathMapObj(type, param).actions.output, 'actions') readAndWrite(pathMapObj(type, param).api.input, pathMapObj(type, param).api.output, 'api', true, param)
} else { console.log(chalk.red('格式错误!正确命令举例:node ./bin/init-project -t component-name')) }
|