-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs-inspector.js
333 lines (285 loc) · 14.2 KB
/
fs-inspector.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
'use strict';
const DependencyInfo = require('./dependency-info');
const fs = require('fs');
const {TreeSet, TreeMap} = require('jstreemap');
const ConfigHelper = require("./config-helper");
class FsInspector {
// Processing for when we have a standard package.json
/**
* Scans what is known about a package to determine who the author of the package is.
* The order of precidence for authorship is as follows:
* 1) Author defined in the package.json file
* 2) Contributors as defined in the package.json file.
* NOTE: In instances where there are more than three contributors, only the first three are listed.
* 3) If the package is hosted on Github, the username of the host will be used
* 4) Authorship is assigned to the username of the account on npm that manages the package.
*
*
* @param {object} descriptor The package descriptor object. Everything one would hope to know about a package.
*
* @returns The author of the project, as determined by the rules above.
*/
static getPackageAuthors(descriptor) {
const nameMapper = (author) => {
if (author && author.name) {
return author.name;
}
return author;
}
if (descriptor.author) {
return nameMapper(descriptor.author);
}
else if (descriptor.contributors) {
// Somewhat arbitrary, I guess
let contributors;
if (descriptor.contributors.length > 3) {
contributors = descriptor.contributors.slice(0, 3).map(nameMapper);
}
else {
contributors = descriptor.contributors.map(nameMapper);
}
return contributors.join(", ")
}
// Parse Github if nothing else
else if ((descriptor.homepage && descriptor.homepage.includes("github"))) {
return descriptor.homepage.split('/')[3];
}
else if ( descriptor.repository && descriptor.repository.url &&
(typeof descriptor.repository.url === 'string') && descriptor.repository.url.includes("github")) {
return descriptor.repository.url.split('/')[3];
}
else if ( descriptor.repository &&
(typeof descriptor.repository === 'string') && descriptor.repository.includes("github")) {
return descriptor.repository.split('/')[3];
}
else if (descriptor["_resolved"]) {
// We really do not know how to handle it for the time being
return descriptor["_resolved"].split('/')[3];
}
else {
return '';
}
}
/**
* Processes the information of a single module on our crawl through the file system.
*
* @param {string} parentName Name of the project/sub-project being analysed
* @param {string} moduleName Name of the module being analysed.
* @param {string} modulePath Path to the module being analysed
* @param {array} ignoreFiles List of modules to ignore for a particular project
* @param {TreeMap} map The TreeMap where we store information related to a module.
* @param {boolean} legacyMode True if we're parsing a pre npmV8 lock file, false otherwise
*
*/
static processModule(parentName, moduleName, modulePath, ignoreFiles, map, legacyMode = false) {
let packagePath = `${modulePath}/package.json`;
console.log(packagePath)
let info = new DependencyInfo(legacyMode);
// If the file does not exist, we must check to see if it's a module
// we are explicily
if (!fs.existsSync(packagePath)) {
// Check if the key is in the ignored modules for the project
if(ignoreFiles.has(key)) {
return;
}
else {
throw new Error(`Missing info for '${moduleName}' path: '${modulePath}' in ${parentName} project`)
}
}
// Implicit Else: Gather Module Information
let contents = fs.readFileSync(packagePath);
let descriptor = JSON.parse(contents);
let proj = descriptor.name;
let version = descriptor.version;
let license = descriptor.license || '';
if (license && license.type) {
license = license.type;
}
if (!license && descriptor.licenses && Array.isArray(descriptor.licenses)) {
license = descriptor.licenses.map(lic => {
if (typeof lic === 'string') {
return lic;
}
if (typeof lic.type === 'string') {
return lic.type.replace(', ', ' ').replace(',', ' ');
}
return '';
}).join(',');
}
let description = descriptor.description || '';
let homepage = descriptor.homepage || '';
let authorName = this.getPackageAuthors(descriptor);
let repository = (descriptor.repository && descriptor.repository.url ? descriptor.repository.url : undefined) ||
(descriptor.repository ? descriptor.repository : undefined) || '';
let downloadUrl = homepage || repository || '';
info.name = proj;
info.version = version;
info.description = description;
info.license = license;
info.homepage = homepage;
info.repository = repository;
info.downloadUrl = downloadUrl;
info.authorName = authorName;
info.modulePath = modulePath;
info.nameVersion = `${proj}/${version}`;
if (!info.name) {
throw new Error(`Information was not collected for module '${moduleName}' at ${modulePath}`)
}
DependencyInfo.addInfo(map, info);
let subModulesPath = `${modulePath}/node_modules`;
// Keep crawling into the software used by this piece of software if we can.
if (fs.existsSync(subModulesPath)) {
// NPM v8 does not reproduce the hierarchy in the package.lock the same way..
if (legacyMode) {
FsInspector.processDirectory(info.name, subModulesPath, ignoreFiles, info.bundled, legacyMode);
}
else {
FsInspector.processDirectory(info.name, subModulesPath, ignoreFiles, map, legacyMode);
}
}
}
/**
* On crawling the file system, this will process the an entire directory before dispatching functions
* which will later process the individual files within a directory
* @param {string} parentName Name of the project/sub-project being analysed
* @param {string} modulesDir The directory we are currently analysing
* @param {array} ignoreFiles List of modules to ignore for a particular project
* @param {TreeMap} map A tree map we build containing each of the module's information.
* @param {boolean} legacyMode True if we're parsing a pre npmV8 lock file, false otherwise
*/
static processDirectory(parentName, modulesDir, ignoreFiles, map, legacyMode = false) {
let files = fs.readdirSync(modulesDir);
let ignoreDirectories = ConfigHelper.getIgnoreDirectories();
for (let fname of files) {
if (!ignoreDirectories.has(fname)) {
if (!fname.startsWith('@')) {
let modulePath = `${modulesDir}/${fname}`;
let fdata = fs.statSync(modulePath);
if (fdata.isDirectory()) {
FsInspector.processModule(parentName, fname, modulePath, ignoreFiles, map, legacyMode);
}
}
else {
let modulePath = `${modulesDir}/${fname}`;
FsInspector.processDirectory(parentName, modulePath, ignoreFiles, map, legacyMode);
}
}
}
}
// Processing for when the user defines their own file containing dependency information
/**
* Processes an individual module in the event it is within a project were we have
* user defined dependency information.
*
* After processing the module, the dependencies of the module used will be further be explored.
*
* @param {string} parentName Name of the parent module
* @param {string} moduleName The current module we are gathering information for.
* @param {string} modulePath Location of the module in the file system (relative path)
* @param {Array} ignoreFiles A list of files to not explore
* @param {TreeMap} map Where we collect all of our information.
* @param {TreeMap} userDefinedPackage A user defined pakage.json like file.
* @param {boolean} legacyMode True if we're parsing a pre npmV8 lock file, false otherwise
*/
static processUserDefinedModule(parentName, moduleName, modulePath, ignoreFiles, map, userDefinedPackage, legacyMode = false) {
// Some modules in a user defined package will still have their own information, let us collect this info...
if (fs.existsSync(`${modulePath}/package.json`)) {
this.processModule(parentName, moduleName, modulePath, ignoreFiles, map, legacyMode);
return;
}
// Implicit else
let info = new DependencyInfo();
let fullModuleName = `${parentName}/${moduleName}`; // Getting the full name of the module
// If we're to ignore the file, return
if (ignoreFiles.has(fullModuleName)) {
return;
}
// If the module has been defined by the user, get info
if (userDefinedPackage.has(moduleName)) {
let userDefinedModule = userDefinedPackage.get(moduleName);
for(let key in userDefinedModule) {
info[key] = userDefinedModule[key];
}
// Check if we read the name at bare minimum
if (!info.name) {
throw new Error(`Information was not collected for module '${moduleName}' at ${modulePath}`);
}
}
else {
// Otherwise throw an error!
throw new Error(`Missing info for '${moduleName}' path: '${modulePath}' in ${parentName} project`);
}
DependencyInfo.addInfo(map, info); // Add the information to the map
// Sub modules are assumed to not have user defined package info defined
let subModulesPath = `${modulePath}/node_modules`;
// Keep crawling into the software used by this piece of software if we can.
if (fs.existsSync(subModulesPath)) {
// NPM v8 does not reproduce the hierarchy in the package.lock the same way..
if (legacyMode) {
FsInspector.processDirectory(info.name, subModulesPath, ignoreFiles, info.bundled, legacyMode);
}
else {
FsInspector.processDirectory(info.name, subModulesPath, ignoreFiles, map, legacyMode);
}
}
}
/**
* On crawling the file system, this will process the an entire directory before dispatching functions
* which will later process the individual files within a directory
* @param {string} parentName Name of the project/sub-project being analysed
* @param {string} modulesDir The directory we are currently analysing
* @param {array} ignoreFiles List of modules to ignore for a particular project
* @param {TreeMap} map A tree map we build containing each of the module's information.
* @param {TreeMap} userDefinedPackage The user's self defined package information.
* @param {boolean} legacyMode True if we're parsing a pre npmV8 lock file, false otherwise
*/
static processUserDefinedDirectory(parentName, modulesDir, ignoreFiles, map, userDefinedPackage, legacyMode = false) {
let files = fs.readdirSync(modulesDir);
let ignoreDirectories = ConfigHelper.getIgnoreDirectories();
for (let fname of files) {
if (!ignoreDirectories.has(fname)) {
if (!fname.startsWith('@')) {
let modulePath = `${modulesDir}/${fname}`;
let fdata = fs.statSync(modulePath);
if (fdata.isDirectory()) {
FsInspector.processUserDefinedModule(parentName, fname, modulePath, ignoreFiles, map, userDefinedPackage, legacyMode)
}
}
else {
let modulePath = `${modulesDir}/${fname}`;
FsInspector.processUserDefinedDirectory(parentName, modulePath, ignoreFiles, map, userDefinedPackage, legacyMode);
}
}
}
}
/**
* Processes modules defined in a user-supplied package.json where we don't wish to do any crawling of the
* file system. This will be called when a project has the "includesNonJSModules" flag is set to true.
*
* @param {string} parentName Name of the project being analysed
* @param {array} ignoreFiles List of modules to ignore for a particular project
* @param {TreeMap} map A tree map we build containing each of the module's information.
* @param {TreeMap} userDefinedPackage The user's self defined package information.
* @param {boolean} legacyMode True if we're parsing a pre npmV8 lock file, false otherwise
*/
static processNonJSModules(projectName, ignoreFiles, map, userDefinedPackage) {
let collectedInfo;
for (let [dependency, rawInfo] of userDefinedPackage) {
let fullDependencyName = `${projectName}/${dependency}`;
if (!ignoreFiles.has(fullDependencyName)) {
collectedInfo = new DependencyInfo();
// Grab each bit of information on a project
for (let key in rawInfo) {
collectedInfo[key] = rawInfo[key];
}
collectedInfo.nameVersion = `${collectedInfo.name}/${collectedInfo.version}`;
// Check if we read the name at bare minimum
if (!collectedInfo.name) {
throw new Error(`Information was not collected for module '${dependency}' at ${modulePath}`);
}
DependencyInfo.addInfo(map, collectedInfo); // Add the information to the map
}
}
}
}
module.exports = FsInspector;