ts-node-client 3.2.1 → 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +10 -10
- package/.eslintrc.json +29 -29
- package/.gitattributes +4 -4
- package/.github/workflows/codeql-analysis.yml +71 -71
- package/.github/workflows/publish.yml +32 -0
- package/.travis.yml +12 -12
- package/CHANGELOG.md +58 -52
- package/LICENSE +202 -202
- package/README.md +191 -191
- package/SECURITY.md +21 -21
- package/lib/cli.js +122 -122
- package/lib/convertor.js +244 -244
- package/lib/dependency.js +169 -169
- package/lib/meteor-scanner.js +61 -61
- package/lib/npm-scanner.js +334 -334
- package/lib/pkg.js +36 -36
- package/lib/rest-client.js +129 -129
- package/lib/scanresult.js +32 -32
- package/package-lock.json +5147 -0
- package/package-lock_dev_test.json +47 -47
- package/package-lock_v1.json +863 -863
- package/package-lock_v2.json +5147 -5147
- package/package-lock_v3.json +3014 -3014
- package/package.json +55 -55
- package/test/dependency-test.js +309 -309
- package/test/error-test.js +80 -80
- package/test/rest-test.js +75 -75
- package/test/scanresult-test.js +44 -44
- package/.yarnrc.yml +0 -1
package/lib/npm-scanner.js
CHANGED
|
@@ -1,334 +1,334 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/**********************************************************
|
|
3
|
-
* Copyright (c) 2017. Enterprise Architecture Group, EACG
|
|
4
|
-
*
|
|
5
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
-
*********************************************************/
|
|
7
|
-
/* eslint-enable */
|
|
8
|
-
|
|
9
|
-
const fs = require('fs');
|
|
10
|
-
const path = require('path');
|
|
11
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
12
|
-
const lockfile = require('@yarnpkg/lockfile');
|
|
13
|
-
const yaml = require('js-yaml');
|
|
14
|
-
const debuglog = (require('debuglog'))('ts-npm-scanner');
|
|
15
|
-
const ScanResult = require('./scanresult');
|
|
16
|
-
const { RestClient } = require('./rest-client');
|
|
17
|
-
const Dependency = require('./dependency');
|
|
18
|
-
|
|
19
|
-
exports.Scanner = Scanner;
|
|
20
|
-
|
|
21
|
-
function Scanner(options) {
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.name = 'ts-npm-scanner';
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function getPackageJson(self) {
|
|
27
|
-
try {
|
|
28
|
-
const file = path.resolve(process.cwd(), 'package.json');
|
|
29
|
-
const data = fs.readFileSync(file);
|
|
30
|
-
if (data && data.code === 'ENOENT') {
|
|
31
|
-
debuglog('npm.fs.package - error:', data);
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
const jsonFile = JSON.parse(data);
|
|
35
|
-
debuglog('Project: ', jsonFile.name, jsonFile.version);
|
|
36
|
-
const dependencies = self.walkPackage(jsonFile, 0, jsonFile);
|
|
37
|
-
return { root: jsonFile, dependencies };
|
|
38
|
-
} catch (e) {
|
|
39
|
-
debuglog('npm.fs.package - error:', e);
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function getPackageLockJson(self) {
|
|
44
|
-
try {
|
|
45
|
-
const file = path.resolve(process.cwd(), 'package-lock.json');
|
|
46
|
-
const data = fs.readFileSync(file);
|
|
47
|
-
if (data && data.code === 'ENOENT') {
|
|
48
|
-
debuglog('npm.fs.package-lock - error:', data);
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
const jsonFile = JSON.parse(data);
|
|
52
|
-
debuglog('Project: ', jsonFile.name, jsonFile.version);
|
|
53
|
-
const dependencies = self.walk(jsonFile, 0, JSON.parse(JSON.stringify(jsonFile)));
|
|
54
|
-
return { root: jsonFile, dependencies };
|
|
55
|
-
} catch (e) {
|
|
56
|
-
debuglog('npm.fs.package-lock - error:', e);
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function getYarnLock(self, packageData) {
|
|
61
|
-
try {
|
|
62
|
-
const file = path.resolve(process.cwd(), 'yarn.lock');
|
|
63
|
-
const data = fs.readFileSync(file);
|
|
64
|
-
if (data && data.code === 'ENOENT') {
|
|
65
|
-
debuglog('npm.fs.yarn-lock - error:', data);
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
// yarn 1
|
|
69
|
-
try {
|
|
70
|
-
const jsonFile = lockfile.parse(data.toString());
|
|
71
|
-
if (jsonFile && jsonFile.type === 'success') {
|
|
72
|
-
const dependencies = yarnToResults(self, jsonFile.object, packageData);
|
|
73
|
-
return { root: jsonFile, dependencies };
|
|
74
|
-
}
|
|
75
|
-
} catch (e) {
|
|
76
|
-
debuglog('npm.fs.yarn-v1-lock - error:', e);
|
|
77
|
-
}
|
|
78
|
-
// yarn 2+
|
|
79
|
-
try {
|
|
80
|
-
const json = yaml.load(data.toString());
|
|
81
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
82
|
-
if (json && json.__metadata) {
|
|
83
|
-
// eslint-disable-next-line no-underscore-dangle
|
|
84
|
-
delete json.__metadata;
|
|
85
|
-
const dependencies = yarnToResults(self, json, packageData);
|
|
86
|
-
return { root: json, dependencies };
|
|
87
|
-
}
|
|
88
|
-
} catch (e) {
|
|
89
|
-
debuglog('npm.fs.yarn-v2-lock - error:', e);
|
|
90
|
-
}
|
|
91
|
-
debuglog('npm.fs.yarn-lock - error: Failed to parse file');
|
|
92
|
-
return null;
|
|
93
|
-
} catch (e) {
|
|
94
|
-
debuglog('npm.fs.yarn-lock - error:', e);
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
function yarnToResults(self, json, packageData) {
|
|
99
|
-
const base = {
|
|
100
|
-
name: (packageData && packageData.root && packageData.root.name) || 'root',
|
|
101
|
-
version: (packageData && packageData.root && packageData.root.version) || '1',
|
|
102
|
-
dependencies: json
|
|
103
|
-
};
|
|
104
|
-
debuglog('Project: ', base.name, base.version);
|
|
105
|
-
return self.walkYarn(base, 0, base);
|
|
106
|
-
}
|
|
107
|
-
function saveResults(cb, options, root, dependencies) {
|
|
108
|
-
const result = new ScanResult(options.project, root.name, `npm:${root.name}`, dependencies);
|
|
109
|
-
debuglog('result: ', JSON.stringify(result));
|
|
110
|
-
cb(undefined, result);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
Scanner.prototype.scan = function scan(cb) {
|
|
114
|
-
const self = this;
|
|
115
|
-
const { options } = this;
|
|
116
|
-
|
|
117
|
-
const packageData = getPackageJson(self);
|
|
118
|
-
const packageLockData = getPackageLockJson(self);
|
|
119
|
-
const yarnLockData = getYarnLock(self, packageData);
|
|
120
|
-
if (packageData && yarnLockData) {
|
|
121
|
-
yarnLockData.root = packageData.root;
|
|
122
|
-
}
|
|
123
|
-
const data = packageLockData || yarnLockData || packageData;
|
|
124
|
-
if (data && data.root) {
|
|
125
|
-
saveResults(cb, options, data.root, data.dependencies);
|
|
126
|
-
} else {
|
|
127
|
-
const err = {
|
|
128
|
-
status: 404,
|
|
129
|
-
message: 'No results found in `package-lock.json` or `package.json` or `yarn.lock`'
|
|
130
|
-
};
|
|
131
|
-
debuglog('result: ', JSON.stringify(err));
|
|
132
|
-
cb(err);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
Scanner.prototype.transfer = function transfer(scan, cb) {
|
|
137
|
-
const client = new RestClient(this.options);
|
|
138
|
-
client.transfer(scan, cb);
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
/* eslint-disable no-underscore-dangle, prefer-rest-params, no-mixed-operators */
|
|
142
|
-
Scanner.prototype.walk = function walk(npmDependency, level, root) {
|
|
143
|
-
const self = this;
|
|
144
|
-
const opts = this.options || {};
|
|
145
|
-
level = level || 0;
|
|
146
|
-
|
|
147
|
-
printDependency(npmDependency, level);
|
|
148
|
-
if (npmDependency.name) {
|
|
149
|
-
let pkg = root && root.packages && root.packages[`node_modules/${npmDependency.name}`];
|
|
150
|
-
if (!pkg) {
|
|
151
|
-
pkg = root && root.packages && root.packages[''];
|
|
152
|
-
}
|
|
153
|
-
let repository = npmDependency.repository && npmDependency.repository.url;
|
|
154
|
-
if (!repository) {
|
|
155
|
-
repository = pkg && pkg.repository && pkg.repository.url;
|
|
156
|
-
}
|
|
157
|
-
const dependency = new Dependency(
|
|
158
|
-
npmDependency.name,
|
|
159
|
-
npmDependency.version,
|
|
160
|
-
'npm',
|
|
161
|
-
npmDependency.description,
|
|
162
|
-
npmDependency.private,
|
|
163
|
-
npmDependency.licenses || npmDependency.license || (pkg && (pkg.licenses || pkg.license)),
|
|
164
|
-
npmDependency.homepage || (pkg && pkg.homepage),
|
|
165
|
-
repository
|
|
166
|
-
);
|
|
167
|
-
if (npmDependency.dependencies) {
|
|
168
|
-
Object.getOwnPropertyNames(npmDependency.dependencies).forEach((val) => {
|
|
169
|
-
const childDependency = npmDependency.dependencies[val];
|
|
170
|
-
if (childDependency) {
|
|
171
|
-
childDependency.name = val;
|
|
172
|
-
}
|
|
173
|
-
checkForChild(self, opts, dependency, childDependency, val, level, root);
|
|
174
|
-
});
|
|
175
|
-
} else if (npmDependency.packages) {
|
|
176
|
-
Object.getOwnPropertyNames(npmDependency.packages).forEach((val) => {
|
|
177
|
-
const childDependency = npmDependency.packages[val];
|
|
178
|
-
if (childDependency) {
|
|
179
|
-
const parts = val.split('node_modules/');
|
|
180
|
-
childDependency.name = parts.length > 1 ? parts.slice(1).join('node_modules/') : parts[0];
|
|
181
|
-
}
|
|
182
|
-
checkForChild(self, opts, dependency, childDependency, val, level, root);
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
return dependency;
|
|
186
|
-
}
|
|
187
|
-
return null;
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
function checkForChild(self, opts, dependency, childDependency, val, level, root) {
|
|
191
|
-
let child = null;
|
|
192
|
-
|
|
193
|
-
function log() {
|
|
194
|
-
const args = [].slice.call(arguments, 0);
|
|
195
|
-
if (opts.verbose) {
|
|
196
|
-
console.log.apply(this, args);
|
|
197
|
-
} else {
|
|
198
|
-
debuglog.apply(this, args);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
// check for errorneous dependencies (e.g a nmp-debug.log file)
|
|
202
|
-
if (childDependency.error) {
|
|
203
|
-
log('Skipping errorneous dependency on level %d: ', level, val);
|
|
204
|
-
// check for blacklisted dependencies on level 0
|
|
205
|
-
} else if (level === 0 && (opts.exclude instanceof Array && opts.exclude.indexOf(val) >= 0 || opts.exclude === val)) {
|
|
206
|
-
log('Skipping level 0 blacklisted dependency: ', val);
|
|
207
|
-
// include dev dependencies on level 0 if configured
|
|
208
|
-
} else if (level === 0 && opts.includeDevDependencies
|
|
209
|
-
&& (opts.includeDevDependencies !== 'false') && childDependency && childDependency.dev) {
|
|
210
|
-
log('Adding level 0 devDependency:', val);
|
|
211
|
-
child = self.walk(childDependency, level + 1, root);
|
|
212
|
-
// include runtime dependencies
|
|
213
|
-
} else if (childDependency && !childDependency.dev) {
|
|
214
|
-
log('Adding dependency on level %d:', level, val);
|
|
215
|
-
child = self.walk(childDependency, level + 1, root);
|
|
216
|
-
} else {
|
|
217
|
-
log('Skipping undeclared dependency on level %d: ', level, val);
|
|
218
|
-
}
|
|
219
|
-
if (child) {
|
|
220
|
-
dependency.addDependency(child);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/* eslint-enable no-underscore-dangle, prefer-rest-params, no-mixed-operators */
|
|
225
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
226
|
-
Scanner.prototype.walkPackage = function walkPackage(npmDependency, level) {
|
|
227
|
-
const self = this;
|
|
228
|
-
const opts = this.options || {};
|
|
229
|
-
level = level || 0;
|
|
230
|
-
|
|
231
|
-
printDependency(npmDependency, level);
|
|
232
|
-
|
|
233
|
-
if (npmDependency.name) {
|
|
234
|
-
const repository = npmDependency.repository && npmDependency.repository.url;
|
|
235
|
-
const dependency = new Dependency(
|
|
236
|
-
npmDependency.name,
|
|
237
|
-
npmDependency.version,
|
|
238
|
-
'npm',
|
|
239
|
-
npmDependency.description,
|
|
240
|
-
npmDependency.private,
|
|
241
|
-
npmDependency.licenses || npmDependency.license,
|
|
242
|
-
npmDependency.homepage,
|
|
243
|
-
repository
|
|
244
|
-
);
|
|
245
|
-
if (npmDependency.dependencies) {
|
|
246
|
-
Object.getOwnPropertyNames(npmDependency.dependencies).forEach((val) => {
|
|
247
|
-
const childDependency = {
|
|
248
|
-
name: val,
|
|
249
|
-
version: npmDependency.dependencies[val]
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
253
|
-
});
|
|
254
|
-
if (opts.includeDevDependencies) {
|
|
255
|
-
Object.getOwnPropertyNames(npmDependency.devDependencies).forEach((val) => {
|
|
256
|
-
const childDependency = {
|
|
257
|
-
name: val,
|
|
258
|
-
version: npmDependency.devDependencies[val]
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return dependency;
|
|
266
|
-
}
|
|
267
|
-
return null;
|
|
268
|
-
};
|
|
269
|
-
|
|
270
|
-
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
271
|
-
Scanner.prototype.walkYarn = function walkPackage(npmDependency, level) {
|
|
272
|
-
const self = this;
|
|
273
|
-
const opts = this.options || {};
|
|
274
|
-
level = level || 0;
|
|
275
|
-
|
|
276
|
-
printDependency(npmDependency, level);
|
|
277
|
-
|
|
278
|
-
if (npmDependency.name) {
|
|
279
|
-
const repository = npmDependency.repository && npmDependency.repository.url;
|
|
280
|
-
const dependency = new Dependency(
|
|
281
|
-
npmDependency.name,
|
|
282
|
-
npmDependency.version,
|
|
283
|
-
'npm',
|
|
284
|
-
npmDependency.description,
|
|
285
|
-
npmDependency.private,
|
|
286
|
-
npmDependency.licenses || npmDependency.license,
|
|
287
|
-
npmDependency.homepage,
|
|
288
|
-
repository
|
|
289
|
-
);
|
|
290
|
-
if (npmDependency.dependencies) {
|
|
291
|
-
Object.getOwnPropertyNames(npmDependency.dependencies).forEach((val) => {
|
|
292
|
-
const name = val.split('@')[0];
|
|
293
|
-
const depData = npmDependency.dependencies[val];
|
|
294
|
-
const version = (depData && depData.version) || depData;
|
|
295
|
-
const childDependency = {
|
|
296
|
-
name,
|
|
297
|
-
version: version && typeof version === 'string' ? version : null
|
|
298
|
-
};
|
|
299
|
-
|
|
300
|
-
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
301
|
-
});
|
|
302
|
-
if (opts.includeDevDependencies && opts.includeDevDependencies !== 'false') {
|
|
303
|
-
Object.getOwnPropertyNames(npmDependency.devDependencies).forEach((val) => {
|
|
304
|
-
const depData = npmDependency.devDependencies[val];
|
|
305
|
-
const version = (depData && depData.version) || depData;
|
|
306
|
-
const childDependency = {
|
|
307
|
-
name: val,
|
|
308
|
-
version: version && typeof version === 'string' ? version : null
|
|
309
|
-
};
|
|
310
|
-
|
|
311
|
-
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
return dependency;
|
|
316
|
-
}
|
|
317
|
-
return null;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
function printDependency(dep, level) {
|
|
321
|
-
level = level || 0;
|
|
322
|
-
const fill = level === 0 ? '' : new Array(level * 4).join(' ');
|
|
323
|
-
|
|
324
|
-
debuglog(`${fill}-----------------------------------------`);
|
|
325
|
-
debuglog(`${fill}Name, Version: `, dep.name, dep.version);
|
|
326
|
-
debuglog(`${fill}License: `, dep.license || dep.licenses);
|
|
327
|
-
debuglog(`${fill}Private: `, dep.private);
|
|
328
|
-
debuglog(`${fill}Description: `, dep.description);
|
|
329
|
-
if (dep.repository) {
|
|
330
|
-
debuglog(`${fill}Repository type:`, dep.repository.type);
|
|
331
|
-
debuglog(`${fill}Repository url:`, dep.repository.url);
|
|
332
|
-
}
|
|
333
|
-
debuglog(`${fill}Homepage: `, dep.homepage);
|
|
334
|
-
}
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**********************************************************
|
|
3
|
+
* Copyright (c) 2017. Enterprise Architecture Group, EACG
|
|
4
|
+
*
|
|
5
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
6
|
+
*********************************************************/
|
|
7
|
+
/* eslint-enable */
|
|
8
|
+
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
12
|
+
const lockfile = require('@yarnpkg/lockfile');
|
|
13
|
+
const yaml = require('js-yaml');
|
|
14
|
+
const debuglog = (require('debuglog'))('ts-npm-scanner');
|
|
15
|
+
const ScanResult = require('./scanresult');
|
|
16
|
+
const { RestClient } = require('./rest-client');
|
|
17
|
+
const Dependency = require('./dependency');
|
|
18
|
+
|
|
19
|
+
exports.Scanner = Scanner;
|
|
20
|
+
|
|
21
|
+
function Scanner(options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.name = 'ts-npm-scanner';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getPackageJson(self) {
|
|
27
|
+
try {
|
|
28
|
+
const file = path.resolve(process.cwd(), 'package.json');
|
|
29
|
+
const data = fs.readFileSync(file);
|
|
30
|
+
if (data && data.code === 'ENOENT') {
|
|
31
|
+
debuglog('npm.fs.package - error:', data);
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const jsonFile = JSON.parse(data);
|
|
35
|
+
debuglog('Project: ', jsonFile.name, jsonFile.version);
|
|
36
|
+
const dependencies = self.walkPackage(jsonFile, 0, jsonFile);
|
|
37
|
+
return { root: jsonFile, dependencies };
|
|
38
|
+
} catch (e) {
|
|
39
|
+
debuglog('npm.fs.package - error:', e);
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function getPackageLockJson(self) {
|
|
44
|
+
try {
|
|
45
|
+
const file = path.resolve(process.cwd(), 'package-lock.json');
|
|
46
|
+
const data = fs.readFileSync(file);
|
|
47
|
+
if (data && data.code === 'ENOENT') {
|
|
48
|
+
debuglog('npm.fs.package-lock - error:', data);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const jsonFile = JSON.parse(data);
|
|
52
|
+
debuglog('Project: ', jsonFile.name, jsonFile.version);
|
|
53
|
+
const dependencies = self.walk(jsonFile, 0, JSON.parse(JSON.stringify(jsonFile)));
|
|
54
|
+
return { root: jsonFile, dependencies };
|
|
55
|
+
} catch (e) {
|
|
56
|
+
debuglog('npm.fs.package-lock - error:', e);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function getYarnLock(self, packageData) {
|
|
61
|
+
try {
|
|
62
|
+
const file = path.resolve(process.cwd(), 'yarn.lock');
|
|
63
|
+
const data = fs.readFileSync(file);
|
|
64
|
+
if (data && data.code === 'ENOENT') {
|
|
65
|
+
debuglog('npm.fs.yarn-lock - error:', data);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
// yarn 1
|
|
69
|
+
try {
|
|
70
|
+
const jsonFile = lockfile.parse(data.toString());
|
|
71
|
+
if (jsonFile && jsonFile.type === 'success') {
|
|
72
|
+
const dependencies = yarnToResults(self, jsonFile.object, packageData);
|
|
73
|
+
return { root: jsonFile, dependencies };
|
|
74
|
+
}
|
|
75
|
+
} catch (e) {
|
|
76
|
+
debuglog('npm.fs.yarn-v1-lock - error:', e);
|
|
77
|
+
}
|
|
78
|
+
// yarn 2+
|
|
79
|
+
try {
|
|
80
|
+
const json = yaml.load(data.toString());
|
|
81
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
82
|
+
if (json && json.__metadata) {
|
|
83
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
84
|
+
delete json.__metadata;
|
|
85
|
+
const dependencies = yarnToResults(self, json, packageData);
|
|
86
|
+
return { root: json, dependencies };
|
|
87
|
+
}
|
|
88
|
+
} catch (e) {
|
|
89
|
+
debuglog('npm.fs.yarn-v2-lock - error:', e);
|
|
90
|
+
}
|
|
91
|
+
debuglog('npm.fs.yarn-lock - error: Failed to parse file');
|
|
92
|
+
return null;
|
|
93
|
+
} catch (e) {
|
|
94
|
+
debuglog('npm.fs.yarn-lock - error:', e);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function yarnToResults(self, json, packageData) {
|
|
99
|
+
const base = {
|
|
100
|
+
name: (packageData && packageData.root && packageData.root.name) || 'root',
|
|
101
|
+
version: (packageData && packageData.root && packageData.root.version) || '1',
|
|
102
|
+
dependencies: json
|
|
103
|
+
};
|
|
104
|
+
debuglog('Project: ', base.name, base.version);
|
|
105
|
+
return self.walkYarn(base, 0, base);
|
|
106
|
+
}
|
|
107
|
+
function saveResults(cb, options, root, dependencies) {
|
|
108
|
+
const result = new ScanResult(options.project, root.name, `npm:${root.name}`, dependencies);
|
|
109
|
+
debuglog('result: ', JSON.stringify(result));
|
|
110
|
+
cb(undefined, result);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
Scanner.prototype.scan = function scan(cb) {
|
|
114
|
+
const self = this;
|
|
115
|
+
const { options } = this;
|
|
116
|
+
|
|
117
|
+
const packageData = getPackageJson(self);
|
|
118
|
+
const packageLockData = getPackageLockJson(self);
|
|
119
|
+
const yarnLockData = getYarnLock(self, packageData);
|
|
120
|
+
if (packageData && yarnLockData) {
|
|
121
|
+
yarnLockData.root = packageData.root;
|
|
122
|
+
}
|
|
123
|
+
const data = packageLockData || yarnLockData || packageData;
|
|
124
|
+
if (data && data.root) {
|
|
125
|
+
saveResults(cb, options, data.root, data.dependencies);
|
|
126
|
+
} else {
|
|
127
|
+
const err = {
|
|
128
|
+
status: 404,
|
|
129
|
+
message: 'No results found in `package-lock.json` or `package.json` or `yarn.lock`'
|
|
130
|
+
};
|
|
131
|
+
debuglog('result: ', JSON.stringify(err));
|
|
132
|
+
cb(err);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
Scanner.prototype.transfer = function transfer(scan, cb) {
|
|
137
|
+
const client = new RestClient(this.options);
|
|
138
|
+
client.transfer(scan, cb);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/* eslint-disable no-underscore-dangle, prefer-rest-params, no-mixed-operators */
|
|
142
|
+
Scanner.prototype.walk = function walk(npmDependency, level, root) {
|
|
143
|
+
const self = this;
|
|
144
|
+
const opts = this.options || {};
|
|
145
|
+
level = level || 0;
|
|
146
|
+
|
|
147
|
+
printDependency(npmDependency, level);
|
|
148
|
+
if (npmDependency.name) {
|
|
149
|
+
let pkg = root && root.packages && root.packages[`node_modules/${npmDependency.name}`];
|
|
150
|
+
if (!pkg) {
|
|
151
|
+
pkg = root && root.packages && root.packages[''];
|
|
152
|
+
}
|
|
153
|
+
let repository = npmDependency.repository && npmDependency.repository.url;
|
|
154
|
+
if (!repository) {
|
|
155
|
+
repository = pkg && pkg.repository && pkg.repository.url;
|
|
156
|
+
}
|
|
157
|
+
const dependency = new Dependency(
|
|
158
|
+
npmDependency.name,
|
|
159
|
+
npmDependency.version,
|
|
160
|
+
'npm',
|
|
161
|
+
npmDependency.description,
|
|
162
|
+
npmDependency.private,
|
|
163
|
+
npmDependency.licenses || npmDependency.license || (pkg && (pkg.licenses || pkg.license)),
|
|
164
|
+
npmDependency.homepage || (pkg && pkg.homepage),
|
|
165
|
+
repository
|
|
166
|
+
);
|
|
167
|
+
if (npmDependency.dependencies) {
|
|
168
|
+
Object.getOwnPropertyNames(npmDependency.dependencies).forEach((val) => {
|
|
169
|
+
const childDependency = npmDependency.dependencies[val];
|
|
170
|
+
if (childDependency) {
|
|
171
|
+
childDependency.name = val;
|
|
172
|
+
}
|
|
173
|
+
checkForChild(self, opts, dependency, childDependency, val, level, root);
|
|
174
|
+
});
|
|
175
|
+
} else if (npmDependency.packages) {
|
|
176
|
+
Object.getOwnPropertyNames(npmDependency.packages).forEach((val) => {
|
|
177
|
+
const childDependency = npmDependency.packages[val];
|
|
178
|
+
if (childDependency) {
|
|
179
|
+
const parts = val.split('node_modules/');
|
|
180
|
+
childDependency.name = parts.length > 1 ? parts.slice(1).join('node_modules/') : parts[0];
|
|
181
|
+
}
|
|
182
|
+
checkForChild(self, opts, dependency, childDependency, val, level, root);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return dependency;
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
function checkForChild(self, opts, dependency, childDependency, val, level, root) {
|
|
191
|
+
let child = null;
|
|
192
|
+
|
|
193
|
+
function log() {
|
|
194
|
+
const args = [].slice.call(arguments, 0);
|
|
195
|
+
if (opts.verbose) {
|
|
196
|
+
console.log.apply(this, args);
|
|
197
|
+
} else {
|
|
198
|
+
debuglog.apply(this, args);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// check for errorneous dependencies (e.g a nmp-debug.log file)
|
|
202
|
+
if (childDependency.error) {
|
|
203
|
+
log('Skipping errorneous dependency on level %d: ', level, val);
|
|
204
|
+
// check for blacklisted dependencies on level 0
|
|
205
|
+
} else if (level === 0 && (opts.exclude instanceof Array && opts.exclude.indexOf(val) >= 0 || opts.exclude === val)) {
|
|
206
|
+
log('Skipping level 0 blacklisted dependency: ', val);
|
|
207
|
+
// include dev dependencies on level 0 if configured
|
|
208
|
+
} else if (level === 0 && opts.includeDevDependencies
|
|
209
|
+
&& (opts.includeDevDependencies !== 'false') && childDependency && childDependency.dev) {
|
|
210
|
+
log('Adding level 0 devDependency:', val);
|
|
211
|
+
child = self.walk(childDependency, level + 1, root);
|
|
212
|
+
// include runtime dependencies
|
|
213
|
+
} else if (childDependency && !childDependency.dev) {
|
|
214
|
+
log('Adding dependency on level %d:', level, val);
|
|
215
|
+
child = self.walk(childDependency, level + 1, root);
|
|
216
|
+
} else {
|
|
217
|
+
log('Skipping undeclared dependency on level %d: ', level, val);
|
|
218
|
+
}
|
|
219
|
+
if (child) {
|
|
220
|
+
dependency.addDependency(child);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/* eslint-enable no-underscore-dangle, prefer-rest-params, no-mixed-operators */
|
|
225
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
226
|
+
Scanner.prototype.walkPackage = function walkPackage(npmDependency, level) {
|
|
227
|
+
const self = this;
|
|
228
|
+
const opts = this.options || {};
|
|
229
|
+
level = level || 0;
|
|
230
|
+
|
|
231
|
+
printDependency(npmDependency, level);
|
|
232
|
+
|
|
233
|
+
if (npmDependency.name) {
|
|
234
|
+
const repository = npmDependency.repository && npmDependency.repository.url;
|
|
235
|
+
const dependency = new Dependency(
|
|
236
|
+
npmDependency.name,
|
|
237
|
+
npmDependency.version,
|
|
238
|
+
'npm',
|
|
239
|
+
npmDependency.description,
|
|
240
|
+
npmDependency.private,
|
|
241
|
+
npmDependency.licenses || npmDependency.license,
|
|
242
|
+
npmDependency.homepage,
|
|
243
|
+
repository
|
|
244
|
+
);
|
|
245
|
+
if (npmDependency.dependencies) {
|
|
246
|
+
Object.getOwnPropertyNames(npmDependency.dependencies).forEach((val) => {
|
|
247
|
+
const childDependency = {
|
|
248
|
+
name: val,
|
|
249
|
+
version: npmDependency.dependencies[val]
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
253
|
+
});
|
|
254
|
+
if (opts.includeDevDependencies) {
|
|
255
|
+
Object.getOwnPropertyNames(npmDependency.devDependencies).forEach((val) => {
|
|
256
|
+
const childDependency = {
|
|
257
|
+
name: val,
|
|
258
|
+
version: npmDependency.devDependencies[val]
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return dependency;
|
|
266
|
+
}
|
|
267
|
+
return null;
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
271
|
+
Scanner.prototype.walkYarn = function walkPackage(npmDependency, level) {
|
|
272
|
+
const self = this;
|
|
273
|
+
const opts = this.options || {};
|
|
274
|
+
level = level || 0;
|
|
275
|
+
|
|
276
|
+
printDependency(npmDependency, level);
|
|
277
|
+
|
|
278
|
+
if (npmDependency.name) {
|
|
279
|
+
const repository = npmDependency.repository && npmDependency.repository.url;
|
|
280
|
+
const dependency = new Dependency(
|
|
281
|
+
npmDependency.name,
|
|
282
|
+
npmDependency.version,
|
|
283
|
+
'npm',
|
|
284
|
+
npmDependency.description,
|
|
285
|
+
npmDependency.private,
|
|
286
|
+
npmDependency.licenses || npmDependency.license,
|
|
287
|
+
npmDependency.homepage,
|
|
288
|
+
repository
|
|
289
|
+
);
|
|
290
|
+
if (npmDependency.dependencies) {
|
|
291
|
+
Object.getOwnPropertyNames(npmDependency.dependencies).forEach((val) => {
|
|
292
|
+
const name = val.split('@')[0];
|
|
293
|
+
const depData = npmDependency.dependencies[val];
|
|
294
|
+
const version = (depData && depData.version) || depData;
|
|
295
|
+
const childDependency = {
|
|
296
|
+
name,
|
|
297
|
+
version: version && typeof version === 'string' ? version : null
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
301
|
+
});
|
|
302
|
+
if (opts.includeDevDependencies && opts.includeDevDependencies !== 'false') {
|
|
303
|
+
Object.getOwnPropertyNames(npmDependency.devDependencies).forEach((val) => {
|
|
304
|
+
const depData = npmDependency.devDependencies[val];
|
|
305
|
+
const version = (depData && depData.version) || depData;
|
|
306
|
+
const childDependency = {
|
|
307
|
+
name: val,
|
|
308
|
+
version: version && typeof version === 'string' ? version : null
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
checkForChild(self, opts, dependency, childDependency, val, level);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return dependency;
|
|
316
|
+
}
|
|
317
|
+
return null;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
function printDependency(dep, level) {
|
|
321
|
+
level = level || 0;
|
|
322
|
+
const fill = level === 0 ? '' : new Array(level * 4).join(' ');
|
|
323
|
+
|
|
324
|
+
debuglog(`${fill}-----------------------------------------`);
|
|
325
|
+
debuglog(`${fill}Name, Version: `, dep.name, dep.version);
|
|
326
|
+
debuglog(`${fill}License: `, dep.license || dep.licenses);
|
|
327
|
+
debuglog(`${fill}Private: `, dep.private);
|
|
328
|
+
debuglog(`${fill}Description: `, dep.description);
|
|
329
|
+
if (dep.repository) {
|
|
330
|
+
debuglog(`${fill}Repository type:`, dep.repository.type);
|
|
331
|
+
debuglog(`${fill}Repository url:`, dep.repository.url);
|
|
332
|
+
}
|
|
333
|
+
debuglog(`${fill}Homepage: `, dep.homepage);
|
|
334
|
+
}
|