为nuxt项目写一个面包屑cli工具实现自动生成页面(2)

lib // 存命令行文件 |-- bcg.js template // 存模版 |-- breadcrumb // 面包屑配置文件与组件,将生成在项目 @/components 中 |-- breadcrumb.config.json |-- index.vue |-- braadcrumb.js // vuex 相关文件,将生成在项目 @/store 中 |-- new-page.vue // 新文件模版,将生成在命令行输入的新路径中 |-- route.js // 路由前置守卫配置文件,将生成在 @/plugins 中 test // 单元测试相关文件

node 支持命令行,只需在 package.json 的 bin 字段中关联命令行执行文件

// 执行 bcg 命令时,就会执行 lib/bcg.js 的代码 { "bin": { "bcg": "lib/bcg.js" } }

实现命令

实现一个 init 命令,生成相关面包屑文件(面包屑组件、 json配置文件、 前置守卫plugin、 面包屑store)

bcg init

实现一个 new 命令生成文件,默认基础路径是 src/pages ,带一个 -b 选项,可用来修改基础路径

bcg new <file-path> -b <base-path>

具体代码如下

#!/usr/bin/env node const path = require('path') const fs = require('fs-extra') const boxen = require('boxen') const inquirer = require('inquirer') const commander = require('commander') const Handlebars = require('handlebars') const { createPathArr, log, errorLog, successLog, infoLog, copyFile } = require('./utils') const VUE_SUFFIX = '.vue' const source = { VUE_PAGE_PATH: path.resolve(__dirname, '../template/new-page.vue'), BREADCRUMB_COMPONENT_PATH: path.resolve(__dirname, '../template/breadcrumb'), PLUGIN_PATH: path.resolve(__dirname, '../template/route.js'), STORE_PATH: path.resolve(__dirname, '../template/breadcrumb.js') } const target = { BREADCRUMB_COMPONENT_PATH: 'src/components/breadcrumb', BREADCRUMB_JSON_PATH: 'src/components/breadcrumb/breadcrumb.config.json', PLUGIN_PATH: 'src/plugins/route.js', STORE_PATH: 'src/store/breadcrumb.js' } function initBreadCrumbs() { try { copyFile(source.BREADCRUMB_COMPONENT_PATH, target.BREADCRUMB_COMPONENT_PATH) copyFile(source.PLUGIN_PATH, target.PLUGIN_PATH) copyFile(source.STORE_PATH, target.STORE_PATH) } catch (err) { throw err } } function generateVueFile(newPagePath) { try { if (fs.existsSync(newPagePath)) { log(errorLog(`${newPagePath} 已存在`)) return } const fileName = path.basename(newPagePath).replace(VUE_SUFFIX, '') const vuePage = fs.readFileSync(source.VUE_PAGE_PATH, 'utf8') const template = Handlebars.compile(vuePage) const result = template({ filename: fileName }) fs.outputFileSync(newPagePath, result) log(successLog('\nvue页面生成成功咯\n')) } catch (err) { throw err } } function updateConfiguration(filePath, { clickable, isShow } = {}) { try { if (!fs.existsSync(target.BREADCRUMB_JSON_PATH)) { log(errorLog('面包屑配置文件不存在, 配置失败咯, 可通过 bcg init 生成相关文件')) return } let pathArr = createPathArr(filePath) const configurationArr = fs.readJsonSync(target.BREADCRUMB_JSON_PATH) // 如果已经有配置就过滤掉 pathArr = pathArr.filter(pathItem => !configurationArr.some(configurationItem => configurationItem.path === pathItem)) const questions = pathArr.map(pathItem => { return { type: 'input', name: pathItem, message: `请输入 ${pathItem} 的面包屑显示名称`, default: pathItem } }) inquirer.prompt(questions).then(answers => { const pathArrLastIdx = pathArr.length - 1 pathArr.forEach((pathItem, index) => { configurationArr.push({ clickable: index === pathArrLastIdx ? clickable : false, isShow: index === pathArrLastIdx ? isShow : true, name: answers[pathItem], path: pathItem }) }) fs.writeJsonSync(target.BREADCRUMB_JSON_PATH, configurationArr, { spaces: 2 }) log(successLog('\n生成面包屑配置成功咯')) }) } catch (err) { log(errorLog('生成面包屑配置失败咯')) throw err } } function generating(newPagePath, filePath) { inquirer.prompt([ { type: 'confirm', name: 'clickable', message: '是否可点击跳转? (默认 yes)', default: true }, { type: 'confirm', name: 'isShow', message: '是否展示面包屑? (默认 yes)', default: true }, { type: 'confirm', name: 'onlyConfig', message: '是否仅生成配置而不生成文件? (默认 no)', default: false } ]).then(({ clickable, isShow, onlyConfig }) => { if (onlyConfig) { updateConfiguration(filePath, { clickable, isShow }) return } generateVueFile(newPagePath) updateConfiguration(filePath, { clickable, isShow }) }) } const program = new commander.Command() program .command('init') .description('初始化面包屑') .action(initBreadCrumbs) program .version('0.1.0') .command('new <file-path>') .description('生成页面并配置面包屑,默认基础路径为 src/pages,可通过 -b 修改') .option('-b, --basePath <base-path>', '修改基础路径 (不要以 / 开头)') .action((filePath, opts) => { filePath = filePath.endsWith(VUE_SUFFIX) ? filePath : `${filePath}${VUE_SUFFIX}` const basePath = opts.basePath || 'src/pages' const newPagePath = path.join(basePath, filePath) log( infoLog( boxen(`即将配置 ${newPagePath}`, { padding: 1, margin: 1, borderStyle: 'round' }) ) ) generating(newPagePath, filePath) }) program.parse(process.argv) if (!process.argv.slice(2)[0]) { program.help() }

发布 npm

开发完成后,发布到 npm,具体方法就不细说了,发布后全局安装就能愉快的使用咯!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/6863ad72d892f00ab9bd6c9192ed0b11.html