forked from timoxley/linklocal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
394 lines (354 loc) · 11 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
"use strict"
var fs = require('fs')
var path = require("path")
var debug = require('debug')('linklocal')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var assert = require('assert')
var map = require('map-limit')
var os = require('os')
// Use junctions on Windows < Vista (6.0),
// Vista and later support regular symlinks.
if (os.platform()=='win32' && parseInt(os.release())<6) {
var symlinkType = 'junction'
} else {
symlinkType = 'dir'
}
module.exports = function linklocal(dirpath, _done) {
function done(err, items) {
_done(err, items || [])
}
assert.equal(typeof dirpath, 'string', 'dirpath should be a string')
assert.equal(typeof done, 'function', 'done should be a function')
readPackage(dirpath, function(err, pkg) {
if (err) return done(err)
getLinks(pkg, function(err, links) {
if (err) return done(err)
filterAllLinksToUnlink(links, function(err, toUnlink) {
if (err) return done(err)
unlinkLinks(toUnlink, function(err) {
if (err) return done(err)
linkLinks(links, function(err) {
if (err) return done(err)
done(null, links)
})
})
})
})
})
}
module.exports.link = module.exports
module.exports.link.recursive = function linklocalRecursive(dirpath, done) {
assert.equal(typeof dirpath, 'string', 'dirpath should be a string')
assert.equal(typeof done, 'function', 'done should be a function')
readPackage(dirpath, function(err, pkg) {
if (err) return done(err)
getLinksRecursive(pkg, function(err, links) {
if (err) return done(err)
filterAllLinksToUnlink(links, function(err, toUnlink) {
if (err) return done(err)
unlinkLinks(toUnlink, function(err) {
if (err) return done(err)
linkLinks(links, function(err) {
if (err) return done(err)
done(null, links)
})
})
})
})
})
}
module.exports.unlink = function unlinklocal(dirpath, done) {
readPackage(dirpath, function(err, pkg) {
if (err) return done(err)
getLinks(pkg, function(err, links) {
if (err) return done(err)
filterLinksToUnlink(links, function(err, toUnlink) {
if (err) return done(err)
unlinkLinks(toUnlink, function(err) {
if (err) return done(err)
done(null, toUnlink)
})
})
})
})
}
module.exports.unlink.recursive = function unlinklocalRecursive(dirpath, done) {
assert.equal(typeof dirpath, 'string', 'dirpath should be a string')
assert.equal(typeof done, 'function', 'done should be a function')
readPackage(dirpath, function(err, pkg) {
if (err) return done(err)
getLinksRecursive(pkg, function(err, links) {
if (err) return done(err)
filterLinksToUnlink(links, function(err, toUnlink) {
if (err) return done(err)
unlinkLinks(toUnlink, function(err) {
if (err) return done(err)
done(null, toUnlink)
})
})
})
})
}
module.exports.list = function list(dirpath, done){
readPackage(dirpath, function(err, pkg) {
if (err) return done(err)
getLinks(pkg, done)
})
}
module.exports.list.recursive = function listRecursive(dirpath, done){
readPackage(dirpath, function(err, pkg) {
if (err) return done(err)
getLinksRecursive(pkg, done)
})
}
function getLinksRecursive(pkg, done) {
var _cache = _cache || {}
return (function _getLinksRecursive(pkg, done) {
if (_cache[pkg.dirpath]) return done(null, _cache[pkg.dirpath])
getLinks(pkg, function(err, links) {
_cache[pkg.dirpath] = _cache[pkg.dirpath] || []
if (err) return done(err)
_cache[pkg.dirpath] = _cache[pkg.dirpath].concat(links)
map(links, Infinity, function(link, next) {
readPackage(link.to, function(err, pkg) {
if (err) return next(err)
_getLinksRecursive(pkg, next)
})
}, done)
})
})(pkg, function(err) {
if (err) return done(err)
var result = Object.keys(_cache).reduce(function(result, key) {
return result.concat(_cache[key])
}, [])
result = uniqueKeys(result, 'from', 'to')
return done(null, result)
})
}
function getLocalDependenciesRecursive(pkg, done) {
var _cache = _cache || {}
return (function _getLocalDependenciesRecursive(pkg, done) {
if (_cache[pkg.dirpath]) return done()
getLocalDependencies(pkg, function(err, localDependencies) {
_cache[pkg.dirpath] = true
if (err) return done(err)
map(localDependencies, Infinity, function(localDependency, next) {
readPackage(localDependency, function(err, childPkg) {
if (err) return next(err)
_getLocalDependenciesRecursive(childPkg, next)
})
}, done)
})
})(pkg, function(err) {
if (err) return done(err)
return done(null, Object.keys(_cache))
})
}
function getRealPaths(links, done) {
map(links, Infinity, function(link, next) {
fs.realpath(path.dirname(link), function(err, realPath) {
if (err) return next(err)
next(null, path.join(realPath, path.basename(link)))
})
}, function(err, links) {
done(err, sortDirs(unique(links || [])))
})
}
function readPackage(dirpath, done) {
assert.equal(typeof dirpath, 'string', 'dirpath should be a string')
var pkgpath = path.join(dirpath, 'package.json')
fs.readFile(pkgpath, function(err, data) {
if (err) return done(err)
try {
var pkg = JSON.parse(data)
} catch(e) {
return done(new Error('Error parsing JSON in '+pkgpath+':\n'+ e.message))
}
pkg.dirpath = dirpath
return done(null, pkg)
})
}
function getLocalDependencies(pkg, done) {
assert.equal(typeof pkg, 'object', 'pkg should be an object')
var deps = getDependencies(pkg)
var localDependencies = getPackageLocalDependencies(pkg)
.map(function(name) {
var pkgPath = deps[name]
pkgPath = pkgPath.replace(/^file:/g, '')
return path.resolve(pkg.dirpath, pkgPath)
})
getRealPaths(localDependencies, done)
}
function getPackageLocalDependencies(pkg) {
assert.equal(typeof pkg, 'object', 'pkg should be an object')
var deps = getDependencies(pkg)
return Object.keys(deps).filter(function(name) {
var dep = deps[name]
return isLocalDependency(dep)
})
}
function getDependencies(pkg) {
var deps = pkg.dependencies || {}
var devDependencies = pkg.devDependencies || {}
for (var name in devDependencies) {
deps[name] = devDependencies[name]
}
return deps
}
function isLocalDependency(val) {
return (
val.indexOf('.') === 0 ||
val.indexOf('/') === 0 ||
val.indexOf('file:') === 0
)
}
function getLinks(pkg, done) {
getLocalDependencies(pkg, function(err, localDependencies) {
if (err) return done(err)
var destination = path.join(pkg.dirpath, 'node_modules')
map(localDependencies, Infinity, readPackage, function(err, localDependencyPackages) {
if (err) return done(err)
var links = localDependencyPackages.map(function(localDependency) {
return {
from: path.resolve(destination, localDependency.name),
to: localDependency.dirpath
}
})
done(null, links)
})
})
}
function isSymbolicLink(filepath, done) {
exists(filepath, function(err, doesExist) {
if (err) return done(err)
if (!doesExist) return done(null, false)
fs.lstat(filepath, function(err, stat) {
if (err) return done(err)
return done(null, stat.isSymbolicLink())
})
})
}
function linksTo(from, to, done) {
to = path.resolve(path.dirname(from), to)
isSymbolicLink(from, function(err, isLink) {
if (err) return done(err)
if (!isLink) return done(null, false)
fs.readlink(from, function(err, currentLink) {
if (err) return done(err)
currentLink = path.resolve(path.dirname(from), currentLink)
return done(null, currentLink === to)
})
})
}
function filterLinksToUnlink(links, done) {
links = uniqueKey(links, 'from')
filter(links, Infinity, function(link, next) {
linksTo(link.from, link.to, next)
}, done)
}
function filterAllLinksToUnlink(links, done) {
links = uniqueKey(links, 'from')
filter(links, Infinity, function(link, next) {
exists(link.from, function(err, ex) {
next(err, ex)
})
}, function(err, toUnlink) {
if (err) return done(err)
return done(null, toUnlink)
})
}
function linkLinks(links, done) {
assert.ok(Array.isArray(links), 'links should be an array')
map(links, Infinity, function(link, next) {
mkdirp(path.dirname(link.from), function(err) {
if (err && err.code !== 'EEXISTS') return next(err)
var from = link.from
var to = link.to
// Junction points can't be relative
if (symlinkType!=='junction') {
to = path.relative(path.dirname(from), to)
}
fs.symlink(to, from, symlinkType, function(err) {
if (err) return next(new Error('Error linking ' +from+ ' to ' + to + ':\n' + err.message))
next(null, link)
})
})
}, done)
}
function unlinkLinks(links, done) {
assert.ok(Array.isArray(links), 'links should be an array')
map(links, Infinity, function(link, next) {
isSymbolicLink(link.from, function(err, isSymlink) {
if (err) return next(err)
var remove = isSymlink ? fs.unlink : rimraf
return remove(link.from, function(err) {
if (err) return next(new Error('Error removing ' +link.from+ ':\n' + err.message))
next(err, link)
})
})
}, done)
}
function uniqueKeys(items, key1, key2) {
var keys = [].slice.call(arguments, 1)
return items.filter(function(item, index, arr) {
return indexOf(items, function(otherItem) {
return keys.every(function(key) {
return item[key] === otherItem[key]
})
}) === index
})
}
function indexOf(items, fn) {
return items.reduce(function(foundIndex, item, index) {
if (foundIndex !== -1) return foundIndex
if (fn(item)) return index
return foundIndex
}, -1)
}
function uniqueKey(items, key) {
var values = items.map(function(item) {
return item[key]
})
values = unique(values)
return items.filter(function(item) {
if (!values.length) return
var indexOfValue = values.indexOf(item[key])
if (indexOfValue === -1) return false
values.splice(indexOfValue, 1)
return true
})
}
function unique(arr) {
return arr.filter(function(item, index, arr) {
return arr.indexOf(item) === index
})
}
function notUnique(arr) {
return arr.filter(function(item, index, arr) {
return arr.indexOf(item) !== index
})
}
function sortDirs(dirs) {
return dirs.sort(function(dirA, dirB) {
if (dirA === dirB) return 0
if (dirB.indexOf(dirA) === 0) return 1
return -1
})
}
function filter(arr, num, filterFn, done) {
map(arr, num, filterFn, function(err, matches) {
return done(err, arr.filter(function(item, index) {
return !!matches[index]
}))
})
}
function exists(filepath, done) {
fs.lstat(filepath, function(err, stat) {
if (err) {
if (err.code !== 'ENOENT') return done(err)
return done(null, false)
}
done(null, !!stat)
})
}