不知道你們每次新建組件的時候,是否是都要建立一個目錄,而後新增一個.vue文件,而後寫template、script、style這些東西,若是是公共組件,是否是還要新建一個index.js用來導出vue組件、雖然有vscode有代碼片斷能實現自動補全,但仍是很麻煩,今天靈活運用scripts工做流,自動生成vue文件和目錄。javascript
template.js文件,裏面的內容能夠本身自定義,符合當前項目的模板便可vue
// template.js module.exports = { vueTemplate: compoenntName => { return `<template> <div class="${compoenntName}"> ${compoenntName}組件 </div> </template> <script> export default { name: '${compoenntName}' } </script> <style scoped lang="stylus" rel="stylesheet/stylus"> .${compoenntName} { } </style> ` }, entryTemplate: `import Main from './main.vue' export default Main` }
generateComponent.js生成vue目錄和文件的代碼java
// generateComponent.js` const chalk = require('chalk') // 控制檯打印彩色 const path = require('path') const fs = require('fs') const resolve = (...file) => path.resolve(__dirname, ...file) const log = message => console.log(chalk.green(`${message}`)) const successLog = message => console.log(chalk.blue(`${message}`)) const errorLog = error => console.log(chalk.red(`${error}`)) const { vueTemplate, entryTemplate } = require('./template') const _ = process.argv.splice(2)[0] === '-com' const generateFile = (path, data) => { if (fs.existsSync(path)) { errorLog(`${path}文件已存在`) return } return new Promise((resolve, reject) => { fs.writeFile(path, data, 'utf8', err => { if (err) { errorLog(err.message) reject(err) } else { resolve(true) } }) }) } // 公共組件目錄src/base,全局註冊組件目錄src/base/global,頁面組件目錄src/components _ ? log('請輸入要生成的組件名稱、如需生成全局組件,請加 global/ 前綴') : log('請輸入要生成的頁面組件名稱、會生成在 components/目錄下') let componentName = '' process.stdin.on('data', async chunk => { const inputName = String(chunk).trim().toString() // 根據不一樣類型組件分別處理 if (_) { // 組件目錄路徑 const componentDirectory = resolve('../src/base', inputName) // vue組件路徑 const componentVueName = resolve(componentDirectory, 'main.vue') // 入口文件路徑 const entryComponentName = resolve(componentDirectory, 'index.js') const hasComponentDirectory = fs.existsSync(componentDirectory) if (hasComponentDirectory) { errorLog(`${inputName}組件目錄已存在,請從新輸入`) return } else { log(`正在生成 component 目錄 ${componentDirectory}`) await dotExistDirectoryCreate(componentDirectory) } try { if (inputName.includes('/')) { const inputArr = inputName.split('/') componentName = inputArr[inputArr.length - 1] } else { componentName = inputName } log(`正在生成 vue 文件 ${componentVueName}`) await generateFile(componentVueName, vueTemplate(componentName)) log(`正在生成 entry 文件 ${entryComponentName}`) await generateFile(entryComponentName, entryTemplate) successLog('生成成功') } catch (e) { errorLog(e.message) } } else { const inputArr = inputName.split('/') const directory = inputArr[0] let componentName = inputArr[inputArr.length - 1] // 頁面組件目錄 const componentDirectory = resolve('../src/components', directory) // vue組件 const componentVueName = resolve(componentDirectory, `${componentName}.vue`) const hasComponentDirectory = fs.existsSync(componentDirectory) if (hasComponentDirectory) { log(`${componentDirectory}組件目錄已存在,直接生成vue文件`) } else { log(`正在生成 component 目錄 ${componentDirectory}`) await dotExistDirectoryCreate(componentDirectory) } try { log(`正在生成 vue 文件 ${componentName}`) await generateFile(componentVueName, vueTemplate(componentName)) successLog('生成成功') } catch (e) { errorLog(e.message) } } process.stdin.emit('end') }) process.stdin.on('end', () => { log('exit') process.exit() }) function dotExistDirectoryCreate (directory) { return new Promise((resolve) => { mkdirs(directory, function () { resolve(true) }) }) } // 遞歸建立目錄 function mkdirs (directory, callback) { var exists = fs.existsSync(directory) if (exists) { callback() } else { mkdirs(path.dirname(directory), function () { fs.mkdirSync(directory) callback() }) } }
"scripts": { "new:view":"node scripts/generateComponent", "new:com": "node scripts/generateComponent -com" },
npm run new:view // 生成頁組件 npm run new:com // 生成基礎組件 或者 yarn run new:view // 生成頁組件 yarn run new:com // 生成基礎組件