|
| 1 | +// index.js` |
| 2 | +const chalk = require('chalk') |
| 3 | +const path = require('path') |
| 4 | +const fs = require('fs') |
| 5 | +const resolve = (...file) => path.resolve(__dirname, ...file) |
| 6 | +const log = message => console.log(chalk.green(`${message}`)) |
| 7 | +const successLog = message => console.log(chalk.blue(`${message}`)) |
| 8 | +const errorLog = error => console.log(chalk.red(`${error}`)) |
| 9 | +const { |
| 10 | + vueTemplate |
| 11 | +} = require('./template') |
| 12 | + |
| 13 | +const generateFile = (path, data) => { |
| 14 | + if (fs.existsSync(path)) { |
| 15 | + errorLog(`${path}文件已存在`) |
| 16 | + return |
| 17 | + } |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + fs.writeFile(path, data, 'utf8', err => { |
| 20 | + if (err) { |
| 21 | + errorLog(err.message) |
| 22 | + reject(err) |
| 23 | + } else { |
| 24 | + resolve(true) |
| 25 | + } |
| 26 | + }) |
| 27 | + }) |
| 28 | +} |
| 29 | +log('请输入要生成的组件名称、如需生成全局组件,请加 global/ 前缀') |
| 30 | +let componentName = '' |
| 31 | +process.stdin.on('data', async chunk => { |
| 32 | + const inputName = String(chunk).trim().toString() |
| 33 | + /** |
| 34 | + * 组件目录路径 |
| 35 | + */ |
| 36 | + const componentDirectory = resolve('../../src/components', inputName) |
| 37 | + |
| 38 | + /** |
| 39 | + * vue组件路径 |
| 40 | + */ |
| 41 | + const componentVueName = resolve(componentDirectory, 'index.vue') |
| 42 | + |
| 43 | + const hasComponentDirectory = fs.existsSync(componentDirectory) |
| 44 | + if (hasComponentDirectory) { |
| 45 | + errorLog(`${inputName}组件目录已存在,请重新输入`) |
| 46 | + return |
| 47 | + } else { |
| 48 | + log(`正在生成 component 目录 ${componentDirectory}`) |
| 49 | + await dotExistDirectoryCreate(componentDirectory) |
| 50 | + // fs.mkdirSync(componentDirectory); |
| 51 | + } |
| 52 | + try { |
| 53 | + if (inputName.includes('/')) { |
| 54 | + const inputArr = inputName.split('/') |
| 55 | + componentName = inputArr[inputArr.length - 1] |
| 56 | + } else { |
| 57 | + componentName = inputName |
| 58 | + } |
| 59 | + log(`正在生成 vue 文件 ${componentVueName}`) |
| 60 | + await generateFile(componentVueName, vueTemplate(componentName)) |
| 61 | + successLog('生成成功') |
| 62 | + } catch (e) { |
| 63 | + errorLog(e.message) |
| 64 | + } |
| 65 | + |
| 66 | + process.stdin.emit('end') |
| 67 | +}) |
| 68 | +process.stdin.on('end', () => { |
| 69 | + log('exit') |
| 70 | + process.exit() |
| 71 | +}) |
| 72 | + |
| 73 | +function dotExistDirectoryCreate (directory) { |
| 74 | + return new Promise((resolve) => { |
| 75 | + mkdirs(directory, function () { |
| 76 | + resolve(true) |
| 77 | + }) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +// 递归创建目录 |
| 82 | +function mkdirs (directory, callback) { |
| 83 | + var exists = fs.existsSync(directory) |
| 84 | + if (exists) { |
| 85 | + callback() |
| 86 | + } else { |
| 87 | + mkdirs(path.dirname(directory), function () { |
| 88 | + fs.mkdirSync(directory) |
| 89 | + callback() |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments