Skip to content

Commit 043446c

Browse files
committed
#26 修改默认设置
1 parent 263daab commit 043446c

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

lib/cli.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ cli.run = function (env) {
8686
program
8787
.command('profile [query]')
8888
.alias('p')
89-
.option('-u, --update', 'check available update(s)')
89+
.option('-d, --default', 'configure default settings')
9090
.description(fecom.i18n('MANAGE_USER_PROFILE'))
9191
.action(function () {
9292
var profile = require('./profile');
9393
var args = [].slice.call(arguments);
9494
var options = args.pop();
9595
var query = args[0];
96-
options = _(options).pick(['update']).set('cwd', env.cwd).value();
96+
options = _(options).pick(['default']).set('cwd', env.cwd).value();
9797
profile(query, options);
9898
});
9999
/**

lib/component/Component.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ Component.prototype = {
101101
var self = this;
102102

103103
function getDependencies(parsed) {
104-
var componentJson = path.join(fecom.config.dir, parsed.name, 'component.json');
104+
var componentDir = path.join(fecom.config.dir, parsed.name);
105+
var componentJson = path.join(componentDir, 'component.json');
105106
var tree = {};
106107
var json, dependenciesMap;
107108

109+
if (!fs.existsSync(componentDir)) {
110+
tree.props = parsed;
111+
tree.subNodes = [];
112+
return tree;
113+
}
114+
108115
if (!fs.existsSync(componentJson)) {
109116
throw new Error('Missing component.json');
110117
}
@@ -141,6 +148,7 @@ Component.prototype = {
141148
var getChildren = function (tree) {
142149
var parsed = tree.props;
143150
var isRoot = (parsed.name === fecom.config.name && parsed.owner === fecom.profile.username);
151+
144152
if (isRoot && !tree.subNodes.length) {
145153
return fecom.async(tree);
146154
}

lib/fecom.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var util = require('util');
77
var _ = require('lodash');
88
var fs = require('graceful-fs');
99
var osLocale = require('os-locale');
10+
var ini = require('ini');
1011
var YAML = require('yamljs');
1112

1213
var parse = require('./util/parse');
@@ -27,6 +28,19 @@ function placeholder(value, patterns) {
2728
return placeholder(value.replace(regexp, patterns.pop()), patterns);
2829
}
2930

31+
var defaults = {}, profile;
32+
var profileFile = path.join(getUserHome(), '.fecomrc');
33+
34+
try {
35+
profile = ini.parse(fs.readFileSync(profileFile, 'utf-8'));
36+
} catch (err) {
37+
logger.error(err);
38+
}
39+
40+
if (fs.existsSync(profileFile)) {
41+
defaults = profile.defaults;
42+
}
43+
3044
var Fecom = function () {
3145
this.root = '';
3246
this.componentRoot = '';
@@ -40,15 +54,14 @@ var Fecom = function () {
4054

4155
util.inherits(Fecom, EventEmitter);
4256

43-
Fecom.defaults = {
44-
config: {
45-
name: path.basename(process.cwd()),
46-
dir: 'components',
47-
protocol: 'gitlab',
48-
owner: 'fecom-fe',
49-
domain: 'http://gitlab.58corp.com'
50-
}
51-
};
57+
Fecom.defaults = {};
58+
Fecom.defaults.config = _.assign({
59+
name: path.basename(process.cwd()),
60+
dir: 'components',
61+
protocol: 'gitlab',
62+
owner: 'fecom-fe',
63+
domain: 'http://gitlab.58corp.com'
64+
}, defaults);
5265

5366
Fecom.prototype = _.assign(Fecom.prototype, {
5467
constructor: Fecom,

lib/profile.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,18 @@ module.exports = function (query, options) {
2525
// Print user profile
2626
var obj = url.parse('?' + query, true);
2727
var profileFile = path.join(getUserHome(), '.fecomrc');
28-
var queryObj = _.pick(obj.query, ['username', 'email', 'token']);
29-
profile = _.assign({}, profile, queryObj);
28+
var queryObj;
29+
30+
if (options.default) {
31+
queryObj = _.pick(obj.query, ['dir', 'protocol', 'domain', 'owner']);
32+
profile.defaults = _.assign({}, profile.defaults, queryObj);
33+
} else {
34+
queryObj = _.pick(obj.query, ['username', 'email', 'token']);
35+
profile = _.assign({}, profile, queryObj);
36+
}
37+
3038
fs.writeFileSync(profileFile, ini.stringify(profile));
39+
3140
}
3241

3342
return profile;

lib/tree.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ module.exports = function (semList, options) {
9494
return treeModel;
9595
})
9696
.catch(function (err) {
97-
spinner.stop(true);
97+
98+
if (spinner) {
99+
spinner.stop(true);
100+
}
101+
98102
fecom.errorHandler(err);
99103
return {};
100104
});

0 commit comments

Comments
 (0)