-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·559 lines (514 loc) · 17.4 KB
/
index.js
File metadata and controls
executable file
·559 lines (514 loc) · 17.4 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#! /usr/bin/env node
const fs = require("fs");
const es = require("espree");
const escodegen = require("escodegen");
const path = require("path");
const chalk = require("chalk");
const sloc = require("sloc");
const AppBuilder = require("./lib/models/AppBuilder");
const ModuleBuilder = require("./lib/models/ModuleBuilder");
const generator = require("./lib/Generator");
const Analyzer = require("./lib/Analyzer");
const Reducer = require("./lib/Reducer");
const Detector = require("./lib/Detector");
const config = require("./lib/Configurator");
const stat = require("./lib/Stat");
const utils = require("./lib/utils");
const packageDependencies = require("./lib/utils/package-dependencies");
const createLogger = require("./lib/Logger");
let logger = createLogger();
if (config.silent) {
logger = createLogger("error");
} else if (config.verbose) {
logger = createLogger("debug");
}
let location = config.origin;
let utf = "utf-8";
location = path.resolve(location);
if (location.indexOf("package.json") !== -1) {
location = path.dirname(location);
}
let _app = new AppBuilder();
let entries = [];
(async function () {
console.time("overall");
try {
console.info(`Starting with app at ${location}`);
await init();
console.info("[index.js] Started Traversing");
await traverseGenerateModule(location, _app.type);
console.info("[index.js] Finished Traversing");
console.info("[index.js] Started Detector");
await Detector(_app, entries);
console.info("[index.js] Finished Detector");
if (_app.usedComplicatedDynamicImport) {
throw new Error("DYNAMIC_IMPORT_DETECTED");
}
if (config.mode === "fine") {
console.info("[index.js] Started building dependency graph");
await buildDependency();
console.info("[index.js] Finished building dependency graph");
console.info("[index.js] Started final reduction");
// 1. iterate modules' memusage
// 2. map results to app.globals by name
// 3. add to individual modules used globals.
for (let modul of _app.modules) {
for (let mem in modul.memusages) {
_app.globals.forEach((globalVar) => {
if (globalVar.name === mem) {
for (let m of modul.memusages[mem]) {
globalVar.members.push(m);
}
}
});
}
}
for (let modul of _app.modules) {
for (let globalVar of _app.globals) {
if (globalVar.path === modul.path) {
for (let m of globalVar.members) {
modul.used.push(m);
}
}
}
}
let usedExternalModules = _app.modules.filter(
(m) => m.isUsed && !m.skipReduce
);
for (let modul of usedExternalModules) {
console.debug(`[index.js] Reducing the module "${modul.path}"`);
await reduceModule(modul);
console.debug(
`[index.js] Finished reducing the module "${modul.path}"`
);
}
console.info(`[index.js] Finished final reduction`);
}
if (!config.skipRemove) {
console.info(`[index.js] Deleting unused modules(files)`);
if (!_app.usedComplicatedDynamicImport) {
let modulesToRemove = _app.modules.filter((m) => !m.isUsed);
for (let modul of modulesToRemove) {
await utils.removeFile(modul, config.dryRun);
}
} else if (config.mode === "fine") {
// run reduction with dimports
for (const dimport of _app.dimports) {
console.debug(
"[index.js] Reducing modules with dynamic imports",
_app.dimports
);
let reducableModules = _app.modules.filter(
(m) =>
!m.skipReduce &&
dimport.members.every((v) => m.members.includes(v))
);
for (let modul of reducableModules) {
await reduceModule(modul, dimport.members);
}
}
}
console.info(`[index.js] Finished deleting unused modules(files)`);
}
console.info(`[index.js] Generating codes for modules`);
let usedModules = _app.modules.filter((m) => !m.isRemoved && !m.skipReduce);
for (var modul of usedModules) {
if (!modul.parseError) {
generator.generate(modul, config.dryRun);
}
}
console.info(`[index.js] Finished generating codes for modules`);
console.info(`[index.js] Calculating overall statistics`);
utils.calculateAppStatictic(_app);
console.info(`[index.js] Finished calculating overall statistics`);
if (config.log) {
console.info(
`[index.js] Creating final statistics file "${config.logOutput}"`
);
let log_path = path.join(location, config.logOutput);
fs.writeFileSync(log_path, JSON.stringify(_app, null, 2), {
encoding: "utf-8",
});
console.info(
`[index.js] Finished creating statistics file. Statistics are stored in "${log_path}"`
);
}
} catch (err) {
throw err;
}
console.timeEnd("overall");
})().catch((e) => {
console.error(e);
process.exit(1);
});
// Initializes the initial state of the application.
async function init() {
if (!config.dryRun) {
generator(location, config.destination);
location = path.resolve(config.destination);
}
if (!fs.existsSync(path.join(location, "package.json"))) {
console.error("Couldn't find package.json");
process.exit(1);
}
let content = fs.readFileSync(path.join(location, "package.json"), {
encoding: utf,
});
let packageJson = JSON.parse(content);
_app.appname = packageJson.name;
_app.version = packageJson.version;
_app.type = packageJson.type || "commonjs";
_app.path = location;
if (packageJson.exports) {
for (var key in packageJson.exports) {
// for now we do not consider difference among these multiple entrypoints
let tempVal = packageJson.exports[key];
if ( (key !== 'types') && (typeof tempVal === 'string') ) {
_app.main.push(tempVal);
} else if (typeof tempVal === 'object') {
for (var nestedKey in tempVal) {
if ((nestedKey !== 'types') && (typeof tempVal[nestedKey] === 'string') ) {
_app.main.push(tempVal[nestedKey]);
}
}
}
}
} else {
_app.main.push(utils.entryPoint(location, packageJson.main));
}
// add more entry points from test scripts
let testEntryPoints = getJSFilenamesInScriptsField(packageJson);
if (testEntryPoints && testEntryPoints.length > 0) {
//TODO-Hui: here do we need to find the path to these test entry points?
//TODO-Hui: also, with these test entry points, perhaps we should not block test directories?
testEntryPoints.forEach((entryP) => {
if (!_app.main.includes(entryP)) {
_app.main.push(entryP);
}
});
}
if (packageJson.hasOwnProperty("directories")) {
if (packageJson.directories.hasOwnProperty("test")) {
_app.directories = packageJson.directories.test;
}
}
if (config.seeds.length === 0) {
if (!_app.main) {
throw new Error("NO_ENTRY_POINT");
}
_app.main.forEach((entryP) => {
entries.push(path.join(_app.path, entryP));
});
} else {
for (let seed of config.seeds) {
let s = utils.entryPoint(location, seed);
if (s) {
entries.push(path.join(location, seed));
} else {
console.error(chalk.default.red(`Can not find ${seed} in ${location}`));
}
}
}
if (packageJson.dependencies) {
_app.declaredDependencyCount = Object.keys(packageJson.dependencies).length;
let packageLock = path.join(location, "package-lock.json");
if (fs.existsSync(packageLock)) {
content = fs.readFileSync(path.join(location, "package-lock.json"), {
encoding: utf,
});
packageJson = JSON.parse(content);
packageDependencies.installedPackages(packageJson, _app.dependencies);
_app.installedUniqueDependencyCount = Object.keys(
_app.dependencies
).length;
for (let i in _app.dependencies) {
_app.installedTotalDependencyCount += _app.dependencies[i].length;
}
}
}
}
// Builds the dependency graph of the application
async function buildDependency() {
console.info(`[index.js] Building the dependency graph of ${_app.appname}`);
for (let entry of entries) {
let entryModule = _app.modules.find((m) => {
return m.path === entry;
});
if (!entryModule) {
console.error(
`[index.js] Critical error, can not find entry point module "${entry}". Exiting the process...`
);
process.exit(1);
}
entryModule.skipReduce = true;
entryModule.isUsed = true;
await readModule(entryModule);
}
}
/**
* @param {ModuleBuilder} modul
*/
async function readModule(modul) {
try {
/**
* Reducer part:
* it will skip reduction if:
* - module was marked with "SkipReduce" flag
* - or --skip-reduce flag was setup when script started
*/
if (!config.skipReduction && !modul.skipReduce) {
await reduceModule(modul);
}
// Analyzer part
console.info(`[index.js] Started analysis of ${modul.path}`);
let result = await Analyzer.analyze(modul);
console.debug(`[index.js] Analysis of ${modul.name} returned ${result}`);
console.info(`[index.js] Finished analysis of ${modul.path}`);
// for chaining. Analyze the descendent module if available
for (let i of modul.descendents) {
let descendent = _app.modules.find((m) => {
return m.path === i;
});
if (descendent) {
if (!descendent.ancestors.includes(modul.path))
descendent.ancestors.push(modul.path);
Array.prototype.push.apply(descendent.used, modul.used);
descendent.isUsed = true;
if (modul.skipReduce) descendent.skipReduce = true;
await readModule(descendent);
}
}
/**
* Invokes readModule for module's child
* only if child's usage has changed
*/
let changed = false;
for (let cindex in modul.children) {
let child = modul.children[cindex].ref;
if (!child) continue;
if (modul.dynamicChildren.includes(cindex)) {
child.skipReduce = true;
}
child.isUsed = true;
for (let e of modul.children[cindex].used) {
if (!child.used.includes(e)) {
child.used.push(e);
changed = true;
}
}
if (changed) {
// calling readModule only if child was updated
console.debug(
`[index.js] Changed the child of "${modul.path}" "${child.path}" is used "${child.used}"`
);
await readModule(child);
changed = false;
}
}
} catch (err) {
throw err;
}
}
/**
* Traverses given directory and create ModuleBuilder object for each .js file
* @returns {Boolean}
* @param {String} directory
* @param {String} packageJsonType Type of package.json. Can be modified if there's a child package.json.
*/
async function traverseGenerateModule(directory, packageJsonType) {
console.info(`[index.js] Traversing directory ${directory}`);
try {
var folderContent = fs.readdirSync(directory);
// override type if there's a package.json in this directory
if (fs.existsSync(path.join(directory, "package.json"))) {
let content = fs.readFileSync(path.join(location, "package.json"), {
encoding: utf,
});
let packageJson = JSON.parse(content);
if (packageJson.hasOwnProperty("type")) {
packageJsonType = packageJson.type;
} else {
packageJsonType = "commonjs";
}
}
for (let item of folderContent) {
// ignore list (configure for your use case)
if (config.ignored.includes(item.toLowerCase())) continue;
let itemPath = path.join(directory, item);
let stat = fs.statSync(itemPath);
if (stat.isDirectory()) {
/* if (config.includeTestFolders === false) {
if (_app.testsDirectory) {
if (item === _app.testsDirectory) continue;
} else {
if (item.toLowerCase().startsWith("test")) {
continue;
}
}
} */
await traverseGenerateModule(itemPath, packageJsonType);
} else if (stat.isFile()) {
let itemPathExtension = path.extname(itemPath).toLowerCase();
if (isValidJavascriptFile(itemPath)) {
var _module = new ModuleBuilder();
_module.app = _app;
_module.name = path.basename(itemPath);
_module.path = itemPath;
switch (packageJsonType) {
case "commonjs":
if ([".js", ".cjs", ""].includes(itemPathExtension)) {
_module.type = "commonjs";
} else if (itemPathExtension === ".mjs") {
_module.type = "module";
} else {
_module.type = packageJsonType;
console.warn(item + " :: File extention not supported");
}
break;
case "module":
if ([".js", ".mjs", ""].includes(itemPathExtension)) {
_module.type = "module";
} else if (itemPathExtension === ".cjs") {
_module.type = "commonjs";
} else {
_module.type = packageJsonType;
console.warn(item + " :: File extention not supported");
}
break;
default:
console.warn("Unsupported package.json type");
break;
}
if (directory.indexOf("/node_modules", location.length - 1) === -1) {
_module.isOwned = true;
// should we add do not reduce here?
} else {
let arr = _module.path.split("/");
let lastIndexNodeModules = arr.lastIndexOf("node_modules");
_module.packagename = arr[lastIndexNodeModules + 1];
// scoped packages packagename
if (_module.packagename.startsWith("@")) {
_module.packagename += "/" + arr[lastIndexNodeModules + 2];
}
}
_module.initialSrc = fs.readFileSync(`${_module.path}`, utf);
await generateModuleStatistics(_module);
_app.modules.push(_module);
}
}
}
} catch (err) {
console.error(
`[index.js] Traversing directory ${directory} returned error ${err.message}`
);
throw new Error(err);
}
return true;
}
/**
* Generate a general statistic for the module
* @param {ModuleBuilder} modul
*/
async function generateModuleStatistics(modul) {
try {
if (modul.initialSrc.startsWith("#!")) {
modul.hashbang = modul.initialSrc.split("\n", 1)[0]; // saves the hashbang value
modul.initialSrc = modul.initialSrc.replace(/^#!(.*\n)/, ""); // removes hashbang value
}
let ast = es.parse(modul.initialSrc, {
sourceType: modul.type || "commonjs",
ecmaVersion: 14,
range: true,
comment: true,
tokens: true,
});
modul.ast = ast;
//calculating the SLOC
let gen = escodegen.generate(modul.ast);
modul.initialSloc = sloc(gen, "js").source;
modul.finalSloc = modul.initialSloc;
} catch (ex) {
console.warn(
`[index.js] Error occured while parsing "${modul.path}". Error message: ${ex.message}. Module type: ${modul.type}`
);
modul.parseError = true;
} finally {
modul.initialSrc = null;
}
try {
if (!config.skipStat && !modul.parseError) {
await stat(modul);
}
} catch (error) {
console.error(
`[index.js] Module ${modul.path} stat returned an error ${error.message}`
);
throw new Error(`STAT FAILED: ${modul.path} ${error.message}`);
}
return modul;
}
/**
* Executes Reducer.reduce function on a provided module
* @param {ModuleBuilder} modul
*/
async function reduceModule(modul, extra = null) {
console.debug(`[index.js] reducing the module ${modul.path}`);
let additional = [];
if (extra && extra.length > 0) {
Array.prototype.push.apply(additional, extra);
}
for (let i of modul.descendents) {
let descendent = _app.modules.find((m) => {
return m.path === i;
});
if (descendent) Array.prototype.push.apply(additional, descendent.selfUsed);
}
for (let i of modul.ancestors) {
let ancestor = _app.modules.find((m) => {
return m.path === i;
});
Array.prototype.push.apply(additional, ancestor.selfUsed);
}
await Reducer.reduce(modul, additional);
}
// Acceptable: .js, .cjs, .mjs files. Blank file extensions without the dot.
// Unacceptable: dot files without extension. Like .gitignore, .eslintrc
function isValidJavascriptFile(file) {
let extension = path.extname(file).toLowerCase();
if ([".js", ".cjs", ".mjs", ""].includes(extension) === false) {
return false;
}
let fileSplitBySlash = file.split("/");
if (
extension === "" &&
fileSplitBySlash[fileSplitBySlash.length - 1].startsWith(".")
) {
return false;
}
return true;
}
// parse a field in Package.JSON to retrieve the names of valid JavaScript files
function getJSFilenamesInScriptsField(packageJson) {
let result = [];
if (packageJson.hasOwnProperty("scripts")) {
let scripts = packageJson.scripts;
for (let key in scripts) {
let stringToDealWith = scripts[key];
let words = stringToDealWith.split(" ");
for (let wPotentialFile of words) {
if (wPotentialFile.indexOf("*") > -1) {
continue;
}
let splitWordArr = wPotentialFile.split(".");
if (splitWordArr.length > 1) {
if (
["js", "mjs", "cjs"].includes(splitWordArr[splitWordArr.length - 1])
) {
result.push(wPotentialFile);
}
}
}
}
}
return result;
}