-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (103 loc) · 3.56 KB
/
index.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
const fs = require('fs-extra');
const chalk = require('chalk');
const readline = require('readline');
const { performance } = require('perf_hooks');
// * lib
const Report = require('./lib/Report');
const Prelog = require('./lib/Prelog');
const { eraseComments, getLangPatterns } = require('./lib/utils');
const { loadConfig } = require('./lib/utils/configUtils');
const { getFilePath, getFilePaths, removeEmptyDir } = require('./lib/utils/fileUtils');
// * patterns
const patterns = getLangPatterns('js');
// * readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const processFile = (filePath, config) => {
const startTime = performance.now();
const { pattern, writeToOutput, replace, outputDir, postfix, excludePatterns } = config;
const jsCode = fs.readFileSync(filePath, 'utf-8');
const outputPath = getFilePath(filePath, outputDir, postfix);
const [commentsRemoved, removedCharsCount] = eraseComments({
code: jsCode,
pattern: pattern,
excludePatterns: excludePatterns,
});
if (writeToOutput) {
if (replace) {
fs.writeFileSync(filePath, commentsRemoved);
} else {
fs.writeFileSync(outputPath, commentsRemoved);
}
}
const endTime = performance.now();
const elapsedTime = endTime - startTime;
return {
filePath,
outputPath,
commentsRemoved,
removedCharsCount,
elapsedTime,
};
};
const processFiles = (filePaths, config) => {
const report = new Report();
filePaths.forEach(f => {
const log = processFile(f, config);
report.append(log);
});
return report;
};
const interactiveProcessFiles = (prelog, config) => {
const report = new Report();
const question = `Edit ${chalk.bold.blue(prelog.fileName)} and then press enter to continue`;
rl.question(question, () => {
const filePaths = prelog.readLines();
filePaths.forEach(f => {
const log = processFile(f, config);
report.append(log);
});
rl.close();
});
return report;
};
const eraseFromString = (code, config = {}) => {
const startTime = performance.now();
const { type = 'both', excludePatterns = [], output = undefined } = config;
const pattern = patterns[type];
let outputPath = null;
const [commentsRemoved, removedCharsCount] = eraseComments({ code, pattern, excludePatterns });
if (output) {
const { path = '', file = 'output.js', append = false } = output;
const filePath = getFilePath(file, path);
if (path) fs.mkdirsSync(path, { recursive: true });
fs.writeFileSync(filePath, commentsRemoved, { flag: append ? 'a' : 'w' });
outputPath = filePath;
}
const endTime = performance.now();
const elapsedTime = Report.formatElapsedTime(endTime - startTime);
return [commentsRemoved, removedCharsCount, outputPath, elapsedTime];
};
const erase = (configPath = 'eraser.config.json') => {
const config = loadConfig(configPath);
const { type, include, exclude, writeToOutput, outputDir, postfix, interactive } = config;
config.pattern = patterns[type];
if (writeToOutput) fs.emptyDirSync(outputDir);
process.on('exit', () => removeEmptyDir(outputDir));
const filePaths = getFilePaths(include, exclude, postfix);
if (interactive) {
const prelog = new Prelog();
prelog.writeLines(filePaths);
const report = interactiveProcessFiles(prelog, config);
rl.on('close', () => report.print(interactive));
return;
}
console.time(chalk.bold.green('erased'));
rl.close();
const report = processFiles(filePaths, config);
report.print();
return report.logs;
};
module.exports = { erase, eraseFromString };