Skip to content

Commit d4d5d61

Browse files
committed
chore: update the usage method of commander
1 parent 07a8952 commit d4d5d61

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-request-mock",
3-
"version": "1.8.23",
3+
"version": "1.8.24",
44
"description": "Intercept & mock http requests issued by XMLHttpRequest, fetch, nodejs https/http module, axios, jquery, superagent, ky, node-fetch, request, got or any other request libraries by intercepting XMLHttpRequest, fetch and nodejs native requests in low level.",
55
"main": "src/index.js",
66
"module": "http-request-mock.esm.mjs",

tool/bin/cli.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ const program = new Command();
1717
module.exports = new class CommandToolLine {
1818
constructor() {
1919
this.appRoot = getAppRoot();
20-
this.setOptions();
20+
this.opts = this.setOptions();
2121

22-
program.environment = program.environment && /^\w+=\w+$/.test(program.environment)
23-
? program.environment
22+
this.opts.environment = this.opts.environment && /^\w+=\w+$/.test(this.opts.environment)
23+
? this.opts.environment
2424
: '';
25-
program.index = entryPoints.includes(program.index) ? program.index : '';
25+
this.opts.index = entryPoints.includes(this.opts.index) ? this.opts.index : '';
2626
this.main();
2727
}
2828

2929
/**
3030
* Main control flow
3131
*/
3232
main() {
33-
if (program.init) {
33+
if (this.opts.init) {
3434
return this.init();
3535
}
36-
if (program.inject) {
36+
if (this.opts.inject) {
3737
return this.inject();
3838
}
39-
if (program.watch) {
39+
if (this.opts.watch) {
4040
return this.watch();
4141
}
42-
if (program.proxy === 'matched') {
42+
if (this.opts.proxy === 'matched') {
4343
return this.proxy();
4444
}
4545
}
@@ -48,7 +48,7 @@ module.exports = new class CommandToolLine {
4848
* -i, --init: Initialize some samples & a .runtime.js in the mock directory
4949
*/
5050
async init() {
51-
const dir = path.resolve(this.appRoot, program.directory);
51+
const dir = path.resolve(this.appRoot, this.opts.directory);
5252
if (fs.existsSync(dir) && fs.statSync(dir).isFile()) {
5353
return log(`${dir} already exists and is not directory.`);
5454
}
@@ -65,10 +65,10 @@ module.exports = new class CommandToolLine {
6565
const webpack = new WebpackPlugin({
6666
dir,
6767
entry: /1/,
68-
type: program.type,
69-
index: program.index,
68+
type: this.opts.type,
69+
index: this.opts.index,
7070
});
71-
webpack.environment = program.environment ? program.environment.split('=') : null;
71+
webpack.environment = this.opts.environment ? this.opts.environment.split('=') : null;
7272

7373
this.copySampleFiles(dir);
7474

@@ -81,14 +81,14 @@ module.exports = new class CommandToolLine {
8181
* -j, --inject <app-entry-file>: Inject .runtime.js into the specified entry relative to the working directory.
8282
*/
8383
async inject() {
84-
const appEntryFile = path.resolve(this.appRoot, program.inject);
84+
const appEntryFile = path.resolve(this.appRoot, this.opts.inject);
8585
if (!fs.existsSync(appEntryFile)) {
8686
log(`The specified app entry file [\x1b[31m${appEntryFile}\x1b[0m] does not exist.`);
8787
return;
8888
}
8989

9090
await this.init();
91-
const dir = path.resolve(this.appRoot, program.directory);
91+
const dir = path.resolve(this.appRoot, this.opts.directory);
9292

9393
let runtime = path.resolve(dir, '.runtime.js');
9494
runtime = path.relative(path.resolve(appEntryFile, '../'), runtime);
@@ -122,39 +122,39 @@ module.exports = new class CommandToolLine {
122122
* ths specified command will be executed together with watching.'
123123
*/
124124
async watch() {
125-
const dir = path.resolve(this.appRoot, program.directory);
125+
const dir = path.resolve(this.appRoot, this.opts.directory);
126126
if (!fs.existsSync(path.resolve(dir, '.runtime.js'))) {
127127
log(`There is no a .runtime.js file in the mock directory: ${dir}.`);
128128
log('Please use command(npx http-request-mock-cli -i) to initialize it.');
129129
return;
130130
}
131131

132-
const proxyServer = program.proxy === 'matched'
132+
const proxyServer = this.opts.proxy === 'matched'
133133
? await server.init({
134-
type: program.type,
134+
type: this.opts.type,
135135
mockDir: dir,
136-
environment: program.environment,
137-
proxyMode: program.proxy
136+
environment: this.opts.environment,
137+
proxyMode: this.opts.proxy
138138
})
139139
: '';
140140
log(`Watching: ${dir}`);
141141
const webpack = new WebpackPlugin({
142142
dir,
143143
entry: /1/,
144-
type: program.type,
145-
index: program.index,
146-
proxyMode: program.proxy
144+
type: this.opts.type,
145+
index: this.opts.index,
146+
proxyMode: this.opts.proxy
147147
});
148148

149-
webpack.environment = program.environment ? program.environment.split('=') : null;
149+
webpack.environment = this.opts.environment ? this.opts.environment.split('=') : null;
150150
if (proxyServer) {
151-
webpack.proxyServer = program.proxy + '@' + proxyServer;
151+
webpack.proxyServer = this.opts.proxy + '@' + proxyServer;
152152
}
153153

154154
watchDir(webpack, dir, (files) => proxyServer && server.reload(files));
155155

156-
if (typeof program.watch === 'string') {
157-
spawn(program.watch, { cwd: this.appRoot, env: process.env, stdio: 'inherit', detached: false, shell: true });
156+
if (typeof this.opts.watch === 'string') {
157+
spawn(this.opts.watch, { cwd: this.appRoot, env: process.env, stdio: 'inherit', detached: false, shell: true });
158158
}
159159
}
160160

@@ -167,9 +167,9 @@ module.exports = new class CommandToolLine {
167167
* [matched] All requests matched by @url will be proxied to a proxy server.
168168
*/
169169
proxy() {
170-
if (/^(matched)$/.test(program.proxy)) {
171-
const dir = path.resolve(this.appRoot, program.directory);
172-
server.init({ type: program.type, mockDir: dir, environment: program.environment, proxyMode: program.proxy });
170+
if (/^(matched)$/.test(this.opts.proxy)) {
171+
const dir = path.resolve(this.appRoot, this.opts.directory);
172+
server.init({ type: this.opts.type, mockDir: dir, environment: this.opts.environment, proxyMode: this.opts.proxy });
173173
}
174174
}
175175

@@ -261,5 +261,6 @@ module.exports = new class CommandToolLine {
261261
'none'
262262
)
263263
.parse(process.argv);
264+
return program.opts();
264265
}
265266
};

0 commit comments

Comments
 (0)