-
Notifications
You must be signed in to change notification settings - Fork 172
/
code_quality.js
392 lines (357 loc) · 11.7 KB
/
code_quality.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
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
const gitChangedFiles = require('git-changed-files');
const prettier = require('prettier');
const prettierConfig = require('./prettier_config.json');
const codeQualityConfig = require('./package.json').codeQuality || {};
const path = require('path');
const fs = require('fs');
const { ESLint } = require('eslint');
const eslint = new ESLint({ fix: true });
const AnsiUp = require('ansi_up').default;
const ansiup = new AnsiUp();
/**
* Get all files, excluding automatically generated or imported from external libraries.
* @param dir: path from where recursively get all the files
* @returns {[]}: list of files (absolut path)
*/
const getAllFiles = () => {
const options = getOptions();
if (!options.sourcePaths) {
return getAllFilesRecoursive(__dirname);
}
const sourcePaths = getSourcePaths(options.sourcePaths);
let results = [];
sourcePaths.forEach((sourcePath) => {
const files = getAllFilesRecoursive(sourcePath);
results = results.concat(files);
});
return results;
};
const getSourcePaths = (sourcePaths) => {
return sourcePaths.split(',').map((sourcePath) => path.resolve(__dirname, ...sourcePath.split('/')));
};
const getAllFilesRecoursive = (dir) => {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(function (file) {
file = dir + '/' + file;
const stat = fs.statSync(file);
if (fileToExclude(file, stat)) {
return [];
}
if (stat && stat.isDirectory()) {
/* Recurse into a subdirectory */
results = results.concat(getAllFilesRecoursive(file));
} else {
/* Is a file */
results.push(file);
}
});
return results;
};
const fileToExclude = (file, stat) => {
if (stat && stat.isDirectory()) {
return (
file.endsWith('node_modules') ||
file.indexOf('.git') !== -1 ||
file.indexOf('/dist/') !== -1 ||
file.indexOf('\\dist\\') !== -1
);
}
const fileName = path.basename(file);
return /(.git|.min.css|.min.js)$/.test(fileName);
};
/**
* Return all changed files that will be commit to git branch.
* @returns {Promise<*>}: promise wiht list of files.
*/
const getChangedFiles = async () => {
const committedGitFiles = await gitChangedFiles({ baseBranch: 'main' });
return committedGitFiles.unCommittedFiles.filter(
(file) => fs.existsSync(file) && !file.endsWith('package-lock.json')
);
};
/**
* Group list of file paths by extension.
* @param files: array of file absolute paths
* @returns {key: 'File extension:, value: Array of file absolute paths}
*/
const groupFilesByExtension = (files) => {
return files.reduce((map, file) => {
try {
if (file.startsWith('./') || file.indexOf('.') === -1) {
return map;
}
const extension = file.substring(file.lastIndexOf('.') + 1).toLowerCase();
const array = map[extension] || [];
array.push(file);
map[extension] = array;
return map;
} catch (e) {
console.error('error --> ', e);
return {};
}
}, {});
};
/**
* Execute prettier and write result on same file.
* @param file: absolute class path
* @param config: configuration that will be used to prettier the file.
*/
const prettifyFile = async (file, config) => {
try {
if (config?.excludedFiles?.includes(file)) {
return;
}
const fileContent = fs.readFileSync(file).toString();
const isFormatted = await prettier.check(fileContent, config);
if (!isFormatted) {
console.log('Running prettier on the file: ' + file);
const format = await prettier.format(fileContent, config);
fs.writeFileSync(file, format);
return true;
}
return false;
} catch (error) {
console.log('Error in running prettier the file ' + file + ': \n' + error);
}
};
/**
* Applying prettier on several files. We have a specific configuration for file extension on file prettier_config.json.
* @param filesByExtension: we pass a Map json object where the key is the extension, value is an Array with absolute file paths
*/
const prettifyFiles = (filesByExtension) => {
let filesChanged = 0;
if (!codeQualityConfig.usePrettier) {
return; // no need to use pretty;
}
Object.keys(filesByExtension).forEach((extension) => {
const files = filesByExtension[extension];
const config = prettierConfig[extension];
if (!config) {
console.log(
"We don't have any configuration for extension " + extension + ', please add it in file prettier_config.json'
);
return;
}
files.forEach(async (file) => {
const action = await prettifyFile(file, config);
if (action) {
filesChanged++;
}
});
});
return filesChanged;
};
/**
* ESlint analysis and fix on an Array of files.
* @param files: array with absolute file paths
* @returns {Promise<{}|{report: string, error: boolean}>}
*/
const eslintFiles = async (files) => {
if (!codeQualityConfig.useEslint) {
return {};
}
const formatter = await eslint.loadFormatter('stylish');
let error = false;
let report = '';
for (const file of files) {
try {
const result = await eslint.lintFiles(file);
error = error || result.some((res) => res.errorCount > 0);
await ESLint.outputFixes(result);
const resultText = formatter.format(result);
if (!!resultText && resultText.trim().length > 0) {
report += resultText;
}
} catch (exception) {
error = true;
report += 'Error in eslint file ' + file + ':\n' + exception;
}
}
return { error, report };
};
/**
* Run `prettier` on changed files before commit;
* You can also call this function using: npm run code-quality-prettier
*/
const preCommitPrettier = async (filesByExtension) => {
if (!filesByExtension) {
const files = await getChangedFiles();
if (!files) {
console.log("Couldn't find any file that hand been changed");
return;
}
console.log('File to be analyzed before commit:\n' + files.join('\n'));
filesByExtension = groupFilesByExtension(files);
}
return prettifyFiles(filesByExtension);
};
/**
* Run `eslint` on changed files before commit;
* You can also call this function using: npm run code-quality-eslint
*/
const preCommitEslint = async (filesByExtension) => {
if (!filesByExtension) {
const files = await getChangedFiles();
if (!files) {
console.log("Couldn't find any file that hand been changed");
return;
}
console.log('File to be analyzed before commit:\n' + files.join('\n'));
filesByExtension = groupFilesByExtension(files);
}
const esLintResult = await eslintFilesByExtension(filesByExtension);
if (esLintResult.error) {
console.log('Please fix these errors before to commit:' + esLintResult.report);
console.log('Eslint executed in ' + esLintResult.numberFiles + ' files ');
if (codeQualityConfig.eslintStopCommit) {
process.exit(1);
}
} else {
console.log('Eslint executed in ' + esLintResult.numberFiles + ' files ');
}
};
/**
* Run `prettier` and `eslint` on changed files before commit;
* You can also call this function using: npm run code-quality
*/
const preCommit = async () => {
const files = await getChangedFiles();
let shouldFail = false;
if (!files) {
console.log("Couldn't find any file that hand been changed");
return;
}
console.log('File to be analyzed before commit:\n' + files.join('\n'));
const filesByExtension = groupFilesByExtension(files);
if (codeQualityConfig.usePrettier) {
console.log('\x1b[33m%s\x1b[0m', 'Running Prettier in pre-commit');
const nrModifiedFiles = await preCommitPrettier(filesByExtension);
shouldFail = nrModifiedFiles > 0;
if (shouldFail) {
console.log(
'\x1b[31m%s\x1b[0m',
`Prettier has modified ${nrModifiedFiles} file(s). Commit will be cancelled. Stage changes and run again.`
);
}
} else {
console.log('Prettier is disabled. Skipping...');
}
if (codeQualityConfig.useEslint) {
console.log('\x1b[33m%s\x1b[0m', 'Running ESlint in pre-commit');
await preCommitEslint(filesByExtension);
} else {
console.log('ESlint is disabled. Skipping...');
}
return shouldFail;
};
/**
* Execute ESlint analysis and try to fix problems
* @param filesByExtension: we pass a Map json object where the key is the extension, value is an Array with absolute file paths
* @returns {Promise<{report: string, numberFiles: number, error: boolean}>}
*/
const eslintFilesByExtension = async (filesByExtension) => {
let error = false;
let report = '';
let numberFiles = 0;
const extensions = Object.keys(filesByExtension);
for (const extension of extensions.filter((extension) => extension === 'ts' || extension === 'js')) {
const esLintFiles = filesByExtension[extension];
numberFiles += esLintFiles.length;
const eslintResult = await eslintFiles(esLintFiles);
error = error || eslintResult.error;
if (!!eslintResult.report && eslintResult.report.trim().length > 0) {
report += eslintResult.report;
}
}
return { error, report, numberFiles };
};
/**
* Run `prettier` and `eslint` on all project files;
* You can also call this function using: npm run full-code-quality
*/
const full = async () => {
const files = getAllFiles();
const filesByExtension = groupFilesByExtension(files);
await fullPrettier(filesByExtension);
console.log('Running EsLint on all files. Please wait...');
await fullEslint(filesByExtension);
};
/**
* Run `prettier` on all project files;
* You can also call this function using: npm run full-code-quality
*/
const fullPrettier = async (filesByExtension) => {
if (!filesByExtension) {
const files = getAllFiles();
filesByExtension = groupFilesByExtension(files);
}
prettifyFiles(filesByExtension);
};
/**
* Run `eslint` on all project files;
* You can also call this function using: npm run full-code-quality
*/
const fullEslint = async (filesByExtension) => {
if (!filesByExtension) {
const files = getAllFiles();
filesByExtension = groupFilesByExtension(files);
}
const esLintResult = await eslintFilesByExtension(filesByExtension);
const options = getOptions();
const reportFile = options.report || 'full_eslint_report.html';
if (esLintResult.error) {
fs.writeFileSync(reportFile, ansiup.ansi_to_html(esLintResult.report).replace(/(?:\r\n|\r|\n)/g, '<br/>'));
console.log('Wrote eslint report to file ' + path.resolve(reportFile));
}
console.log(
'Eslint executed in ' + esLintResult.numberFiles + ' files. Results written in "full_eslint_report.html"'
);
};
/**
* We parse application node parameters; they must be in format -- key1=value1 key2=value2
* @returns {{}} an json Map object
*/
const getOptions = () => {
const options = {};
for (let i = 0; i < process.argv.length; i++) {
const arg = process.argv[i];
if (!arg.includes('=')) {
continue;
}
const key = process.argv[i].substring(0, process.argv[i].indexOf('='));
const value = process.argv[i].substring(process.argv[i].indexOf('=') + 1);
options[key] = value;
}
return options;
};
/**
* This is the method that will be executed with node code_quality.js
*/
(async () => {
const options = getOptions();
if (options.mode === 'pre_commit') {
return await preCommit();
}
if (options.mode === 'pre_commit_prettier') {
return await preCommitPrettier();
}
if (options.mode === 'pre_commit_eslint') {
return await preCommitEslint();
}
if (options.mode === 'full') {
return await full();
}
if (options.mode === 'full_prettier') {
return await fullPrettier();
}
if (options.mode === 'full_eslint') {
return await fullEslint();
}
console.error(
'You need to pass application parameter -- mode=pre_commit|pre_commit_prettier|pre_commit_eslint|full|full-prettier|full-eslint'
);
})().catch((err) => {
console.log(err);
process.exit(1);
});