-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
255 lines (216 loc) · 7.12 KB
/
index.js
File metadata and controls
255 lines (216 loc) · 7.12 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
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
#!/usr/bin/env node
//
// A utility for saving shrinkwrapped npm packages away and installing them
// later.
//
// TODO: Show a warning if no '_resolved' is present due to npm bug
// (https://github.com/npm/npm/issues/3581).
var fs = require('fs'),
path = require('path'),
async = require('async'),
http = require('http'),
ecstatic = require('ecstatic'),
httpRequest = require('http-request'),
traverse = require('traverse'),
mkdirp = require('mkdirp'),
portfinder = require('portfinder'),
glob = require('glob'),
spawn = require('child_process').spawn,
chalk = require('chalk');
var optimist = require('optimist')
.alias('s', 'store')
.alias('p', 'port')
.alias('a', 'address')
.alias('h', 'help')
argv = optimist.argv;
var findRootPath = function() {
var cwd = path.resolve('.'),
parts = cwd.split(/\//);
for (var end = parts.length; end > 0; end -= 1) {
var packageFile = parts.slice(0, end).concat('package.json').join(path.sep);
if (fs.existsSync(packageFile)) {
return path.dirname(packageFile);
}
}
};
// Like path.join, but ignores the first path if the second is absolute.
var pathJoin = function(p1, p2) {
if (p2.length > 0 && p2[0] == path.sep)
return p2;
else
return path.join(p1, p2);
};
var rootPath = findRootPath(),
inRoot = function(p) { return pathJoin(rootPath, p); };
var getStorePath = function() {
if (!rootPath) {
console.error("Unable to find 'package.json'. Are you in the right directory?");
process.exit(1);
}
var packageJson = JSON.parse(fs.readFileSync(inRoot('package.json')));
return argv.store || inRoot((packageJson.shrinkwrapper || {}).store || './packages');
};
var urlBasename = function(url) {
return path.basename(require('url').parse(url).path);
};
var download = function(url, dest, next) {
next = next || function() {};
var filename = urlBasename(url);
if (filename == '') return;
filename = path.join(dest, filename);
fs.exists(filename, function(exists) {
if (exists) {
next(null);
return;
} else {
console.log(chalk.green('http'), chalk.magenta('GET'), url);
httpRequest.get(url, filename, function(err, res) {
if (err) {
console.error(err);
next(err);
return;
}
console.log(chalk.green('http'), chalk.magenta(res.code), url);
next(null);
});
}
});
};
var getBackupFilename = function(filename) {
return path.join(path.dirname(filename), '.' + path.basename(filename) + '.bak');
};
// Asynchronously applies a mapping function to values of the given field in
// the identified JSON file. Makes a backup copy of the file first.
var mapFile = function(filename, field, fn, next) {
var backupFilename = getBackupFilename(filename);
fs.rename(filename, backupFilename, function(err) {
if (err) return next(err);
fs.readFile(backupFilename, function(err, data) {
if (err) return next(err);
var data = JSON.parse(data);
traverse(data).forEach(function() {
if (this.key == field) {
this.update(fn(this.node));
}
});
fs.writeFile(filename, JSON.stringify(data, null, 2), next);
});
});
};
// Asynchronously restores the mapped file from the backup copy.
var unmapFile = function(filename, next) {
var backupFilename = getBackupFilename(filename);
fs.rename(backupFilename, filename, next);
};
//
// Shrinkwrap command
//
var shrinkwrap = function() {
var storePath = getStorePath();
spawn('npm', ['shrinkwrap'], { stdio: 'inherit' }).
on('close', function(code) {
if (code != 0) {
process.exit(code);
}
var tasks = {};
traverse(JSON.parse(fs.readFileSync(inRoot('npm-shrinkwrap.json')))).
forEach(function() {
if (this.node['resolved']) {
var url = this.node['resolved'];
tasks[url] = tasks[url] || function(next) { download(url, storePath, next); };
}
});
console.log("Downloading to package store", chalk.magenta(storePath));
mkdirp.sync(storePath);
async.parallelLimit(tasks, 10, function(err) {
if (err) {
console.error(err);
process.exit(1);
}
});
});
};
//
// Install command
//
var install = function() {
var storePath = getStorePath();
// Complain if we don't have a shrinkwrap file
if (!fs.existsSync(inRoot('npm-shrinkwrap.json'))) {
console.log("Missing 'npm-shrinkwrap.json'. Run " + chalk.yellow(argv.$0 + ' shrinkwrap'));
process.exit(1);
}
var basePort = argv.port || '8080',
host = argv.address || 'localhost';
portfinder.basePort = parseInt(basePort, 10);
portfinder.getPort(function (err, port) {
if (err) throw err;
console.log(
"Installing from package store", chalk.magenta(storePath),
"as", chalk.green(host+':'+port)
);
var server = http.createServer(ecstatic(storePath));
server.listen(port, host, function() {
// Redirect resolved references from the default npm registry to
// localhost in npm-shrinkwrap.json and all top-level package.json files
var mapUrl = function(url) {
return url.indexOf('https://registry.npmjs.org/') == 0 ?
'http://' + host + ':' + port + '/' + urlBasename(url) :
url;
};
var files = glob.sync(inRoot('node_modules/*/package.json'));
files.unshift(inRoot('npm-shrinkwrap.json'));
async.each(files, function(file, next) {
mapFile(file,
path.basename(file) == 'package.json' ? '_resolved' : 'resolved',
mapUrl, next
);
}, function(err) {
var restore = function(code) {
// Restore the mapped files
async.each(files, unmapFile, function() {
process.exit(code);
});
};
if (err) {
console.error(err);
restore(1);
}
process.on('SIGINT', function () { restore(1); });
// Install package files from the vault
spawn('npm', ['install'], { stdio: 'inherit' }).
on('close', function(code) {
server.close();
restore(code);
});
}
);
});
});
};
//
// Usage (help)
//
var usage = function() {
console.log([
'Usage: ' + argv.$0 + ' <command> <options>',
'',
' Save shrinkwrapped npm packages away and install them later.',
'',
'Commands:',
'',
' shrinkwrap (default) run ' + chalk.yellow('npm shrinkwrap') + ' and download required packages',
' install run ' + chalk.yellow('npm install') + ' using previously saved packages',
'',
'Options:',
'',
' -s, --store set directory for saved packages (overrides setting in package.json)',
' -h, --help show usage information',
''
].join('\n'));
};
var command = argv._.join();
if (argv.help) usage();
else if (command == '' || command == 'shrinkwrap') shrinkwrap();
else if (command == 'install') install();
else usage();