Make writing a habit together! This is the 10th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
Download the template
/ * * *@description: Download templates * 1. Obtain project template information through the project template API * 1.1 Build a back-end system through egg.js HZW -cli-dev-server * 1.2 Store project templates through NPM (vue-cli/vue-element-admin) * 1.3 Store project template information in mongodb database * 1.4 Retrieve mongodb data via egg.js and return it via API *@param {*}
* @return {*}* /
async downloadTemplate() {
// Get the template name
const { template } = this.projectInfo
// The template information is matched based on the name
const templateInfo = this.template.find((item) = > item.npmName === template)
// Splice paths
const targetPath = path.resolve(userHome, '.hzw-cli-dev'.'template')
const storeDir = path.resolve(userHome, '.hzw-cli-dev'.'template'.'node_modules')
const { npmName, version } = templateInfo
// Create a Package
const templateNpm = new Package({
targetPath,
storeDir,
packageName: npmName,
packageVersion: version,
})
// Check whether the package exists
if (! await templateNpm.exists()) {
/ / installation
await templateNpm.install();
} else {
/ / update
await templateNpm.update()
}
}
Copy the code
After executing the command, we found that our template was already in the user’s home directory.
hzw-cli-dev init -tp D:\imooc-learn\hzw-cli-dev\hzw-cli-dev\commands\init test-project
Copy the code
Using spinner to implement the command-line loading effect
/ / install cli - spinner
npm i cli-spinner -S
Copy the code
The code is simple, just a few lines
const Spinner = require('cli-spinner').Spinner
const spinner = new Spinner('processing.. %s');
spinner.setSpinnerString('| / - \ \');
spinner.start();
Copy the code
The default effect is as follows
Stop the loading effect with spinner.start(). But the text will also be displayed, and passing a true will make the text disappear when loading is finished, that is, spinner.start(true).
Encapsulate as a method
/ * * *@description: command line loading effect *@param {*}
* @return {*}* /
const spinnerStart = (message) = > {
const Spinner = require('cli-spinner').Spinner
const spinner = new Spinner(`${message} %s`);
spinner.setSpinnerString('⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏');
spinner.start()
return spinner
}
Copy the code
A method is called
// Check whether the package exists
if (!await templateNpm.exists()) {
/ / installation
const spinner = spinnerStart('Template download, please wait... ')
await sleep()
await templateNpm.install();
spinner.stop(true)
log.warn('Template downloaded successfully')}else {
/ / update
const spinner = spinnerStart('Template updating, please wait... ')
await sleep()
await templateNpm.update()
spinner.stop(true)
log.warn('Template updated successfully')}Copy the code
The effect is as follows, there is a flash during installation, maybe there is a problem with spinner library, try another library then.
Template update
Publish a new version of the hzw-cli-dev-template-vue3 template via NPM publish.
The template was downloaded for the first time
You can see that it is version 1.0.0
Updated the template for the second time
You can see there’s an extra version
Note: I didn’t read chapter 7 of week 5.