Skip to content

Commit dda0c46

Browse files
committed
refactory init
1 parent df93016 commit dda0c46

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

lib/commands/init.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ var commander = require('commander');
66
var JSONConfig = require('../opm/Package/configloader/json.js').JSONConfig;
77

88
function init(dir, options) {
9+
init.run(dir, options);
10+
}
911

10-
init.root = path.join(process.cwd(), dir);
12+
// 用独立的方法运行init逻辑,保证扩展不必须要替换init方法就能实现扩展
13+
init.run = function(dir, options) {
14+
init.root = path.resolve(process.cwd(), dir);
1115

1216
async.auto(init.tasks, function(err) {
1317
if (err) {
@@ -16,33 +20,40 @@ function init(dir, options) {
1620
console.log('inited');
1721
}
1822
});
19-
}
23+
};
2024

21-
init.infoCollectors = [
25+
init.infoDefaults = {
26+
name: function() {
27+
return path.basename(init.root);
28+
},
29+
version: '0.0.1'
30+
};
31+
32+
init.infoCollectors = {
2233
// 名称
23-
function(callback) {
24-
var defaultValue = path.basename(init.root);
34+
name: function(callback) {
35+
var defaultValue = init.infoDefaults['name']();
2536
commander.prompt('name(' + defaultValue + '): ', function(input) {
2637
init.config.setProperty('name', input || defaultValue);
2738
callback();
2839
});
2940
},
3041
// 版本
31-
function(callback) {
42+
version: function(callback) {
3243
var defaultValue = '0.0.1';
3344
commander.prompt('version(' + defaultValue + '): ', function(input) {
3445
init.config.setProperty('version', input || defaultValue)
3546
callback();
3647
});
3748
},
3849
// 描述
39-
function(callback) {
50+
description: function(callback) {
4051
commander.prompt('description: ', function(input) {
4152
init.config.setProperty('description', input);
4253
callback();
4354
});
4455
}
45-
];
56+
};
4657

4758
init.tasks = {
4859
'check_inited': function(callback, results) {
@@ -71,7 +82,7 @@ init.tasks = {
7182
}],
7283
// 初始化package.json
7384
'save_package': ['collect_info', 'create_path', function(callback, results) {
74-
init.config.save(path.join(root, 'package.json'), callback);
85+
init.config.save(path.join(init.root, 'package.json'), callback);
7586
}]
7687
};
7788

0 commit comments

Comments
 (0)