Repository navigation
8000 Expand file tree
/
Copy pathutil.js
More file actions
167 lines (137 loc) · 3.1 KB
/
Copy pathutil.js
File metadata and controls
167 lines (137 loc) · 3.1 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Module dependencies.
*/
var detect = require('language-classifier');
var resolve = require('path').resolve;
var exists = require('fs').existsSync;
var stat = require('fs').statSync;
var netrc = require('node-netrc');
var glob = require('glob').sync;
var path = require('path');
/**
* Language mapping.
*/
var langmap = {
javascript: 'js',
css: 'css'
};
/**
* Pull GH auth from ~/.netrc or $GH_TOKEN env.
*/
exports.token = function () {
if ('GH_TOKEN' in process.env) return process.env.GH_TOKEN;
var auth = netrc('api.github.com');
if (auth && auth.password) return auth.password;
return false;
};
/**
* Normalize entries list.
*
* - expand globs
* - expand directories into list of all nested files
*
* @param {Array:String}
* @return {Array:String}
*/
exports.entries = function (root, list) {
return list.filter(globs).reduce(function (memo, entry) {
if (isDir(path.join(root, entry))) {
return memo.concat(listFiles(root, entry));
} else {
return memo.concat(entry);
}
}, []);
};
/**
* Helper for collecting CLI params into a single array.
*
* @param {String} val
* @param {Array:String} memo
* @returns {Array:String}
*/
exports.collect = function (val, memo) {
val.split(',').forEach(function (val) {
memo.push(val);
});
return memo;
};
/**
* Find the root.
*
* @param {String} root
* @param {String}
*/
exports.findroot = function (root) {
var cwd = process.cwd();
if (root) return resolve(cwd, root);
var sep = path.sep;
var parts = cwd.split(sep);
var dir = cwd;
while (!exists(path.join(dir, 'component.json')) && parts.length > 1) {
parts.pop();
dir = parts.join(sep);
}
return parts.length <= 1
? cwd
: dir;
};
/**
* Detect the type of a source-file.
*
* @param {String} src
* @returns {String}
*/
exports.type = function (src) {
return langmap[detect(src)];
};
/**
* Retrieve an array of plugins from `--use`.
*
* @param {Array:String} plugins
* @return {Array:Function}
*/
exports.plugins = function (root, plugins) {
return plugins.map(function (plugin) {
var local = resolve(root, plugin);
var npm = resolve(root, 'node_modules', plugin);
var cwd = resolve(process.cwd(), 'node_modules', plugin);
var mod;
if (exists(local)) mod = require(local);
else if (exists(local + '.js')) mod = require(local);
else if (exists(npm)) mod = require(npm);
else mod = require(cwd);
return Array.isArray(mod) ? mod : mod();
}, []);
};
/**
* Filter out unexpanded globs.
*
* @param {String} entry
* @return {Boolean}
*/
function globs(path) {
return !/\*/.test(path);
}
/**
* Gets a list of all files within a directory recursively (and synchronously)
*
* @param {String} path
* @return {Array:String}
*/
function listFiles(root, dir, pattern) {
var opts = { cwd: root, nodir: true };
return glob(path.join(dir, pattern || '**/*'), opts);
}
/**
* Simple hueristic to check if `path` is a directory.
*
* @param {String} path
* @return {Boolean}
*/
function isDir(path) {
try {
return stat(path).isDirectory();
} catch (e) {
return false;
}
}