Skip to content

Commit 562e036

Browse files
committed
#53 fecom link
1 parent 57a437e commit 562e036

File tree

4 files changed

+112
-4
lines changed

4 files changed

+112
-4
lines changed

i18n/lang.yml

+29-1
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,32 @@ SEARCH_RESULTS:
232232

233233
ERROR_INVALID_COMPONENT:
234234
en_US: "Invalid component: {0}"
235-
zh_CN: "无效的组件: {0}"
235+
zh_CN: "无效的组件: {0}"
236+
237+
LINK_COMPONENT:
238+
en_US: link component
239+
zh_CN: 链接组件
240+
241+
ERROR_NO_LINK_FOUND:
242+
en_US: No link found for {0}
243+
zh_CN: 未找到{0}的链接
244+
245+
ERROR_NOT_VALID_COMPONENT_DIRECTORY:
246+
en_US: Not a valid component directory
247+
zh_CN: 不是有效的组件目录
248+
249+
LINK_REGISTERED:
250+
en_US: "Link registered: {0} -> {1}"
251+
zh_CN: "注册链接: {0} -> {1}"
252+
253+
ERROR_LINK_TARGET_NOT_EXIST:
254+
en_US: Link target does not exist
255+
zh_CN: 链接目标不存在
256+
257+
LINKED_COMPONENT:
258+
en_US: "Linked component: {0}"
259+
zh_CN: "链接的组件: {0}"
260+
261+
LINK_PATH_EXISTS:
262+
en_US: Link path already exists
263+
zh_CN: 链接路径已经存在

lib/cli.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,25 @@ cli.run = function (env) {
106106
var info = require('./info');
107107
var args = [].slice.call(arguments);
108108
var options = args.pop();
109-
var semList = args[0];
109+
var semantic = args[0];
110+
options = {};
111+
// fecom.logger.info('Listing component versions...');
112+
info(semantic, options);
113+
});
114+
/**
115+
* Linking component
116+
*/
117+
program
118+
.command('link [component]')
119+
.description(fecom.i18n('LINK_COMPONENT'))
120+
.action(function () {
121+
var link = require('./link');
122+
var args = [].slice.call(arguments);
123+
var options = args.pop();
124+
var semantic = args[0];
110125
options = {};
111126
// fecom.logger.info('Listing component versions...');
112-
info(semList, options);
127+
link(semantic, options);
113128
});
114129
/**
115130
* Searching components

lib/link.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
var _ = require('lodash');
6+
var fs = require('graceful-fs');
7+
var ini = require('ini');
8+
var jsonfile = require('jsonfile');
9+
var Promise = require('bluebird');
10+
11+
var fecom = require('./fecom');
12+
13+
function getUserHome() {
14+
return process.env[('win32' === process.platform) ? 'USERPROFILE' : 'HOME'];
15+
}
16+
17+
module.exports = function (semantic) {
18+
var componentJson = path.join(fecom.root, 'component.json');
19+
var profile = fecom.profile;
20+
var profileFile = path.join(getUserHome(), '.fecomrc');
21+
var linkMap = _.assign({}, profile.link);
22+
23+
return Promise
24+
.try(function () {
25+
26+
if (!semantic) {
27+
28+
if (!fs.existsSync(componentJson)) {
29+
throw new Error(fecom.i18n('ERROR_NOT_VALID_COMPONENT_DIRECTORY'));
30+
}
31+
32+
var json = jsonfile.readFileSync(componentJson);
33+
var componentName = json.name;
34+
linkMap[componentName] = fecom.root;
35+
profile.link = linkMap;
36+
fs.writeFileSync(profileFile, ini.stringify(profile));
37+
fecom.logger.info(fecom.i18n('LINK_REGISTERED', componentName.blue, fecom.root));
38+
} else {
39+
var parsed = fecom.parse(semantic);
40+
41+
if (!linkMap.hasOwnProperty(parsed.name) || !linkMap[parsed.name]) {
42+
throw new Error(fecom.i18n('ERROR_NO_LINK_FOUND', parsed.name));
43+
}
44+
45+
var target = linkMap[parsed.name];
46+
var linkPath = path.join(fecom.componentRoot, parsed.name);
47+
48+
if (!fs.existsSync(target)) {
49+
throw new Error(fecom.i18n('ERROR_LINK_TARGET_NOT_EXIST'));
50+
}
51+
52+
if (!fs.existsSync(fecom.componentRoot)) {
53+
fs.mkdirSync(fecom.componentRoot);
54+
}
55+
56+
if (fs.existsSync(linkPath)) {
57+
throw new Error(fecom.i18n('LINK_PATH_EXISTS'));
58+
}
59+
60+
fs.symlinkSync(target, linkPath);
61+
fecom.logger.info(fecom.i18n('LINKED_COMPONENT', parsed.name.blue));
62+
}
63+
})
64+
.catch(fecom.errorHandler);
65+
66+
};

lib/profile.js

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ module.exports = function (query, options) {
3636
}
3737

3838
fs.writeFileSync(profileFile, ini.stringify(profile));
39-
4039
}
4140

4241
return profile;

0 commit comments

Comments
 (0)