This repository has been archived by the owner on Jun 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnpm-files.js
62 lines (59 loc) · 1.69 KB
/
npm-files.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
"use strict"
const R = require('ramda');
const path = require('path');
const fs = require('fs');
/**
* Load a file
* @param {} filename: string
* @returns {}
*/
function LoadJsonFile(filename){
return fs.existsSync(filename) ? JSON.parse(fs.readFileSync(filename)) : {};
}
function filterLocalReps(name) {
return name && name !== 'electron';
}
/**
* recursion find dependencies path and exclude electron
* @param {} pkgPath
* @param {} thinCache
* @return Promise.resolve([paths])
*/
function Deps(pkgPath,thinCache){
thinCache = thinCache|| [] ;
let deps = R.keys(R.pathOr({},['dependencies'])(LoadJsonFile(path.resolve(pkgPath,"package.json"))));
const deep = R.length(R.split('/', pkgPath));
deps = R.filter(filterLocalReps, deps);
return Promise.all(R.map(function(dep){
return new Promise(function(resolve,reject){
let done = false;
let p = null;
for(let i of R.range(0,deep)){
p = path.resolve(pkgPath+'/../'.repeat(i),'node_modules',dep);
if(fs.existsSync(p)){
if(R.contains(p,thinCache)){
resolve([]);
return;
}else{
thinCache = R.concat([p],thinCache);
Deps(p, thinCache).then(function(ary) {
resolve(R.concat([p],ary));
});
done = true;
}
break;
}
}
done ? null : reject('error: can\'t find pkg:'+dep+' for '+pkgPath);
});
}, deps)).then(function(argv){
return Promise.resolve(R.compose(R.uniq,R.flatten)(argv));
},console.error);
}
module.exports = function(name){
return new Promise(function(resolve){
Deps(name || '.').then(function(ary){
resolve(R.map(v=>v+"/**/*",ary));
});
});
}