Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 17c58a8

Browse files
author
victorsun
committed
upd
1 parent fb227f8 commit 17c58a8

File tree

11 files changed

+626
-0
lines changed

11 files changed

+626
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 01-start 创建 csxiaoyao-cli
2+
3+
1. 创建 /bin 文件夹
4+
2. 修改 package.json,添加命令及文件
5+
6+
```
7+
"bin": {
8+
"csxiaoyao": "./bin/csxiaoyao.js"
9+
},
10+
```
11+
12+
3. 编写csxiaoyao.js代码文件
13+
```
14+
#!/user/bin/env node
15+
```
16+
17+
4. 添加软链接
18+
```
19+
$ sudo npm link
20+
# 取消
21+
$ npm unlink
22+
```
23+
24+
5. process.argv 接收命令行参数
25+
26+
27+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env node
2+
console.log('hello world');
3+
4+
/**
5+
* [ process.argv ] return Array
6+
* 返回参数1: node.js可执行文件的绝对路径
7+
* 返回参数2: 正在执行的javascript文件路径
8+
* 返回参数3开始: 通过命令行传递的参数
9+
*/
10+
process.argv.forEach(v => {
11+
console.log(v);
12+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "csxiaoyao-cli",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"bin": {
10+
"csxiaoyao": "./bin/csxiaoyao.js"
11+
},
12+
"author": "",
13+
"license": "ISC"
14+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 02-commander
2+
3+
1. 安装commander
4+
```
5+
$ npm install commander -S
6+
```
7+
8+
2. 编写代码查看版本号
9+
```
10+
const { program } = require('commander');
11+
program
12+
.version('0.0.1')
13+
.parse(process.argv)
14+
```
15+
查看版本号
16+
```
17+
$ csxiaoyao -h
18+
Usage: csxiaoyao [options]
19+
20+
Options:
21+
-V, --version output the version number
22+
-h, --help display help for command
23+
24+
$csxiaoyao -V
25+
0.0.1
26+
```
27+
28+
3. 修改帮助文档、配置options参数
29+
```
30+
.usage('<command> [csxiaoyao options]')
31+
.option('命令', '说明', '默认值')
32+
```
33+
34+
4. 获取输入参数
35+
36+
5. 创建命令
37+
```
38+
.command('add', 'add a template')
39+
.command('init', 'init a template')
40+
.command('create [options] <app-name>', 'create a new project powered by xxx')
41+
```
42+
43+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* 原生方式
5+
*/
6+
// process.argv.forEach(v => {
7+
// console.log(v);
8+
// });
9+
10+
const { program } = require('commander');
11+
program
12+
.version('0.0.1')
13+
.usage('<command> [csxiaoyao options]')
14+
.option('-cr, --classroom <custom classroom\'s name>', 'current classroom name') // '命令', '说明', '默认值'
15+
.command('add', 'add a template')
16+
.command('init', 'init a template')
17+
.command('create [options] <app-name>', 'create a new project powered by xxx')
18+
.parse(process.argv)
19+
20+
// 获取输入参数
21+
console.log(program.classroom);
22+

‎11-构建工具/05-脚手架/02-commander/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "commander-study",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"bin": {
10+
"csxiaoyao": "./bin/csxiaoyao.js"
11+
},
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"commander": "^11.0.0"
16+
}
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 03-inquirer
2+
3+
1. 安装
4+
```
5+
$ npm install inquirer
6+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
3+
// const inquirer = require('inquirer');
4+
import inquirer from 'inquirer';
5+
6+
// 定义基本的问答结构
7+
// 1. 定义问题列表
8+
const promptList = [
9+
// 输入
10+
{
11+
type: 'input',
12+
message: '请输入6位数字ID',
13+
name: 'id', // key
14+
default: '123456',
15+
validate: (val) => {
16+
if (val.match(/^\d{6}$/ig)) {
17+
return true;
18+
}
19+
return '请输入6位数字的ID〜〜〜';
20+
}
21+
},
22+
// 是/否选择
23+
{
24+
type: 'confirm',
25+
message: '是否使用监听模式',
26+
name: 'watch',
27+
prefix: '🌹', // 前缀
28+
suffix: '🇨🇳', // 后缀
29+
default: false,
30+
},
31+
//
32+
{
33+
34+
}
35+
];
36+
37+
// 2. 获取问题回答答案
38+
inquirer.prompt(promptList).then(answers => {
39+
console.log(answers);
40+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /