Skip to content

Commit

Permalink
add network judge
Browse files Browse the repository at this point in the history
  • Loading branch information
zoumiaojiang committed Jun 30, 2017
1 parent 68678ae commit 1087014
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const cp = require('child_process');
const log = require('./log');
const initCommand = require('./init');

const version = process.env.VERSION || require('../package.json').version;
let version = process.env.VERSION || require('../package.json').version;

if (!process.argv[2]) {
cp.exec('lavas -h', (err, stdout, stderr) => {
Expand Down
60 changes: 34 additions & 26 deletions lib/init/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const fs = require('fs-extra');

const formQ = require('./formQuestion');
const log = require('../log');
const utils = require('../utils');

const cwd = process.cwd();
let cwd = process.cwd();

/**
* 导出工程
Expand All @@ -33,7 +34,7 @@ async function exportProject(params) {
spinner.stop();
console.log('');
log.info(''
+ '项目创建已成功,您可以操作如下命令开始开发工程:\n\n'
+ '项目已创建成功,您可以操作如下命令开始开发 Lavas 工程:\n\n'
+ log.chalk.green('cd ' + params.name + '\n'
+ 'npm install'
));
Expand All @@ -45,40 +46,47 @@ async function exportProject(params) {
* @param {Object} conf 初始化配置
*/
module.exports = async function (conf) {
let isNetworkOk = await utils.isNetworkConnect();

if (!isNetworkOk) {
log.error('创建工程需要下载云端模版,请确认您的设备处于网络环境中');
return;
}

if (!shelljs.which('git')) {
log.error('当前环境下没有检测到 git 命令,请确认是否安装 git');
return;
}

log.info(`欢迎使用 ${log.chalk.green('Lavas')} 解决方案`);
log.info('新建一个 PWA 项目\n');

if (shelljs.which('git')) {
let schema = await lavasScaffold.getSchema();
let params = await formQ(schema);
let projectTargetPath = path.resolve(params.dirPath || cwd, params.name);
let schema = await lavasScaffold.getSchema();
let params = await formQ(schema);
let projectTargetPath = path.resolve(params.dirPath || cwd, params.name);

if (fs.existsSync(projectTargetPath)) {
if (conf.force) {
// 直接覆盖当前项目
fs.removeSync(projectTargetPath);
await exportProject(params);
}
else {
let ret = await inquirer.prompt([{
'type': 'confirm',
'message': '存在同名项目,是否覆盖?',
'default': false,
'name': 'isForce'
}]);

if (fs.existsSync(projectTargetPath)) {
if (conf.force) {
// 直接覆盖当前项目
if (ret.isForce) {
fs.removeSync(projectTargetPath);
await exportProject(params);
}
else {
let ret = await inquirer.prompt([{
'type': 'confirm',
'message': '存在同名项目,是否覆盖?',
'default': false,
'name': 'isForce'
}]);

if (ret.isForce) {
fs.removeSync(projectTargetPath);
await exportProject(params);
}
}
}
else {
await exportProject(params);
}
}
else {
log.info('当前环境下没有检测到 git 命令,请确认是否安装 git');
await exportProject(params);
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/init/formQuestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function questionList(key, schema, params) {

sourceList.forEach(item => {
let url = '';
let desc = '\n\n ' + item.desc;
let desc = log.chalk.gray('\n\n ' + item.desc);

if (item.url) {
url = '\n\n - ' + log.chalk.yellow.bold.underline(item.url);
Expand All @@ -89,7 +89,7 @@ function questionList(key, schema, params) {

choiceList.push({
'value': item.value,
'name': `${item.value}${desc}${url}`,
'name': `${item.name}${desc}${url}`,
'short': item.value
});
});
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const os = require('os');
const fs = require('fs');
const path = require('path');
const dns = require('dns');

module.exports = {

Expand All @@ -27,5 +28,16 @@ module.exports = {
!fs.existsSync(dir) && fs.mkdirSync(dir);

return dir;
},

/**
* 检测当前网络环境
*
* @return {boolean} 是否联网
*/
isNetworkConnect() {
return new Promise(resolve => {
dns.lookup('baidu.com', err => resolve(!(err && err.code === 'ENOTFOUND')));
});
}
};

0 comments on commit 1087014

Please sign in to comment.