123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- const fs = require('fs-extra')
- // eslint-disable-next-line import/no-extraneous-dependencies
- const path = require('path')
- const inquirer = require('inquirer')
- const ejs = require('ejs')
- const chalk = require('chalk')
- const yaml = require('js-yaml')
- const dirTree = require('directory-tree')
- const validatePName = require('validate-npm-package-name')
- const semver = require('semver')
- // const came = require('./lib/util')
- const came = str => `${str}`.replace(/-\D/g, match => match.charAt(1).toUpperCase())
- const questions = []
- questions.push({
- type: 'input',
- name: 'name',
- message: '请输入插件名, 多个单词以 "-" 连接, 如: user-permission\n',
- validate(value) {
- const done = this.async()
- setTimeout(() => {
- if (!value) {
- done('不可为空')
- return
- }
- const regex = /^[a-z][a-z0-9]*(-[a-z][a-z0-9]*)*/
- if (!regex.test(value)) {
- done('检测到格式错误, 多个单词以 "-" 连接, 如: user-permission')
- return
- }
- const filePath = path.resolve(__dirname, `../src/plugin/${value}`)
- if (fs.existsSync(filePath)) {
- done('项目中已存在该插件, 请更换其他插件名')
- return
- }
- const v = validatePName(`lc-plugin-${value}`)
- if (!v.validForNewPackages) {
- done(v.errors.join(', '))
- return
- }
- done(null, true)
- })
- },
- })
- questions.push({
- type: 'input',
- name: 'title',
- message: '请输入插件标题, 7个字内\n',
- default: '插件标题',
- })
- questions.push({
- type: 'input',
- name: 'version',
- message: '请输入版本号\n',
- default: '1.0.0',
- validate(value) {
- const done = this.async()
- if (!semver.valid(value)) {
- done('版本号不符合规范')
- return
- }
- done(null, true)
- },
- })
- questions.push({
- type: 'input',
- name: 'description',
- message: '简述插件处理的业务场景\n',
- default: '',
- })
- questions.push({
- type: 'input',
- name: 'author',
- message: '请输入作者\n',
- default: '',
- })
- const cachePath = path.resolve(__dirname, './.cache')
- const cachePluginPath = path.resolve(__dirname, './.cache/plugin')
- const pluginTmpPath = path.resolve(__dirname, './template/plugin')
- const pluginViewsPath = path.resolve(__dirname, './template/plugin/view')
- const pluginStrPos = __dirname.length + '/template/'.length
- const pluginsPath = path.resolve(__dirname, '../src/plugin')
- // 检测是否有插件文件夹
- if (!fs.existsSync(pluginsPath)) {
- fs.mkdirSync(pluginsPath)
- }
- inquirer
- .prompt(questions)
- .then(answers => {
- const result = answers
- result.camelCaseName = came(result.name)
- return result
- })
- .then(answers => {
- const config = { ...answers }
- // 创建缓存文件夹 .cache
- if (!fs.existsSync(cachePath)) {
- fs.mkdirSync(cachePath)
- }
- // 清空 plugin 文件夹
- if (fs.existsSync(cachePluginPath)) {
- fs.removeSync(cachePluginPath)
- }
- fs.mkdirSync(cachePluginPath)
- dirTree(pluginTmpPath, {}, item => {
- // 忽略隐藏文件
- if (item.extension === '' || item.name[0] === '.') {
- return
- }
- // 处理模板文件
- if (item.extension === '.ejs') {
- const template = fs.readFileSync(item.path, 'utf8')
- const fileConfig = { ...config }
- // 舞台 view 文件配置处理
- if (item.path.slice(pluginStrPos).split(path.sep)[1] === 'view' && item.name.slice(-8) === '.vue.ejs') {
- const viewConfig = {}
- viewConfig.icon = 'iconfont icon-demo'
- viewConfig.name = fileConfig.camelCaseName + item.name.slice(0, -8)
- viewConfig.route = path
- .join(config.name, path.relative(pluginViewsPath, item.path))
- .split(path.sep)
- .join('/')
- viewConfig.route = `/${viewConfig.route.slice(0, -8)}`
- viewConfig.order = null
- viewConfig.inNav = true
- viewConfig.title = '舞台页'
- viewConfig.type = 'view'
- viewConfig.auths = {
- role: null,
- permission: null,
- }
- viewConfig.needLogin = true
- fileConfig.configYml = yaml.safeDump(viewConfig)
- }
- const result = ejs.render(template, fileConfig)
- const targetPath1 = path.resolve(cachePluginPath, path.relative(pluginTmpPath, item.path).slice(0, -4))
- fs.outputFileSync(targetPath1, result)
- return
- }
- // 拷贝其他文件
- const targetPath1 = path.resolve(cachePluginPath, path.relative(pluginTmpPath, item.path))
- fs.copySync(item.path, targetPath1)
- })
- return config
- })
- .then(answers => {
- // 复制 .cache 到 plugin
- const sourcePath = path.resolve(__dirname, './.cache/plugin')
- const targetPath = path.resolve(__dirname, `../src/plugin/${answers.name}`)
- fs.copySync(sourcePath, targetPath)
- console.log(chalk.green(`创建插件 ${answers.name}: ${targetPath}`))
- // eslint-disable-next-line
- })
- .then(() => {
- // eslint-disable-next-line
- require('./plugin-get-config.js')
- // eslint-disable-next-line
- })
- .catch(err => {
- // eslint-disable-next-line
- console.log(chalk.red('创建插件失败'))
- console.error(err)
- process.exit(1)
- })
|