-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
210 lines (193 loc) · 7.6 KB
/
index.ts
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
"use strict"
import {Command, ConfigProvider} from "./ConfigProvider";
import {CheckResults, CodeTesterInterface} from "./CodeTesterInterface";
import {fs} from 'memfs';
import archiver from "archiver";
import chalk from 'chalk';
import Progress, {asyncPrompt, printHelp, printVersion} from "./TerminalIO";
import {checkForUpdates} from "./GHUpdateChecker";
import {open} from 'fs/promises';
const cfgProvider = new ConfigProvider();
const codeTesterInterface = new CodeTesterInterface(cfgProvider);
async function checkCode(): Promise<void> {
Progress.startSpinner();
let folderName = await cfgProvider.getSource();
Progress.updateSpinnerMessage(`Zipping ${folderName}...`);
const archive = archiver('zip');
const fw = fs.createWriteStream('/tmp.zip');
archive.directory(folderName, false);
archive.pipe(fw);
await archive.finalize()
await fw.close();
const fr = fs.createReadStream("/tmp.zip");
Progress.persistMessage(`${chalk.green(">")} Zipped ${folderName}`);
Progress.updateSpinnerMessage("Uploading & testing code...");
await cfgProvider.getCategoryId();
let result: CheckResults;
try {
result = await codeTesterInterface.checkCode(fr);
} catch (e) {
Progress.stopSpinner();
console.log(chalk.red.bold(e));
process.exitCode = 1;
return;
}
Progress.persistMessage(`${chalk.green(">")} Uploaded & tested code`);
Progress.stopSpinner();
console.log(chalk.cyan.bold("\n TEST RESULTS\n"));
let idx = 1;
for(let file in result) {
console.log(chalk.yellow.bold(`${file}`));
let successfulTests = 0;
for(let test of result[file]) {
if(test.result === "SUCCESSFUL") {
successfulTests++;
}
}
if(cfgProvider.getCheckList()) {
for(let test of result[file]) {
const prefix = cfgProvider.getInteractiveResults() ? ` (${idx++})` : "";
if(test.result === "SUCCESSFUL") {
console.log(` ${chalk.green("✓")} ` + test.check + prefix);
} else {
console.log(` ${chalk.red("✕")} ` + test.check + prefix);
}
}
}
if(successfulTests == result[file].length) {
console.log(chalk.bold.green(` All ${successfulTests} tests successful.`));
} else {
let failedTests = result[file].length - successfulTests
console.log(chalk.bold.green(` ${successfulTests} tests successful`) + chalk.bold(", ") + chalk.bold.red(`${failedTests} tests failed.`));
}
}
console.log("\nImprove the code tester by writing more tests :)");
console.log("https://codetester.ialistannen.de/#/submit-check\n");
if(cfgProvider.getInteractiveResults()) {
let shouldRun = true;
while(shouldRun) {
let id = -1;
console.log("Enter a check number to see the check details, type 'r' to rerun the checks or type 'q' to quit.");
while(id < 0 || isNaN(id)) {
const tmp = await asyncPrompt("> ");
if(tmp.toLowerCase() === "q") {
console.log("");
return;
} else if (tmp.toLowerCase() === "r") {
console.log("");
await checkCode();
return;
} else {
id = parseInt(tmp);
}
}
let fileId = 0;
while(id > result[Object.keys(result)[fileId]].length) {
id -= result[Object.keys(result)[fileId]].length;
fileId++;
if(fileId >= Object.keys(result).length) {
break;
}
}
if(fileId >= Object.keys(result).length) {
console.log(chalk.red("Invalid check id."));
continue;
}
for(let line of result[Object.keys(result)[fileId]][id-1].output) {
switch(line.type) {
case "PARAMETER":
console.log(chalk.gray.italic(`$$ ${line.content}`));
break;
case "INPUT":
console.log(chalk.gray("> ") + chalk.greenBright(line.content.replace(/\s/g, "␣")));
break;
case "OUTPUT":
console.log(chalk.green(` ${line.content}`));
break;
case "OTHER":
console.log(chalk.blueBright(line.content));
break;
case "ERROR":
console.log(chalk.red(` ${line.content}`));
break;
default:
console.log(chalk.cyanBright(line.content + " [[" + line.type + "]]"));
break;
}
}
}
}
}
async function listCategories(): Promise<void> {
Progress.updateSpinnerMessage("Querying categories");
let categories = await codeTesterInterface.getCategories();
Progress.persistMessage(chalk.green("> ") + "Queried categories");
Progress.stopSpinner();
console.log(chalk.cyan.bold("Categories\n"));
for(let category of categories) {
console.log(`(${category.id}) ${category.name}`);
}
}
async function main(): Promise<void> {
try {
let fd = await open(".codetester", "r");
await cfgProvider.parseFile(fd);
} catch {}
const command = await cfgProvider.parseCommandLine(process.argv);
if(command === Command.VERSION) {
printVersion();
return;
}
if(command === Command.HELP) {
printHelp(process.argv0);
return;
}
console.log(chalk.cyan("SimpleCodeTester-CLI\n"));
console.log(chalk.cyan("SimpleCodeTester by ") + chalk.yellow.bold("@I-Al-Istannen"));
console.log(chalk.cyan("CLI by ") + chalk.yellow.bold("@c0derMo"));
console.log("See cli arguments by using " + chalk.italic("--help\n"));
if(cfgProvider.getUpdateCheck()) {
let update = await checkForUpdates();
if(update) console.log(update);
}
let username = await cfgProvider.getUsername();
Progress.updateSpinnerMessage(`Logging in as ${chalk.yellow(username)}...`);
Progress.startSpinner();
try {
await codeTesterInterface.fetchRefreshToken();
await codeTesterInterface.fetchAccessToken();
} catch (e) {
Progress.stopSpinner();
console.log(chalk.red.bold(e));
process.exitCode = 1;
return;
}
Progress.persistMessage(`${chalk.green(">")} Logged in as ${chalk.yellow(username)}`);
switch (cfgProvider.getCommand()) {
case Command.INTERACTIVE:
Progress.stopSpinner();
switch((await asyncPrompt("Do you want to (r)un checks, or (l)ist categories? ")).toLowerCase()) {
case "r":
Progress.persistMessage(" 'Run checks' selected.");
Progress.startSpinner();
await checkCode();
break;
case "l":
Progress.persistMessage(" 'List categories' selected.");
Progress.startSpinner();
await listCategories();
break;
default:
console.log("Invalid input. Exiting.");
break;
}
break;
case Command.CHECK:
await checkCode();
break;
case Command.LISTCHECKS:
await listCategories();
break;
}
}
void main();