const fs = require('fs'); const path = require('path'); const actionsall = fs.readFileSync(path.resolve(__dirname, './commitApps.txt'), 'utf-8'); const nameDisablelist = fs.readFileSync(path.resolve(__dirname, './nameDisablelist.txt'), 'utf-8'); const sorceProjectname = fs.readFileSync(path.resolve(__dirname, './sorceprojectName.txt'), 'utf-8'); const all = JSON.parse(actionsall); module.exports = function (plop) { plop.setGenerator('createWebapp', { description: '创建一个新的webapp项目', prompts: [ { type: 'input', name: 'projectName', message: `请输入新项目文件夹名称请以${sorceProjectname.replace(sorceProjectname.split('-')[0], 'xxx')}命名`, }, ], actions: function (data) { const projectName = data.projectName.trim(); const actions = []; const nameRE = new RegExp('(\\-(' + sorceProjectname.split('-')[1] + '))$'); if (projectName && nameRE.test(projectName) && !JSON.parse(nameDisablelist).includes(projectName)) { fs.writeFileSync(path.resolve(__dirname, './projectName.txt'), projectName); all.forEach(item => { const newitem = item.replace(sorceProjectname, projectName); if (/(.vue)$/.test(newitem)) { actions.push({ type: 'add', path: `..${newitem}`, template: '', abortOnFail: false, }); } else { actions.push({ type: 'add', path: `..${newitem}`, templateFile: `..${item}`, abortOnFail: false, }); } }); } else { console.error( `注意: 请以${sorceProjectname.replace( sorceProjectname.split('-')[0], 'xxx' )}命名, 且不能与webapp目录下已有项目名(现存:${JSON.parse(nameDisablelist).join(',')})重复` ); process.exit(1); } return actions; }, }); };标签:fs,const,projectName,基础,plopjs,actions,sorceProjectname,path,方法 From: https://www.cnblogs.com/taxun/p/16805499.html