WTF doesn’t know what scaffolding is? Then hurry up and make up the front base brother die, I believe you will come back soon.
Ps: The file operation in this article is based on bash, MAC students can use terminal or iterm directly, win users recommend using Git bash
Scaffolding, from the construction industry, colloquially speaking is a tool for building a project. Vue-cli create-react-app is one of the most popular tools in the world. This tool is usually named XXX – CLI
Little brother, take a look at this data, do you want it or not?
==================== split line ====================
Implement node and shell interaction
The first step is to run our own Node command
First, create a project directory. According to the convention, our scaffolding is also called CLI. Let’s call it learn-CLI
mkdir learn-cli
Copy the code
Then, create a Node project directly by NPM init-y
cd learn-cli
npm init -y
Copy the code
With your favorite editor, open the learn-cli directory, recommend VScode, and add the code to package.json
"bin": {
"learn": "./bin/learn.js"
},
Copy the code
Create the bin directory and the learn.js file and add the following code to learn.js. File header #! /usr/bin/env node tells the shell to parse the next learn file as node. The rest is the familiar js ^ – ^
#! /usr/bin/env node
console.log('Good good study, day day up')
Copy the code
Enter the root directory of the project through the terminal and execute the NPM link. If a message is displayed, the link is successful
The effect of this command is to add two soft links (which Win users can understand as shortcuts) to the system environment variable. At this point, type learn on the command line to try it out
Ha ha ha ha, familiar with the famous words so out, out….
Receives arguments from the command line
Process. argv can be used to get the command-line arguments passed in when the Node process is started
Get the command’s parameters in the native way
Adjust the learn.js file as follows:
#! /usr/bin/env node
console.log(process.argv[2] + 'Good good study, day day up')
Copy the code
Go back to the terminal and execute the Learn circle again, and you get the following output. See the documentation for why process.argv[2] was used
usecommanderReceives arguments passed from the command line
It is possible to use the native method to get the parameters passed in, but this method is more suitable for simple demonstration projects. Here we use the community-preferred commander to handle parameters. Don’t ask me why, just for two words: ——-
Start with an official chestnut
#! /usr/bin/env node
const program = require('commander')
program
.version('0.0.1')
.option('-C, --chdir <path>'.'change the working directory')
.option('-c, --config <path>'.'set config path. defaults to ./deploy.conf')
.option('-T, --no-tests'.'ignore test hook')
program
.command('setup')
.description('run remote setup commands')
.action(function() {
console.log('setup');
});
program
.command('exec <cmd>')
.description('run the given remote command')
.action(function(cmd) {
console.log('exec "%s"', cmd);
});
program
.command('teardown
[otherDirs...] '
)
.description('run teardown commands')
.action(function(dir, otherDirs) {
console.log('dir "%s"', dir);
if (otherDirs) {
otherDirs.forEach(function (oDir) {
console.log('dir "%s"', oDir); }); }}); program .command(The '*')
.description('deploy the given env')
.action(function(env) {
console.log('deploying "%s"', env);
});
program.parse(process.argv);
Copy the code
First, install Commander
npm i commander -S
Copy the code
Then paste the official example into our learn.js, execute a random command to try the “learn” circle “learn-v”, it is so strange to use.
So far, we have completed the first step of the Great Wall. If you are interested, you can try the following steps for yourself.
Series of articles:
- Step one, create the first command
- Step 2: Set up the development environment
- Step 3: Get information about github projects
- The fourth step is to obtain project information through lifting code
- [Hand in hand to take you to lift a scaffolding] step five, finish the job