-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
98 lines (88 loc) · 2.75 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
var path = require('path');
var fs = require('fs');
var _ = require('lodash');
//
// determine bower components home dir, maybe need to get dir name from '.bowerrc'
//
function componentsHome(workdir) {
var bowerrc = path.join(workdir, '.bowerrc'), bowerConfig = {};
if (fs.existsSync(bowerrc)) bowerConfig = JSON.parse(fs.readFileSync(bowerrc));
return path.join(workdir, bowerConfig.directory || 'bower_components');
}
//
// extract component name from inputting rawname
//
function componentName(rawname) {
var name = rawname.split(':')[0],
index = name.replace('\\', '/').indexOf('/');
return (index > 0) ? name.substring(0, index) : name;
}
//
// get bower components names
//
function componentNames(workdir) {
workdir = workdir || process.cwd();
var bowerJson = require(path.join(workdir, 'bower.json'));
return _(Object.keys(bowerJson.dependencies || {}))
.union(Object.keys(bowerJson.devDependencies || {}))
.value();
}
exports.componentNames = componentNames;
//
// resolve and return entry file's full path w/ deps for specified component
//
function resolve(name, workdir, mainfiles) {
workdir = workdir || process.cwd();
var compName = componentName(name),
subname = name.substring(compName.length + 1),
basedir = path.join(componentsHome(workdir), compName),
bowerJson = {},
mainfile;
if (mainfiles[compName]) {
mainfile = mainfiles[compName];
} else {
var bowerPath = path.join(basedir, 'bower.json');
var exists = fs.existsSync(bowerPath);
if (!exists) {
bowerPath = path.join(basedir, '.bower.json');
exists = fs.existsSync(bowerPath);
}
if (exists) {
bowerJson = require(bowerPath);
mainfile = Array.isArray(bowerJson.main)
? bowerJson.main.filter(function(file) { return /\.js$/.test(file); })[0]
: bowerJson.main;
}
}
var entryPath = '';
if (subname && subname.length > 0) {
var subpath = mainfile && path.join(basedir, mainfile, '..', subname);
if (subpath && (fs.existsSync(subpath) || fs.existsSync(subpath + '.js'))) {
entryPath = path.join(basedir, mainfile, '..', subname);
} else {
entryPath = path.join(basedir, subname);
}
} else {
if (mainfile) {
entryPath = path.join(basedir, mainfile);
} else {
if (fs.existsSync(path.join(basedir, "index.js"))) {
entryPath = path.join(basedir, "index.js");
} else {
entryPath = path.join(basedir, compName);
}
}
}
var deps = (subname && subname.length > 0) ? [] // don't resolve deps for partial reference
: _(bowerJson.dependencies || {})
.map(function(ver, name) {
return resolve(name, workdir, mainfiles);
}).flatten().value();
if (!entryPath) throw new Error('Unable to resolve mainfile for component ' + name);
return [{
name: name,
path: entryPath
}].concat(deps);
}
exports.resolve = resolve;