-
Notifications
You must be signed in to change notification settings - Fork 2
/
fenestrate.js
290 lines (267 loc) · 10.8 KB
/
fenestrate.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/** fenestrate npm package utility for windows compat
* (c) 2014 James Zetlen, every single right reserved
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
var fs = require('fs'),
path = require('path'),
rimraf = require('rimraf'),
childProcess = require("child_process"),
cmdArgs = process.argv.slice(2),
cmd = cmdArgs.shift(),
modulePath = cmdArgs.shift(),
windows = /^win/.test(process.platform),
helpText = fs.readFileSync(path.resolve(__dirname, './README.md'), 'utf-8').split('<!-- cut here -->').shift(),
dependencyTypes = ["dependencies","devDependencies"];
if (cmd === "help" || cmd === "-h" || !cmd) {
log(helpText);
process.exit(0);
}
if (cmdArgs > 1) {
die("\n Multiple non-flag arguments detected. You must provide only one path.");
}
modulePath = path.resolve(modulePath || "./");
var rootPkg = require(path.resolve(modulePath, './package.json'));
if (!fs.existsSync(modulePath)) {
die('Path "' + modulePath + '" does not exist.');
}
if (!fs.existsSync(path.resolve(modulePath, './node_modules')) && cmd === "make") {
die('The node_modules directory is not present in "' + modulePath + '". Cannot run `fenestrate make` until the package has been installed.');
}
function log(str) {
process.stderr.write(str + "\n");
}
function flattener(conf, dep, declared, depType) {
if (conf && !conf.missing && conf.from) {
var sv = conf.from.split('@').pop();
if (!declared.hasOwnProperty(dep)) {
log('Adding dependency "' + dep + '" at semver "' + sv + '" to ' + depType);
declared[dep] = sv;
} else {
log('Skipping dependency "' + dep + '" at semver "' + sv + '" because it already exists at "' + declared[dep] + '" in ' + depType);
}
if (conf.dependencies) flattenDependencies(declared, conf.dependencies, depType);
}
}
function flattenDependencies(declared, resolved, depType) {
for (var d in resolved) {
if (resolved.hasOwnProperty(d)) {
flattener(resolved[d], d, declared, depType);
}
}
return declared;
}
function reinstallNodeModules(p, prod, cb) {
rimraf(path.resolve(p, "./node_modules"), function(err) {
var cmd = prod ? "npm install --production" : "npm install",
args = ((windows ? "cmd /c " : "") + cmd).split(' ');
if (err) {
cb(err);
} else {
childProcess.spawn(args[0], args.slice(1), { cwd: p, stdio: 'inherit' }).on('close', function(code) {
cb(null, code);
});
}
});
}
function die(why) {
console.error(why);
process.exit(1);
}
function clone(obj) {
if (!obj) return obj;
return JSON.parse(JSON.stringify(obj));
}
function shunt(modPath, restore, prod) {
var pkg = require(path.resolve(modPath, './package.json'));
var f = pkg.__fenestrate,
rewriteFrom = restore ? "previous" : "flattened",
saveTo = restore ? "flattened" : "previous";
if (prod) dependencyTypes.pop();
if (f && !restore && f.previous) {
log('This package is already in a transformed state. Run `fenestrate restore` before running `fenestrate rewrite` again.')
process.exit(0);
}
if (f && restore && !f.previous) {
die('This package is not in a transformed state and cannot be restored.')
}
if (!f || !f[rewriteFrom]) {
log('A ' + rewriteFrom + ' configuration was never added to this package.');
process.exit(0);
}
f[saveTo] = {};
dependencyTypes.forEach(function(type) {
if (pkg[type] && !f[rewriteFrom][type]) {
die('The package.json file has a "' + type + '" configuration, but there is no "' + type + '" present in the fenestrate config. Run `fenestrate make` again.');
}
f[saveTo][type] = clone(pkg[type]);
pkg[type] = clone(f[rewriteFrom][type]);
});
delete f[rewriteFrom];
log("Writing " + rewriteFrom + " package.json...");
fs.writeFileSync(path.resolve(modPath, './package.json'), JSON.stringify(pkg, null, 2));
log("Successfully saved " + rewriteFrom + " package.json.");
}
var commands = {
make: function(modPath, dry, cb) {
var pkg = require(path.resolve(modPath, './package.json'));
var f = pkg.__fenestrate;
if (!f) {
f = pkg.__fenestrate = {
comment: "This package.json file was modified by the `fenestrate` utility for Windows compatibility. The flattened dependency graph was flattened to accommodate the 260-character limit on Windows file paths. If there is a `previous` dependency graph, this package has already been transformed."
};
}
if (f.previous) {
die('This package is already in a transformed state. Run `fenestrate restore` before running `fenestrate make` again.')
}
log('Reading installed dependencies for ' + modPath);
childProcess.exec("npm ls --json", { maxBuffer: 1024 * 4096, cwd: modPath }, function(err, res) {
if (err && !res) {
console.error("Failed to do initial listing of dependencies.");
die(err.message);
}
log('Successfully read dependencies.');
f.flattened = {};
res = JSON.parse(res);
dependencyTypes.forEach(function(type) {
if (pkg[type]) {
f.flattened[type] = flattenDependencies(clone(pkg[type]), res.dependencies, type);
}
});
var pkgString = JSON.stringify(pkg, null, 2);
if (dry) {
log(pkgString);
log('\n\nDry run only, making no changes.');
} else {
fs.writeFileSync(path.resolve(modPath, './package.json'), pkgString);
log('Updated package.json. This package can now be rewritten using `fenestrate rewrite`.');
}
if (cb) {
cb();
} else {
process.exit(0);
}
});
},
activate: function(modPath, prod) {
shunt(modPath, false, prod);
},
deactivate: function(modPath, prod) {
shunt(modPath, true, prod);
},
rewrite: function(modPath, restore, prod) {
var pkg = require(path.resolve(modPath, './package.json'));
log("Rewriting node_modules directory...")
reinstallNodeModules(modPath, prod, function(err, code) {
if (code !== 0) {
die("Error building " + rewriteFrom + " node_modules directory. Could not continue.");
}
if (restore) {
log('Done restoring original node_modules directory structure. Defenestration complete.')
process.exit(0);
}
log("Installed node_modules directory. Recursing into it; hold tight.");
(function rewriteDeep(mPath, levels, outerCb) {
var mNMPath = path.resolve(mPath, './node_modules'),
levelsUp = 0;
fs.readdirSync(mNMPath).reverse().reduceRight(function(cb, subModule) {
return function() {
var subModulePath = path.resolve(mNMPath, subModule);
if (
fs.statSync(subModulePath).isDirectory() &&
fs.existsSync(path.resolve(subModulePath, './node_modules')) &&
fs.existsSync(path.resolve(subModulePath, './package.json'))
) {
// it's a real module
if (dependencyTypes.some(function(type){
return pkg[type] && pkg[type].hasOwnProperty(subModule)
}) && levels > 2) {
// it's already present in the root package. we don't need semver
// to know that npm left it because it's an incompatible version
//
// (and this won't ever work at the first layer of packages, so we count up)
log('Detected that "' + subModule + '" has mutually incompatible versions in the tree, resulting in excess depth. Checking if we can make it shallower...');
var ancestorToMoveTo = mNMPath;
function logLookin() {
log('Trying to find ' + subModule + ' above ' + ancestorToMoveTo);
return true;
}
while (!(logLookin() && fs.existsSync(path.resolve(ancestorToMoveTo, '../../', subModule)))) { // go up until it's there
ancestorToMoveTo = path.resolve(ancestorToMoveTo, '../../');
if (++levelsUp > levels) {
log("Didn't find the conflicting dependency higher up and we're at " + ancestorToMoveTo + ". Something's weird about " + subModule);
return cb();
}
}
log('Found the shallowest available directory for moving ' + subModule + ', at ' + ancestorToMoveTo);
var newSubModulePath = path.resolve(ancestorToMoveTo, subModule);
fs.renameSync(subModulePath, newSubModulePath)
log("Successfully moved " + subModulePath + " to " + newSubModulePath);
subModulePath = newSubModulePath;
}
rewriteDeep(subModulePath, ((levels - levelsUp) + 1), cb);
} else {
cb();
}
};
}, outerCb)();
})(modPath, 1, function() {
log("Done walking the tree for deep duplicates. Fenestration complete.")
process.exit(0);
});
});
},
'rewrite-full': function(modPath) {
commands.activate(modPath);
commands.rewrite(modPath);
},
'rewrite-prod': function(modPath) {
commands.rewrite(modPath, false, true);
},
'rewrite-prod-full': function(modPath) {
commands.activate(modPath, true);
commands.rewrite(modPath, false, true);
},
restore: function(modPath) {
commands.rewrite(modPath, true);
},
'restore-full': function(modPath) {
commands.deactivate(modPath);
commands.rewrite(modPath, true);
},
remove: function(modPath) {
var pkg = require(path.resolve(modPath, './package.json'));
if (!pkg.__fenestrate) {
die("Cannot remove fenestrate config because there isn't one present in package.json.")
}
if (pkg.__fenestrate.previous) {
die("Cannot remove fenestrate config because this package is in a transformed state. Run `fenestrate restore` before `fenestrate remove`.");
}
log("Removing fenestrate config from package.json...");
delete pkg.__fenestrate;
fs.writeFileSync(path.resolve(modPath, './package.json'), JSON.stringify(pkg, null, 2));
log("Saved package.json.");
process.exit(0);
},
"dry-run": function(modPath) {
commands.make(modPath, true);
}
};
if (!commands[cmd]) {
console.error("Unrecognized command " + cmd);
log(helpText);
process.exit(1);
}
commands[cmd](modulePath);