Skip to content

Commit 587e4a5

Browse files
committed
feat(cli): 添加 config 命令用来进行全局配置
1 parent ce0652b commit 587e4a5

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var cli = module.exports = {
5656
help: function help() {
5757
info(helpTitle);
5858
Manager.getProject(process.cwd()).commands.forEach(function (command) {
59-
var commandStr = rightPad(rightPad(command.name, 8) + (command.abbr || ''), 35);
59+
var commandStr = rightPad(rightPad(command.name, 8) + (command.abbr || ''), 25);
6060
info(' ' + commandStr + ' # ' + (command.module.usage || ''));
6161
});
6262
info();

lib/commands/config.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict';
2+
3+
exports.usage = '操作全局配置';
4+
exports.abbr = 'c';
5+
6+
exports.setOptions = function () {};
7+
8+
exports.run = function () {
9+
var operation = process.argv[3];
10+
11+
var globalConfig = JSON.parse(fs.readFileSync(YKIT_RC, { encoding: 'utf8' }));
12+
13+
if (operation === 'list') {
14+
log('config file path: ' + YKIT_RC + '\n');
15+
log('global configs');
16+
17+
Object.keys(globalConfig).forEach(function (configKey) {
18+
if (configKey !== 'commands' && configKey !== 'configs') {
19+
log(rightPad(configKey, 6) + ' = ' + globalConfig[configKey]);
20+
}
21+
});
22+
23+
process.exit(0);
24+
}
25+
26+
if (operation === 'set') {
27+
var settingKey = process.argv[4];
28+
var settingValue = process.argv[5];
29+
30+
if (settingKey && settingValue) {
31+
globalConfig[settingKey] = settingValue;
32+
fs.writeFileSync(YKIT_RC, JSON.stringify(globalConfig, null, ' '), { encoding: 'utf8' });
33+
process.exit(0);
34+
}
35+
}
36+
37+
error('缺少参数或参数有误,命令格式为: ');
38+
error('ykit config list');
39+
error('ykit config set <key> <value>\n');
40+
process.exit(1);
41+
};

src/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ let cli = module.exports = {
5454
help: () => {
5555
info(helpTitle);
5656
Manager.getProject(process.cwd()).commands.forEach((command) => {
57-
const commandStr = rightPad(rightPad(command.name, 8) + (command.abbr || ''), 35);
57+
const commandStr = rightPad(rightPad(command.name, 8) + (command.abbr || ''), 25);
5858
info(` ${commandStr} # ${command.module.usage || ''}`);
5959
});
6060
info();

src/commands/config.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
exports.usage = '操作全局配置';
4+
exports.abbr = 'c';
5+
6+
exports.setOptions = () => {
7+
};
8+
9+
exports.run = () => {
10+
const operation = process.argv[3];
11+
12+
let globalConfig = JSON.parse(fs.readFileSync(YKIT_RC, {encoding: 'utf8'}));
13+
14+
if(operation === 'list') {
15+
log(`config file path: ${YKIT_RC}\n`);
16+
log('global configs');
17+
18+
Object.keys(globalConfig).forEach((configKey) => {
19+
if(configKey !== 'commands' && configKey !== 'configs') {
20+
log(`${rightPad(configKey, 6)} = ${globalConfig[configKey]}`);
21+
}
22+
});
23+
24+
process.exit(0);
25+
}
26+
27+
if(operation === 'set') {
28+
const settingKey = process.argv[4];
29+
const settingValue = process.argv[5];
30+
31+
if(settingKey && settingValue) {
32+
globalConfig[settingKey] = settingValue;
33+
fs.writeFileSync(YKIT_RC, JSON.stringify(globalConfig, null, ' '), {encoding: 'utf8'});
34+
process.exit(0);
35+
}
36+
}
37+
38+
error('缺少参数或参数有误,命令格式为: ');
39+
error('ykit config list');
40+
error('ykit config set <key> <value>\n');
41+
process.exit(1);
42+
};

0 commit comments

Comments
 (0)