forked from RevRocky/pack-it-in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.js
114 lines (97 loc) · 4.06 KB
/
collector.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
'use strict';
const DependencyInfo = require('./dependency-info');
const LockParser = require('./lock-parser');
const FsInspector = require('./fs-inspector');
const ConfigHelper = require('./config-helper');
const fs = require("fs");
const {TreeMap, TreeSet} = require('jstreemap');
class Collector {
constructor(project) {
this.project = project; // No need to duplicate references.
this.infos = new TreeMap();
}
flattenInfos(srcInfos, dstInfos) {
for (let [name, version, info] of DependencyInfo.forEachRecursively(srcInfos)) {
DependencyInfo.mergeInfo(dstInfos, info);
}
}
/**
* Merges a second collector into our collector.
* @param {Collector} col A second collector we wish to merge into our collector
*/
mergeWith(col) {
for (let [name, version, info] of DependencyInfo.forEach(col.infos)) {
DependencyInfo.mergeInfo(this.infos, info);
}
}
splitDevInfos() {
let devInfos = new TreeMap();
let projInfos = new TreeMap();
for (let [name, version, info] of DependencyInfo.forEach(this.infos)) {
if (info.prod) {
DependencyInfo.addInfo(projInfos, info);
}
else {
DependencyInfo.addInfo(devInfos, info);
}
}
return [projInfos, devInfos];
}
/**
* Purges all information pertaining to the dev dependencies from our collection of dependency information.
* Note: This function will only be called if the user has elected to ignoreDevDependencies for a given project.
* Otherwise, the information about dev dependencies will be maintained and, per usual, written out to the second
* of the excel spreadsheets.
*
* @param {TreeMap} topFileInfos The collection of all file information for the project.
*/
purgeDevDependencies(topFileInfos) {
for (const topModuleInfo of topFileInfos) { // For each module in the project
for (const versionInfo of topModuleInfo[1]) { // For each version of the module within the project
if (versionInfo[1].dev) { // If it's a dev dependency, delete it.
topModuleInfo[1].delete(versionInfo[0]);
}
}
// Check to see if there are aby versions left to speak of...
if (topModuleInfo[1].size === 0) {
topFileInfos.delete(topModuleInfo[0]);
}
}
}
run() {
let topFileInfos = new TreeMap();
let modulesPath = this.project.parentDirectory;
// If it is a project, append the path onto node modules
if (this.project.isProject) {
modulesPath = `${this.project.parentDirectory}/node_modules`
}
if (this.project.userDefinedDependency) {
let dependencyInfo = fs.readFileSync(this.project.userDefinedDependency);
dependencyInfo = JSON.parse(dependencyInfo);
dependencyInfo = new TreeMap(dependencyInfo.dependencies);
FsInspector.processUserDefinedDirectory(this.project.name, modulesPath, this.project.ignore, topFileInfos, dependencyInfo);
}
else {
FsInspector.processDirectory(this.project.name, modulesPath, this.project.ignore, topFileInfos);
}
// Processing the package-lock.json files
if (this.project.hasPackage) {
let lockParser = new LockParser(this.project.parentDirectory, topFileInfos);
lockParser.processLockFile();
} else {
for (let [name, version, info] of DependencyInfo.forEach(topFileInfos)) {
if (this.project.isDev) {
info.dev = true;
}
else {
info.prod = true;
}
}
}
if (this.project.ignoreDevDependencies) {
this.purgeDevDependencies(topFileInfos);
}
this.flattenInfos(topFileInfos, this.infos);
}
};
module.exports = Collector;