Skip to content

Commit 4627daa

Browse files
committed
perf(log): reform log methods
1 parent b95d359 commit 4627daa

File tree

11 files changed

+144
-113
lines changed

11 files changed

+144
-113
lines changed

.eslintrc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ globals:
4747
spinner: true
4848
logTime: true
4949
mkdirp: true
50+
logError: true
51+
logWarn: true
52+
logInfo: true
53+
logDoc: true
5054
JSON5: true
5155
YKIT_COMMANDS_PATH: true
5256
YKIT_RC: true

lib/commands/build.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,42 @@ exports.setOptions = function (optimist) {};
2020

2121
exports.npmInstall = function (a) {
2222
var currentNpm = null;
23+
var cwd = process.cwd();
2324

2425
// 检测是否存在 ykit.*.js
25-
var configFile = globby.sync(['ykit.*.js', 'ykit.js'], { cwd: process.cwd() })[0];
26+
var configFile = globby.sync(['ykit.*.js', 'ykit.js'], { cwd: cwd })[0];
2627
if (!configFile) {
27-
log('未发现存在 ykit 配置文件,编译退出.');
28+
logError('Local ykit.js not found in' + cwd);
2829
process.exit(1);
2930
}
3031

3132
// 检测是否存在 node_modules
32-
var isNodeModulesExists = fs.existsSync(sysPath.join(process.cwd(), 'node_modules'));
33+
var isNodeModulesExists = fs.existsSync(sysPath.join(cwd, 'node_modules'));
3334
if (isNodeModulesExists) {
34-
log('发现仓库中已存在 node_modules,这会导致由于 npm 包系统版本不兼容而编译失败,请从仓库中删除并重新编译.');
35+
logError('Find node_modules in the current directory which can cause compilation failure.');
36+
logError('Please remove it from your registry.');
37+
logInfo('Visit ' + 'http://ued.qunar.com/ykit/docs-%E5%8F%91%E5%B8%83.html'.underline + ' for doc.');
3538
process.exit(1);
3639
}
3740

3841
try {
3942
child_process.execSync('npm_cache_share -h');
4043

41-
if (UtilFs.fileExists(sysPath.join(process.cwd(), 'yarn.lock'))) {
44+
if (UtilFs.fileExists(sysPath.join(cwd, 'yarn.lock'))) {
4245
currentNpm = 'npm_cache_share';
43-
log('将使用 npm_cache_share + yarn 进行模块安装.');
44-
} else if (UtilFs.fileExists(sysPath.join(process.cwd(), 'npm-shrinkwrap.json'))) {
46+
log('Installing npm modules with npm_cache_share + yarn.');
47+
} else if (UtilFs.fileExists(sysPath.join(cwd, 'npm-shrinkwrap.json'))) {
4548
currentNpm = 'npm_cache_share';
46-
log('将使用 npm_cache_share + npm 进行模块安装.');
49+
log('Installing npm modules with npm_cache_share + npm.');
4750
} else {
4851
currentNpm = 'npm';
49-
log('yarn.lock/npm-shrinkwrap.json 不存在,将使用 npm 进行模块安装.');
50-
log('建议使用 yarn.lock 或 npm-shrinkwrap.json 固定模块版本,否则构建存在风险。文档请参考:http://ued.qunar.com/ykit/docs-npm%20shrinkwrap.html');
52+
log('Installing npm modules with npm.');
53+
logWarn('please use yarn or shrinkwrap to lock down the versions of packages.');
54+
logInfo('Visit ' + 'http://ued.qunar.com/ykit/docs-npm%20shrinkwrap.html'.underline + ' for doc.');
5155
}
5256
} catch (e) {
5357
currentNpm = 'npm';
54-
log('yarn.lock/npm_cache_share 不存在,将使用 npm 进行模块安装.');
58+
log('Installing npm modules with npm.');
5559
}
5660

5761
// cache clean
@@ -68,10 +72,10 @@ exports.run = function (options) {
6872
execute('ykit -v');
6973

7074
// build
71-
log('Build Started.');
75+
log('Building Started.');
7276
execute('ykit pack -m -q');
7377
clearGitHooks();
74-
log('Build Finished.\n');
78+
log('Building Finished.\n');
7579

7680
function clearGitHooks() {
7781
var gitHooksDir = './.git/hooks/';
@@ -101,7 +105,7 @@ function execute(cmd) {
101105
});
102106

103107
if (child.code !== 0) {
104-
log('Building encounted error while executing ' + cmd);
108+
logError('Building encounted error while executing ' + cmd);
105109
process.exit(1);
106110
}
107111

lib/commands/init.js

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,56 +32,52 @@ exports.run = function (options) {
3232

3333
// 如果初始化时带着初始化类型
3434
if (typeof process.argv[3] === 'string') {
35-
(function () {
36-
var checkConfigPkg = function checkConfigPkg(callback, packageName, registry) {
37-
if (!isInitReady) {
38-
(function () {
39-
var timeout = void 0;
40-
var child = shell.exec('npm view ' + packageName + ' --registry http://registry.npm.' + registry, { silent: true }, function (code, stdout, stderr) {
41-
if (stdout) {
42-
isInitReady = true;
43-
initProject(packageName, registry);
44-
}
45-
clearTimeout(timeout);
46-
callback(null);
47-
});
48-
49-
// 防止超时
50-
timeout = setTimeout(function () {
51-
child.kill('SIGINT');
52-
}, 20000);
53-
})();
54-
} else {
55-
callback(null, 'two');
56-
}
57-
};
58-
59-
var initParam = process.argv[3];
60-
var isInitReady = false;
61-
62-
spinner.start();
63-
spinner.text = 'Checking package ykit-config-' + initParam;
64-
async.series([
65-
// qnpm 寻找是否存在 @qnpm/ykit-config-xxx 的插件
66-
function (callback) {
67-
checkConfigPkg(callback, '@qnpm/ykit-config-' + initParam, 'corp.qunar.com');
68-
},
69-
// qnpm 寻找是否存在 ykit-config-xxx 的插件
70-
function (callback) {
71-
checkConfigPkg(callback, 'ykit-config-' + initParam, 'corp.qunar.com');
72-
},
73-
// cnpm 寻找是否存在 ykit-config-xxx 的插件
74-
function (callback) {
75-
checkConfigPkg(callback, 'ykit-config-' + initParam, 'taobao.org');
76-
}], function () {
77-
if (isInitReady) {
78-
spinner.stop();
79-
} else {
80-
spinner.text = 'Can\'t find package ykit-config-' + initParam;
81-
spinner.fail();
82-
}
83-
});
84-
})();
35+
var checkConfigPkg = function checkConfigPkg(callback, packageName, registry) {
36+
if (!isInitReady) {
37+
var timeout = void 0;
38+
var child = shell.exec('npm view ' + packageName + ' --registry http://registry.npm.' + registry, { silent: true }, function (code, stdout, stderr) {
39+
if (stdout) {
40+
isInitReady = true;
41+
initProject(packageName, registry);
42+
}
43+
clearTimeout(timeout);
44+
callback(null);
45+
});
46+
47+
// 防止超时
48+
timeout = setTimeout(function () {
49+
child.kill('SIGINT');
50+
}, 20000);
51+
} else {
52+
callback(null, 'two');
53+
}
54+
};
55+
56+
var initParam = process.argv[3];
57+
var isInitReady = false;
58+
59+
spinner.start();
60+
spinner.text = 'Checking package ykit-config-' + initParam;
61+
async.series([
62+
// qnpm 寻找是否存在 @qnpm/ykit-config-xxx 的插件
63+
function (callback) {
64+
checkConfigPkg(callback, '@qnpm/ykit-config-' + initParam, 'corp.qunar.com');
65+
},
66+
// qnpm 寻找是否存在 ykit-config-xxx 的插件
67+
function (callback) {
68+
checkConfigPkg(callback, 'ykit-config-' + initParam, 'corp.qunar.com');
69+
},
70+
// cnpm 寻找是否存在 ykit-config-xxx 的插件
71+
function (callback) {
72+
checkConfigPkg(callback, 'ykit-config-' + initParam, 'taobao.org');
73+
}], function () {
74+
if (isInitReady) {
75+
spinner.stop();
76+
} else {
77+
spinner.text = 'Can\'t find package ykit-config-' + initParam;
78+
spinner.fail();
79+
}
80+
});
8581
} else {
8682
// 只初始化一个空白项目
8783
initProject();
@@ -125,7 +121,7 @@ exports.run = function (options) {
125121
if (!UtilFs.fileExists(packageJsonPath)) {
126122
var writePackageJsonStream = create();
127123
writePackageJsonStream.on('finish', function () {
128-
log('Saved package.json file in ' + cwd);
124+
logInfo('Saved package.json file in ' + cwd);
129125
callback(null);
130126
});
131127
} else {
@@ -150,7 +146,7 @@ exports.run = function (options) {
150146
var stream = fs.createReadStream(configFilePath).pipe(replaceStream('#_name', projectName)).pipe(writeStream);
151147

152148
stream.on('finish', function () {
153-
log('Saved ' + configFileName + ' in ' + cwd);
149+
logInfo('Saved ' + configFileName + ' in ' + cwd);
154150
callback(null);
155151
});
156152
} else {
@@ -161,7 +157,7 @@ exports.run = function (options) {
161157
function setup(callback) {
162158
var initParams = process.argv.slice(4) || [];
163159
var setupCmd = 'ykit setup ' + initParams.join(' ');
164-
log('Running ' + setupCmd);
160+
logInfo('Running ' + setupCmd);
165161
shell.exec(setupCmd, { silent: false }, function (code, stdout, stderr) {
166162
callback(null);
167163
});

lib/commands/server.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ exports.run = function (options) {
339339
});
340340
} else {
341341
res.statusCode = 404;
342-
res.end('[ykit] - js入口未找到,请检查项目' + projectName + '的ykit配置文件.');
342+
res.end('[ykit] - js入口未找到,请检查项目' + projectName + '的 ykit 配置文件.');
343343
}
344344
}, 100);
345345
} else {
@@ -427,17 +427,17 @@ exports.run = function (options) {
427427
servers.forEach(function (server) {
428428
server.on('error', function (e) {
429429
if (e.code === 'EACCES') {
430-
warn('权限不足, 请使用sudo/管理员模式执行');
430+
logError('Permission denied. Please try running again as root/Administrator.');
431431
} else if (e.code === 'EADDRINUSE') {
432-
warn('端口 ' + server._port + ' 已经被占用, 请关闭占用该端口的程序或者使用其它端口.');
432+
logError('Port ' + server._port + ' has been opened by another application.');
433433
}
434434
process.exit(1);
435435
});
436436
server.listen(server._port, function () {
437437
var serverUrl = (server._isHttps ? 'https' : 'http') + '://127.0.0.1:' + server._port;
438438

439439
!server._isHttps && log('Starting up server, serving at: ' + options.cwd);
440-
log('Available on: ' + serverUrl.underline);
440+
logInfo('Available on: ' + serverUrl.underline);
441441
});
442442
});
443443

lib/global.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,21 @@ global.warn = function () {
4040
global.log = function () {
4141
info('[YKit] '.gray + Array.prototype.join.call(arguments, ' '));
4242
};
43+
global.logError = function () {
44+
info('[Error] '.red + Array.prototype.join.call(arguments, ' '));
45+
};
46+
global.logWarn = function () {
47+
info('[Warn] '.yellow + Array.prototype.join.call(arguments, ' '));
48+
};
49+
global.logInfo = function () {
50+
info('[Info] '.blue + Array.prototype.join.call(arguments, ' '));
51+
};
4352
global.logTime = function () {
4453
info(logSymbols.info + (' [' + moment().format('YY.MM.DD HH:mm:ss') + '] ').gray + Array.prototype.join.call(arguments, ' '));
4554
};
55+
global.logDoc = function () {
56+
info('[Doc] '.blue + 'Visit ' + Array.prototype.join.call(arguments, ' ').underline + ' for doc.');
57+
};
4658
global.logLinefeed = function () {
4759
info();
4860
};

lib/models/Project.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var Project = function () {
6565
});
6666
nextCommands.forEach(function (cmd) {
6767
if (existCommands.indexOf(cmd.name) > -1) {
68-
warn('\u547D\u4EE4 ' + cmd.name + ' \u5DF2\u7ECF\u5B58\u5728\uFF0C\u53EF\u80FD\u4F1A\u9020\u6210\u51B2\u7A81\u3002\n');
68+
logWarn('Command ' + cmd.name + ' exists. It may cause collision.');
6969
}
7070
});
7171

@@ -157,7 +157,8 @@ var Project = function () {
157157
pluginName = pluginItem.name ? pluginItem.name : '';
158158
_typeof(pluginItem.options) === 'object' && extend(options, pluginItem.options);
159159
} else {
160-
error(pluginItem.name + ' 插件配置有误,请检查 ykit.js');
160+
logError(pluginItem.name || 'Unknown' + ' plugin config error,please check local ykit.js.');
161+
logDoc('http://ued.qunar.com/ykit/plugins.html');
161162
process.exit(1);
162163
}
163164

@@ -201,7 +202,8 @@ var Project = function () {
201202
_module2.config.call(userConfig, options, _this2.cwd);
202203
}
203204
} else {
204-
error('没有找到 ' + pluginName + ' 配置插件,你可能需要安装相应 NPM 模块。\n' + ' 插件文档见: ' + 'http://ued.qunar.com/ykit/plugins.html'.underline);
205+
logError('Local ' + pluginName + ' plugin not found,you may need to intall it first.');
206+
logDoc('http://ued.qunar.com/ykit/plugins.html');
205207
process.exit(1);
206208
}
207209
}
@@ -255,7 +257,8 @@ var Project = function () {
255257
} else if (_typeof(configMethod.config) === 'object') {
256258
_handleConfigObj.bind(this)(configMethod.config);
257259
} else {
258-
error(this.configFile + ' 缺少 config 配置项,请参考文档 ' + 'http://ued.qunar.com/ykit/docs-配置.html'.underline);
260+
logError('Local ' + this.configFile + ' config not found.');
261+
logDoc('http://ued.qunar.com/ykit/docs-配置.html');
259262
process.exit(1);
260263
}
261264
}
@@ -386,7 +389,8 @@ var Project = function () {
386389
config = this.config.getConfig();
387390

388391
if (Object.keys(config.entry).length === 0) {
389-
warn('没有发现资源入口,也许你需要设置 exports。');
392+
logWarn('Local config exports aseets not found.');
393+
logDoc('http://ued.qunar.com/ykit/docs-%E9%85%8D%E7%BD%AE.html');
390394
process.exit(1);
391395
}
392396

@@ -481,8 +485,6 @@ var Project = function () {
481485
cc.exit();
482486
spinner.stop();
483487

484-
logTime('minify complete!');
485-
486488
// 更新 stats
487489
stats.compilation.assets = Object.keys(nextAssets).length > 0 ? nextAssets : originAssets;
488490

@@ -544,7 +546,7 @@ var Project = function () {
544546
});
545547

546548
var packDuration = Date.now() - packStartTime > 1000 ? Math.floor((Date.now() - packStartTime) / 1000) + 's' : Date.now() - packStartTime + 'ms';
547-
log('Packing Finished in ' + packDuration + '.\n');
549+
logInfo('Bundling Finishes in ' + packDuration + '.\n');
548550

549551
callback(err, stats);
550552
});

0 commit comments

Comments
 (0)