-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathtest.js
More file actions
125 lines (123 loc) · 4.34 KB
/
Copy pathtest.js
File metadata and controls
125 lines (123 loc) · 4.34 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
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,
{ dirname } = path,
__filename = url,
__dirname = dirname(url),
ECMA_VERSION = 2020,
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) / 1e3);
spinner.text = chalk.bold.blue(`Checking files... ( ${elapsedTime}s )`);
}, 1e3);
return {
spinner: spinner,
intervalId: intervalId,
startTime: startTime,
};
}
function stopSpinner(spinnerInfo) {
return spinnerInfo
? (spinnerInfo.spinner.stop(),
clearInterval(spinnerInfo.intervalId),
console.log(chalk.bold.green("All files checked successfully.")),
spinnerInfo.startTime)
: null;
}
const runSyntaxCheck = async () => {
const errorResults = [];
let spinnerInfo;
try {
spinnerInfo = startSpinner();
const testFile = async (file) => {
try {
const content = readFileSync(file, "utf8"),
syntaxErrorResult = syntaxError(content, file, {
sourceType: "module",
ecmaVersion: 2020,
allowAwaitOutsideFunction: !0,
allowReturnOutsideFunction: !0,
allowImportExportEverywhere: !0,
});
if (syntaxErrorResult) {
const result = {
"File Name": path.basename(file),
Line: "N/A",
Status: "Error",
Size: "N/A",
Path: path.dirname(file),
"Error Message": syntaxErrorResult.message,
};
errorResults.push(result),
console.error(
chalk.bold.red("An error occurred:"),
syntaxErrorResult,
);
}
} catch (fileReadError) {
const result = {
"File Name": path.basename(file),
Line: "N/A",
Status: "Error",
Size: "N/A",
Path: path.dirname(file),
"Error Message": `File read error: ${fileReadError.message}`,
};
console.error(chalk.bold.red("An error occurred:"), fileReadError),
errorResults.push(result);
}
},
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),
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"])}`,
);
}))
: 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);
}
},
formatResult = (result) => {
const { "File Name": fileName, Line, Status, Size, Path } = result;
return [
chalk.bold.white(`File Name: ${chalk.bold.yellow(fileName)}`),
"N/A" !== Line
? chalk.bold.yellow(`Line: ${chalk.bold.yellow(Line)}`)
: "",
"Success" === Status
? 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();