-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
142 lines (127 loc) · 3.72 KB
/
index.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
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
// Generated by CoffeeScript 1.6.3
(function() {
var async, copyComponents, copyScript, copyScriptTo, extractMain, flatsplat, fs, inBowerDir, mainFromFolder, path, readJSON, resolveComponents, _,
__slice = [].slice;
fs = require("fs-extra");
_ = require("underscore");
path = require("path");
async = require("async");
flatsplat = function(list) {
if (list.length === 1 && _.isArray(list[0])) {
return list[0];
} else {
return list;
}
};
inBowerDir = function() {
var pathParts, _path;
pathParts = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
pathParts = ["bower_components/"].concat(flatsplat(pathParts));
_path = path.join.apply(null, pathParts);
return _path;
};
readJSON = function(filePath, cb) {
return fs.readFile(filePath, "utf8", function(err, res) {
var _json;
_json = JSON.parse(res);
return cb(null, _json);
});
};
extractMain = function(filePath, data, cb) {
var mainPath, relativeRegex, _main, _match, _pd;
_main = data.main;
if (_main == null) {
return null;
}
relativeRegex = /^\.\/(.+)$/im;
_match = relativeRegex.exec(_main);
if (_match != null) {
_main = _match[1];
}
_pd = path.dirname(filePath);
mainPath = path.join(path.dirname(filePath), _main);
return mainPath;
};
mainFromFolder = function(folderName, cb) {
var _filePath;
_filePath = inBowerDir(folderName, ".bower.json");
return readJSON(_filePath, function(err, pkg) {
var mainPath;
mainPath = extractMain(_filePath, pkg);
if (mainPath != null) {
return cb(null, {
component: folderName,
main: mainPath
});
} else {
_filePath = inBowerDir(folderName, "package.json");
return readJSON(_filePath, function(err, pkg) {
mainPath = extractMain(_filePath, pkg);
return cb(null, {
component: folderName,
main: mainPath
});
});
}
});
};
copyScript = function(scriptRef, outputDir, cb) {
var outputPath, scriptPath;
scriptPath = scriptRef.main;
outputPath = path.join(outputDir, scriptRef.component) + ".js";
return fs.copy(scriptPath, outputPath, function(err) {
return cb(null, {
src: scriptPath,
dest: outputPath
});
});
};
copyScriptTo = function(outputDir) {
return function(scriptRef, cb) {
return copyScript(scriptRef, outputDir, cb);
};
};
copyComponents = function(options, cb) {
var _copyFn, _opts;
_opts = _.clone(options);
if (_opts.src == null) {
_opts.src = "./bower_components";
}
if (_opts.dest == null) {
throw new Error("No destination specified.");
}
_copyFn = function() {
return fs.readdir(_opts.src, function(err, folders) {
return async.map(folders, mainFromFolder, function(err, completed) {
return async.map(completed, copyScriptTo(_opts.dest), function(err, copied) {
return cb(null, copied);
});
});
});
};
return fs.exists(_opts.dest, function(exists) {
if (exists) {
return _copyFn();
} else {
return fs.mkdirs(_opts.dest, function(err) {
return _copyFn();
});
}
});
};
resolveComponents = function(bowerDir, cb) {
if (_.isFunction(bowerDir)) {
cb = bowerDir;
bowerDir = "./bower_components";
}
return fs.readdir(bowerDir, function(err, folders) {
return async.map(folders, mainFromFolder, function(err, resolved) {
return cb(null, resolved);
});
});
};
module.exports = {
copyComponents: copyComponents,
resolveComponents: resolveComponents
};
}).call(this);