forked from AyGemuy/Taylor-V2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
144 lines (125 loc) · 4.45 KB
/
Copy pathtest.js
File metadata and controls
144 lines (125 loc) · 4.45 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
import stripAnsi from 'strip-ansi';
import path from 'path';
import {
readFileSync
} from 'fs';
import * as glob from 'glob';
import chalk from 'chalk';
import ora from 'ora';
import syntaxError from 'syntax-error';
const {
url
} = import.meta;
const {
dirname
} = path;
const __filename = url;
const __dirname = dirname(url);
const ECMA_VERSION = 2020;
const NOT_APPLICABLE = 'N/A';
function startSpinner() {
const spinner = ora({
text: chalk.bold.blue('🚀 Starting test...'),
spinner: 'moon'
}).start();
let startTime = new Date();
const intervalId = setInterval(() => {
const elapsedTime = Math.round((new Date() - startTime) / 1000);
spinner.text = chalk.bold.blue(`Checking files... ( ${elapsedTime}s )`);
}, 1000);
return {
spinner,
intervalId,
startTime,
};
}
function stopSpinner(spinnerInfo) {
if (spinnerInfo) {
spinnerInfo.spinner.stop();
clearInterval(spinnerInfo.intervalId);
console.log(chalk.bold.green('All files checked successfully.'));
return spinnerInfo.startTime;
} else {
return null;
}
}
const runSyntaxCheck = async () => {
const errorResults = [];
let spinnerInfo;
try {
spinnerInfo = startSpinner();
const testFile = async (file) => {
try {
const content = readFileSync(file, 'utf8');
const syntaxErrorResult = syntaxError(content, file, {
sourceType: 'module',
ecmaVersion: ECMA_VERSION,
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true,
allowImportExportEverywhere: true,
});
if (syntaxErrorResult) {
const result = {
'File Name': path.basename(file),
Line: NOT_APPLICABLE,
Status: 'Error',
Size: NOT_APPLICABLE,
Path: path.dirname(file),
'Error Message': syntaxErrorResult.message,
};
errorResults.push(result);
}
} catch (fileReadError) {
const result = {
'File Name': path.basename(file),
Line: NOT_APPLICABLE,
Status: 'Error',
Size: NOT_APPLICABLE,
Path: path.dirname(file),
'Error Message': `File read error: ${fileReadError.message}`,
};
errorResults.push(result);
}
};
const files = glob.sync('**/*.js', {
cwd: __dirname,
ignore: ['node_modules/**', __filename, '**/run.js'],
});
for (const file of files) {
await testFile(file);
const progressPercentage = ((files.indexOf(file) + 1) / files.length) * 100;
spinnerInfo.spinner.text = `${chalk.bold.blue('🚀 Starting test...')} ${chalk.bold.gray('Checking files...')} ${chalk.bold.magenta(` ${Math.floor(progressPercentage)}% `)}`;
spinnerInfo.spinner.render();
}
stopSpinner(spinnerInfo);
if (errorResults.length > 0) {
console.log(chalk.bold.red(`\nTotal Errors: ${errorResults.length}`));
console.log('List Errors:');
errorResults.forEach((result, index) => {
console.log(`${index + 1}. ${formatResult(result)}\nError Message: ${chalk.bold.red(result['Error Message'])}`);
});
} else {
console.log(chalk.bold.magenta('\nNo syntax errors found.'));
}
console.log(chalk.bold.magenta('All processes completed.'));
} catch (error) {
console.error(chalk.bold.red('An error occurred:'), error);
}
};
const formatResult = (result) => {
const {
'File Name': fileName,
Line,
Status,
Size,
Path
} = result;
return [
chalk.bold.white(`File Name: ${chalk.bold.yellow(fileName)}`),
Line !== NOT_APPLICABLE ? chalk.bold.yellow(`Line: ${chalk.bold.yellow(Line)}`) : '',
Status === 'Success' ? chalk.bold.green(`Status: ${chalk.bold.blue(Status)}`) : chalk.bold.red(`Status: ${chalk.bold.blue(Status)}`),
chalk.bold.blue(`Size: ${chalk.bold.green(Size)}`),
chalk.bold.yellow(`Path: ${chalk.bold.blue(Path)}`),
].join(' ');
};
runSyntaxCheck();