forked from scivey/bower-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (157 loc) · 4.52 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
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
168
169
170
171
172
173
// Generated by CoffeeScript 1.10.0
(function() {
var _, async, copyComponents, copyMain, copyMainTo, extractMain, flatsplat, fs, inBowerDir, joinPath, 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 _path, pathParts;
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);
});
};
joinPath = function(bowerPath, main) {
var _main, _match, relativeRegex;
_main = main;
relativeRegex = /^\.\/(.+)$/im;
_match = relativeRegex.exec(_main);
if (_match != null) {
_main = _match[1];
}
return path.join(path.dirname(bowerPath), main);
};
extractMain = function(filePath, data, cb) {
var _main, mainPath, p;
_main = data.main;
if (_main == null) {
return null;
}
mainPath = (function() {
var i, len, results;
if (Array.isArray(_main)) {
results = [];
for (i = 0, len = _main.length; i < len; i++) {
p = _main[i];
results.push(joinPath(filePath, p));
}
return results;
} else {
return [joinPath(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.length > 0) {
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
});
});
}
});
};
copyMain = function(scriptRef, outputDir, cb) {
var cbHandler, copyScript, filesToCopy, scriptPaths;
scriptPaths = scriptRef.main;
filesToCopy = scriptPaths.length;
copyScript = function(scriptRef, outputDir, cb) {
var scriptPath;
return scriptPath = scriptRef.main;
};
cbHandler = function(scriptPath, outputPath) {
return function(err) {
if (filesToCopy === 1) {
cb(null, {
src: scriptPath,
dest: outputPath
});
}
return filesToCopy--;
};
};
return scriptPaths.forEach(function(p) {
var outputPath;
outputPath = path.join(outputDir, path.basename(p));
return fs.copy(p, outputPath, cbHandler(p, outputPath));
});
};
copyMainTo = function(outputDir) {
return function(scriptRef, cb) {
return copyMain(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, copyMainTo(_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);