Skip to content

Commit

Permalink
feat: 用户执行 init 时判断环境
Browse files Browse the repository at this point in the history
  • Loading branch information
imsobear committed Jan 20, 2019
1 parent 6a36abe commit 142e0e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/midway-init/bin/midway-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
'use strict';

const co = require('co');
const childProcess = require('child_process');
const { Confirm } = require('enquirer');
const Command = require('..');
const pkgInfo = require('../package.json');

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

co(function* () {
const args = process.argv.slice(2);

if (isInternal()) {
const prompt = new Confirm({
name: 'really',
message: '检测到目前可能处于内网环境,推荐使用 @ali/midway-init,确定要继续吗?',
initial: false,
default: '[Y(es)|N(o)]',
});
const isContinue = yield prompt.run();

if (!isContinue) {
return;
}
}

const cmd = new Command(Object.assign({}, options));
yield cmd.run(process.cwd(), args);
}).catch(err => {
console.error(err.stack);
process.exit(1);
});

// 判断是否处于内网环境
function isInternal() {
try {
const { stdout } = childProcess.spawnSync('tnpm', [ 'view', '@ali/midway-init', '--json' ]);
const npmData = JSON.parse(stdout.toString(), null, 2);

if (npmData.name === '@ali/midway-init') {
return true;
} else {
return false;
}
} catch (err) {
return false;
}
}
1 change: 1 addition & 0 deletions packages/midway-init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"co": "^4.6.0",
"egg-init": "^1.15.0",
"enquirer": "^2.3.0",
"yargs": "^12.0.5"
},
"main": "lib/command.js",
Expand Down

0 comments on commit 142e0e2

Please sign in to comment.