Flow chart
Tools used
- commander
- chalk
- cross-spawn
- fs-etra
- inquirer
The main idea and key code to start scaffolding
According to user terminal input, get global configuration information
Program.version (packagejson.version) node index.js --version/-V .usage(`${chalk.green('<project-name>')} [options]`) .option('-t --template <template-name>', 'Please select template name '). Option ('-r --templateRepoUrl <template-repo-url>', Description (' from woyao '). Action (function (path, CMD) {// output validation}). On ('--help', function () { howToUse('<project-name>') }) .parse(process.argv) const projectName = program.args[0] const templateRepoUrl = program.templateRepoUrlCopy the code
Copy the remote template repository
function gitCloneRepository(templateRepoUrl) { return new Promise(function (resolve, Reject) {log(' ${chalk. Green (' drop template ')} ') const command = 'git' const args = ['clone', templateRepoUrl || `https://gitee.com/chenwoyao/woyao_templates.git`, path.join(cliRoot, 'temp') ] const child = spawn(command, args, { stdio: 'inherit' }) child.on('close', (code) => { if (code ! == 0) {reject(' make sure you have access to the remote repository or your remote repository address is incorrect ') return} console.log(' ${chalk. Green (' template repository successfully ')} ') resolve()})})}Copy the code
Select a template
Function choiceTemplate(dirs) {return inquirer. Prompt ([{name: 'template', message: 'please select the template you want to use ', type: 'list', choices: dirs, }]) .then(answer => { return answer }) }Copy the code
Embed the template in the project to be built
function copyTemplate(templateName, appRoot) { return new Promise((resolve, Reject) => {console.log(' \n${chalk. GreenBright (' Copy template... ')}`) const templatePath = path.join( cliRoot, 'temp', 'templates', templateName ) if (fs.existsSync(templatePath)) { fs.copySync(templatePath, appRoot) util.createDirTree(appRoot, [' node_modules', 'vscode', 'git']). The console log (` ${chalk. GreenBright (' template copy complete... ')}\n ') resolve()} else {reject(' not found: ${templateName} ') return}})}Copy the code
Release NPM package
-
Create an NPM account (username, email, password)
-
Initialize the project
mkdir <projectName> && cd <projectName>
npm init
npm adduse
-
npm publish
Matters needing attention:
Make sure your official NPM source is NPM config set registry HTTPS :*//registry.npmjs.org
After the switch to the source: taobao NPM config set registry HTTPS: * / / registry.npm.taobao.org my side even if switch to taobao source node installation – sass and sass – loader is very slow when the two packages. So I went straight to Yarn. There is always a warning message printed when using CNPM management tool installation, so it is not necessary.
If your login has expired or you want to switch users, type NPM login to login again