Skip to content

Commit 3ef6432

Browse files
committed
feat: first blood
0 parents  commit 3ef6432

File tree

5 files changed

+174
-0
lines changed

5 files changed

+174
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# dependencies
2+
/node_modules
3+
/npm-debug.log*
4+
/yarn-error.log
5+
/yarn.lock
6+
/package-lock.json
7+
8+
.DS_Store
9+
.idea/
10+
.vscode

README.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# cli-theory
2+
3+
## 原理
4+
5+
- **命令行参数**[process.argv](https://devdocs.io/node/process#process_process_argv)
6+
- **脚本解释程序**:#!/usr/bin/env
7+
- 放在脚本语言的第一行,目的是指出,你想要你的这个文件中的代码用什么可执行程序去运行它
8+
- 脚本用 env 启动的原因,是因为脚本解释器在 linux 中可能被安装于不同的目录,env 可以在系统的 PATH 目录中查找
9+
- **可执行文件的软链**:package.json: bin 字段
10+
11+
### demo
12+
13+
```bash
14+
.
15+
├── bin
16+
│   └── index.js
17+
├── index.js
18+
└── package.json
19+
```
20+
21+
bin/index.js
22+
23+
```javascript
24+
const pkg = require('../package.json');
25+
26+
function run(argv) {
27+
console.log(argv);
28+
argv = argv.slice(2);
29+
if (!argv.length) {
30+
// 对于退出操作,用 process.exit(code) 代替 return,成功的话 code 为 0,失败为 1
31+
process.exit(1);
32+
}
33+
if (argv[0] === '-v' || argv[0] === '--version') {
34+
console.log(` version is ${pkg.version}`);
35+
} else if (argv[0] === '-h' || argv[0] === '--help') {
36+
console.log(' usage:\n');
37+
console.log(' -v --version [show version]');
38+
}
39+
}
40+
41+
run(process.argv);
42+
```
43+
44+
运行
45+
46+
```bash
47+
$ node bin/index.js -v
48+
$ [ '/Users/jiangzhiguo/.nvm/versions/node/v10.15.3/bin/node',
49+
'/Users/jiangzhiguo/Workspace/jsany/cli-theory/bin/index.js',
50+
'-v' ]
51+
version is 1.0.0
52+
```
53+
54+
通过脚本解释程序以及 package.json 的 bin 字段对上面进行简化
55+
56+
bin/index.js
57+
58+
```diff
59+
+ #!/usr/bin/env node
60+
const pkg = require('../package.json')
61+
```
62+
63+
package.json
64+
65+
```json
66+
{
67+
"name": "cli-theory",
68+
"version": "1.0.0",
69+
"description": "",
70+
"main": "index.js",
71+
"scripts": {
72+
"test": "echo \"Error: no test specified\" && exit 1"
73+
},
74+
"bin": {
75+
"cli-theory": "bin/index.js"
76+
},
77+
"author": "",
78+
"license": "ISC"
79+
}
80+
```
81+
82+
### 测试
83+
84+
用过 link 符号链接,模拟发包后进行安装使用这个命令
85+
86+
在这个包的根目录执行:
87+
88+
`yarn link`
89+
90+
查看验证 cli-theory 软链:
91+
92+
```bash
93+
$ cat /usr/local/bin/cli-theory
94+
$ #!/usr/bin/env node
95+
96+
const pkg = require('../package.json')
97+
98+
function run(argv) {
99+
console.log(argv);
100+
argv = argv.slice(2)
101+
if(!argv.length){
102+
// 对于退出操作,用 process.exit(code) 代替 return,成功的话 code 为 0,失败为 1
103+
process.exit(1)
104+
}
105+
if (argv[0] === '-v' || argv[0] === '--version') {
106+
console.log(` version is ${pkg.version}`);
107+
} else if (argv[0] === '-h' || argv[0] === '--help') {
108+
console.log(' usage:\n');
109+
console.log(' -v --version [show version]');
110+
}
111+
}
112+
113+
run(process.argv)%
114+
```
115+
116+
执行脚手架命令:
117+
118+
```bash
119+
$ cli-theory -v
120+
$ [ '/Users/jiangzhiguo/.nvm/versions/node/v10.15.3/bin/node',
121+
'/usr/local/bin/cli-theory',
122+
'-v' ]
123+
version is 1.0.0
124+
```
125+
126+
可以看到 cli-theory === node bin/index.js,简化了命令
127+
128+
## 完整的脚手架
129+
130+
源码:<https://github.com/jsany/cli-kit>

bin/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env node
2+
3+
const pkg = require('../package.json')
4+
5+
function run(argv) {
6+
console.log(argv);
7+
argv = argv.slice(2)
8+
if (!argv.length) {
9+
// 对于退出操作,用 process.exit(code) 代替 return,成功的话 code 为 0,失败为 1
10+
process.exit(1);
11+
}
12+
if (argv[0] === '-v' || argv[0] === '--version') {
13+
console.log(` version is ${pkg.version}`);
14+
} else if (argv[0] === '-h' || argv[0] === '--help') {
15+
console.log(' usage:\n');
16+
console.log(' -v --version [show version]');
17+
}
18+
}
19+
20+
run(process.argv)

index.js

Whitespace-only changes.

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "cli-theory",
3+
"version": "1.0.0",
4+
"description": "The principle of scaffold",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"bin": {
10+
"cli-theory": "bin/index.js"
11+
},
12+
"author": "",
13+
"license": "ISC"
14+
}

0 commit comments

Comments
 (0)