Skip to content

Commit 142e0e2

Browse files
committed
feat: 用户执行 init 时判断环境
1 parent 6a36abe commit 142e0e2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/midway-init/bin/midway-init.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
'use strict';
44

55
const co = require('co');
6+
const childProcess = require('child_process');
7+
const { Confirm } = require('enquirer');
68
const Command = require('..');
79
const pkgInfo = require('../package.json');
810

@@ -13,9 +15,40 @@ const options = {
1315

1416
co(function* () {
1517
const args = process.argv.slice(2);
18+
19+
if (isInternal()) {
20+
const prompt = new Confirm({
21+
name: 'really',
22+
message: '检测到目前可能处于内网环境,推荐使用 @ali/midway-init,确定要继续吗?',
23+
initial: false,
24+
default: '[Y(es)|N(o)]',
25+
});
26+
const isContinue = yield prompt.run();
27+
28+
if (!isContinue) {
29+
return;
30+
}
31+
}
32+
1633
const cmd = new Command(Object.assign({}, options));
1734
yield cmd.run(process.cwd(), args);
1835
}).catch(err => {
1936
console.error(err.stack);
2037
process.exit(1);
2138
});
39+
40+
// 判断是否处于内网环境
41+
function isInternal() {
42+
try {
43+
const { stdout } = childProcess.spawnSync('tnpm', [ 'view', '@ali/midway-init', '--json' ]);
44+
const npmData = JSON.parse(stdout.toString(), null, 2);
45+
46+
if (npmData.name === '@ali/midway-init') {
47+
return true;
48+
} else {
49+
return false;
50+
}
51+
} catch (err) {
52+
return false;
53+
}
54+
}

packages/midway-init/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"co": "^4.6.0",
66
"egg-init": "^1.15.0",
7+
"enquirer": "^2.3.0",
78
"yargs": "^12.0.5"
89
},
910
"main": "lib/command.js",

0 commit comments

Comments
 (0)