-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
172 lines (151 loc) · 4.39 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
'use strict';
var path = require('path');
var fs = require('fs');
var mkdirp = require('mkdirp');
var jsonfile = require('jsonfile');
var semver = require('semver')
var async = require('async');
var ncp = require('ncp').ncp;
var _ = require('lodash');
var g_opts = {};
var copysync = require("copysync");
function isDirExists(directory)
{
try
{
var stats = fs.lstatSync(directory);
if (stats.isDirectory())
return true;
}
catch(e)
{
return false;
}
return false;
}
function getPackageJson(pkgFolder)
{
var pkgPath = path.resolve(pkgFolder, './package.json');
var pkgContent = null;
try
{
pkgContent = jsonfile.readFileSync(pkgPath, {throws: false});
}
catch(e)
{
// skip missing modules
return null;
}
return pkgContent;
}
function hasPackage(pkg, pkgs)
{
for (var i = 0; i < pkgs.length; ++i)
{
if (pkgs[i].name === pkg.name && pkgs[i].version === pkg.version)
return true;
}
return false;
}
function addPkgDeps(baseDir, pkg, pkgs, callback)
{
var pkgDir = path.resolve(baseDir, './node_modules/' + pkg.name);
//console.log('checking dir:' + pkgDir);
var pkgContent = getPackageJson(pkgDir);
if (!pkgContent)
return;
if (hasPackage(pkgContent, pkgs))
{
//console.log('existed, pkg:' + pkgContent.name + ' ver:' + pkgContent.version) ;
return;
}
if (baseDir === g_opts.srcDir)
{
if (!semver.validRange(pkg.version))
{
pkgs.push({name: pkgContent.name, version: pkgContent.version});
}
else if (semver.satisfies(pkgContent.version, pkg.version))
pkgs.push({name: pkgContent.name, version: pkgContent.version});
else
return;
}
// recursive search sub modules
var subPkgBase = path.resolve(pkgDir, './node_modules');
if (isDirExists(subPkgBase))
{
var subPkgs = fs.readdirSync(subPkgBase);
for (var i in subPkgs)
{
var subPkgName = subPkgs[i];
addPkgDeps(pkgDir, {name: subPkgName, version: '*'}, pkgs, callback);
}
}
for (var pkgName in pkgContent.dependencies)
{
var version = pkgContent.dependencies[pkgName];
var depPkg = {name: pkgName, version: version};
addPkgDeps(g_opts.srcDir, depPkg, pkgs, callback);
addPkgDeps(pkgDir, depPkg, pkgs, callback);
}
}
function findPkgDeps(pkg, callback)
{
var pkgs = [];
addPkgDeps(g_opts.srcDir, pkg, pkgs, callback);
callback(null, pkgs);
}
function copyModules(pkgContent, callback)
{
var pkg = pkgContent.name;
var srcDir = path.resolve(g_opts.srcDir, './node_modules/' + pkg);
var dstDir = path.resolve(g_opts.dstDir, './node_modules/' + pkg);
copysync(srcDir,dstDir)
}
function copyNodeModulesSync(srcDir, dstDir, opts, callback)
{
if (!srcDir)
throw new Error('missing source diretory argument');
if (!dstDir)
throw new Error('missing destination diretory argument');
if (!callback)
{
g_opts = {srcDir: srcDir, dstDir: dstDir, devDependencies: false};
callback = opts;
}
else
{
g_opts = opts || {};
g_opts.srcDir = srcDir;
g_opts.dstDir = dstDir;
}
var pkgPath = path.resolve(srcDir, './package.json');
var pkgContent = jsonfile.readFileSync(pkgPath, {throws: false});
if (!pkgContent)
throw new Error('parse package.json in source directory fail');
// prepare root package list
var rootPkgList = [];
for (var depPkgName in pkgContent.dependencies)
rootPkgList.push({name: depPkgName, version: pkgContent.dependencies[depPkgName]});
if (g_opts.devDependencies)
{
for (var devDepPkgName in pkgContent.devDependencies)
rootPkgList.push({name: devDepPkgName, version: pkgContent.dependencies[devDepPkgName]});
}
async.map(rootPkgList, findPkgDeps, function(err, results) {
if (err)
{
callback(err);
return;
}
var allPkgList = _.uniqWith(_.flatten(results), function(a, b) {
if (a.name === b.name && a.version === b.version)
return true;
return false;
});
async.each(allPkgList, copyModules, function(err) {
callback(err, allPkgList);
});
});
}
module.exports = copyNodeModulesSync;