|
| 1 | +const inquirer = require('inquirer'); |
| 2 | +const changeCase = require('change-case'); |
| 3 | +const Handlebars = require('handlebars'); |
| 4 | +const Metalsmith = require('metalsmith'); |
| 5 | +const chalk = require('chalk'); |
| 6 | +const path = require('path'); |
| 7 | +const fs = require('fs'); |
| 8 | + |
| 9 | +const questions = [ |
| 10 | + { |
| 11 | + name: 'dirName', |
| 12 | + message: '组件目录名,中横线命名法(param-case)', |
| 13 | + type: 'input', |
| 14 | + default: 'new-comp', |
| 15 | + }, |
| 16 | + { |
| 17 | + name: 'compNameZH', |
| 18 | + message: '组件中文名', |
| 19 | + type: 'input', |
| 20 | + default: '测试组件', |
| 21 | + }, |
| 22 | +]; |
| 23 | + |
| 24 | +/** |
| 25 | + * 确认组件目录是否已经存在 |
| 26 | + * @param {string} path 文件夹路径 |
| 27 | + */ |
| 28 | +function isDirExisted(path) { |
| 29 | + try { |
| 30 | + fs.accessSync(path); |
| 31 | + } catch (e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + return true; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 渲染组件初始化模板 |
| 39 | + * @param {string} dest 目标文件夹名称 |
| 40 | + * @param {Object} state 模板数据 |
| 41 | + */ |
| 42 | +function renderTemplate(dest, state) { |
| 43 | + return new Promise((resolve, reject) => { |
| 44 | + const source = path.join(__dirname, '../template'); |
| 45 | + const metalsmith = Metalsmith(process.cwd()) |
| 46 | + .metadata(state) |
| 47 | + .clean(false) |
| 48 | + .source(source) |
| 49 | + .destination(dest); |
| 50 | + |
| 51 | + metalsmith |
| 52 | + .use((files, metalsmith, done) => { |
| 53 | + const meta = metalsmith.metadata(); |
| 54 | + Object.keys(files).forEach(fileName => { |
| 55 | + try { |
| 56 | + const t = files[fileName].contents.toString(); |
| 57 | + files[fileName].contents = new Buffer(Handlebars.compile(t)(meta)); |
| 58 | + } catch (error) { |
| 59 | + console.error(chalk.red(`render file error: ${fileName}`)); |
| 60 | + } |
| 61 | + }); |
| 62 | + done(undefined, files, metalsmith); |
| 63 | + }) |
| 64 | + .build(err => { |
| 65 | + if (err) { |
| 66 | + reject(err); |
| 67 | + } else { |
| 68 | + resolve(); |
| 69 | + console.log(chalk.yellowBright(`- 目录创建成功`)); |
| 70 | + } |
| 71 | + }); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * 向入口文件 re-export 组件 |
| 77 | + * @param {string} dirName 目标文件夹名称 |
| 78 | + * @param {string} compNameEN 组件英文名称 |
| 79 | + */ |
| 80 | +function insert2EntryFile(dirName, compNameEN) { |
| 81 | + const entryFilePath = path.join(__dirname, '../components/index.ts'); |
| 82 | + fs.appendFileSync( |
| 83 | + entryFilePath, |
| 84 | + `\nexport { default as ${compNameEN} } from './${dirName}';\n`, |
| 85 | + 'utf8', |
| 86 | + ); |
| 87 | + console.log(chalk.yellowBright(`- 入口文件写入成功`)); |
| 88 | +} |
| 89 | + |
| 90 | +async function main() { |
| 91 | + const { dirName, compNameZH } = await inquirer.prompt(questions); |
| 92 | + const dest = path.join(__dirname, `../components/${dirName}`); |
| 93 | + if (isDirExisted(dest)) { |
| 94 | + console.log(chalk.red(`💣 该组件目录已存在`)); |
| 95 | + return; |
| 96 | + } |
| 97 | + const compNameEN = changeCase.pascalCase(dirName); |
| 98 | + const state = { compNameZH, compNameEN, line_for_keep_metadata: '---' }; |
| 99 | + await renderTemplate(dest, state); |
| 100 | + insert2EntryFile(dirName, compNameEN); |
| 101 | + console.log(chalk.green(`✨ ${compNameEN}-${compNameZH} 组件创建成功`)); |
| 102 | +} |
| 103 | + |
| 104 | +main(); |
0 commit comments