-
Notifications
You must be signed in to change notification settings - Fork 1
/
config-helper.js
181 lines (153 loc) · 5.81 KB
/
config-helper.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
"use strict";
const defaultConfig = require('./default-config.json');
const profiles = require('./profiles.json')
const {TreeMap, TreeSet} = require("jstreemap");
const fs = require("fs");
const path = require("path");
var archivedConfig; // Assigned to the module scope, so I'm okay with this.
/**
* Loads the user's configuration file. Any field present in the user's config will overwrite that of the default configuration.
* The license white list profile will also be loaded into the config option at this time.
*
* @param {string} pathToConfigFile Path to the user's config file. If the config file does not exist or is not valid JSON, the user
* will be notified and the default configuration will be used.
*
* @return A fully iniitalised config option.
*/
function loadConfig(pathToConfigFile) {
// Default config is loaded due to being required
var config = defaultConfig;
// If the user has provided a config file, load that one and overwrite anything within config.
if (pathToConfigFile) {
try {
let userConfig = fs.readFileSync(pathToConfigFile);
userConfig = JSON.parse(userConfig); // Parse that bad boy
// Overwrite field from the default config with the one in user config.
for (let field in userConfig) {
config[field] = userConfig[field];
}
}
catch (err) {
process.stderr.write(`Provided Config File: ${pathToConfigFile} does not exist or is not valid JSON\n`);
console.log("Using Default Configuration Only");
}
}
// Load the user's license profile
loadLicenseProfile(config.license);
// For better performance we convert many fields of the config to
// TreeMaps or TreeSets.
convertToTree(config);
archivedConfig = config;
return config
}
/**
* Loads the white list of the user's desired license profile into the configuration
* object. Should the profile defined in the config file not be found in profiles.jsonc,
* only the white list defined in the config option will be used.
*
* @param {object} licenseConfig The license object within the user's configuration.
*/
function loadLicenseProfile(licenseConfig) {
var profileWhiteList;
try {
profileWhiteList = profiles[licenseConfig.profile].whiteList;
}
catch (err) {
// If no white list for the profile, inform the user
console.log(`No white list could be found for the profile ${licenseConfig.profile}.`);
console.log("Continuing only with white list found in the config file...");
return;
}
// The white list in license config will, most of the time be smaller so this is a more
// efficient way then doing pushing in place.
profileWhiteList.push(...licenseConfig.whiteList);
licenseConfig.whiteList = profileWhiteList;
// No need to return. Object has been modified in place
}
function convertToTree(config) {
config.license.whiteList = new TreeSet(config.license.whiteList);
config.license.exceptions = new TreeSet(config.license.exceptions);
config.license.alternateLocations = new TreeMap(config.license.alternateLocations);
// Don't waste time and space if we won't use them...
if (config.cryptography.enable) {
config.cryptography.packageList = new TreeSet(config.cryptography.packageList);
config.cryptography.exceptions = new TreeSet(config.cryptography.exceptions);
}
for (let project of config.targetDirectories) {
project.ignore = new TreeSet(project.ignore);
}
config.ignoreDirectories = new TreeSet(config.ignoreDirectories);
config.excludeFromPackSpec = new TreeSet(config.excludeFromPackSpec);
// No need to return, pass by reference
}
/**
* @return Licence info within the Config File
*/
function getLicense() {
return archivedConfig.license;
}
/**
* @return Output File for the Package Report
*/
function getOutputFile() {
return archivedConfig.outputFile;
}
/**
* @return True if we are to analyse the build tools, false otherwise
*/
function getAnalyseBuildTools() {
return archivedConfig.analyseBuildTools;
}
/**
* @return Cryptography info within the Config File
*/
function getCryptography() {
return archivedConfig.cryptography;
}
/**
* @return List with information about the directories we're to analyse
*/
function getTargetDirectories() {
return archivedConfig.targetDirectories;
}
/**
* @return List containing information on the fields to include within the reports.
*/
function getReportFields() {
return archivedConfig.reportFields;
}
/**
* @return List of the directories to be ignored
*/
function getIgnoreDirectories() {
return archivedConfig.ignoreDirectories;
}
/**
*
* @param {string} projectName The name of the project we want to know if we're to ignore dev dependencies for
*
* @return {boolean} Whether we're to ignore dev dependencies for a project.
*/
function getIgnoreDevDependencies(projectName) {
return archivedConfig.targetDirectories.filter(project => projectName === project.name)[0].ignoreDevDependencies;
}
/**
* @param {string} dependencyName The name of a dependency that we check whether we are to exclude from the pack-spec document.
*
* @return {TreeSet} The set of packages we wish to exclude from the pack-spec document.
*/
function getExcludeFromPackSpecSet() {
return archivedConfig.excludeFromPackSpec;
}
module.exports = {
loadConfig: loadConfig,
getLicense: getLicense,
getOutputFile: getOutputFile,
getAnalyseBuildTools: getAnalyseBuildTools,
getCryptography: getCryptography,
getTargetDirectories: getTargetDirectories,
getReportFields: getReportFields,
getIgnoreDirectories: getIgnoreDirectories,
getIgnoreDevDependencies: getIgnoreDevDependencies,
getExcludeFromPackSpecSet: getExcludeFromPackSpecSet
}