-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
122 lines (103 loc) · 3.33 KB
/
cli.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
// Copyright 2020-present numb86. All rights reserved. MIT license.
import { argumentsParse } from "./src/deps.ts";
import { convertTestCode } from "./src/convert_test_code.ts";
const TARGET_EXTENSIONS = ["js", "mjs", "ts", "jsx", "tsx"];
const currentAbsolutePath = Deno.cwd();
const parsedArgs = argumentsParse(Deno.args);
const firstArg: string | undefined = (parsedArgs._[0] as string | undefined);
const flags = Object.entries(parsedArgs).filter((entry) => {
if (entry[0] === "_") return false;
return entry[1];
}).map((entry) => `--${entry[0]}`);
const adjustedFirstArg = firstArg && firstArg.endsWith("/")
? firstArg.slice(0, firstArg.length - 1)
: firstArg;
function isTestFileName(arg: string): boolean {
return TARGET_EXTENSIONS.some((extension) =>
arg.endsWith(`.test.${extension}`) || arg.endsWith(`_test.${extension}`)
);
}
async function getTestFileNames(path: string) {
if (isTestFileName(path)) {
return [path];
}
const files: string[] = [];
for await (const dirEntry of Deno.readDir(path)) {
if (dirEntry.isFile && isTestFileName(dirEntry.name)) {
files.push(`${path}/${dirEntry.name}`);
}
if (dirEntry.isDirectory) {
const children = await getTestFileNames(`${path}/${dirEntry.name}`);
files.push(...children);
}
}
return files;
}
function getAbsolutePath(path: string): string {
if (path.startsWith("/")) {
return path;
}
if (path.startsWith("./")) {
return `${currentAbsolutePath}${path.slice(1)}`;
}
if (path.startsWith("../")) {
let pathArray = path.split("../");
let done = false;
let count = 0;
pathArray.forEach((elem) => {
if (elem !== "") {
done = true;
}
if (!done) {
count += 1;
}
});
const currentPathArray = currentAbsolutePath.split("/");
for (let i = count; i > 0; i -= 1) {
pathArray.shift();
currentPathArray.pop();
}
return `${currentPathArray.join("/")}/${pathArray.join("../")}`;
}
return `${currentAbsolutePath}/${path}`;
}
const targetFilePaths = adjustedFirstArg === undefined
? await getTestFileNames(getAbsolutePath(Deno.cwd()))
: await getTestFileNames(getAbsolutePath(adjustedFirstArg));
const tempDirName = await Deno.makeTempDir();
const writeFilePromises = targetFilePaths.map(async (filePath) => {
if (filePath.endsWith(".ts") || filePath.endsWith(".tsx")) {
const testProcess = Deno.run({
cmd: ["deno", "run", filePath],
});
const typeCheck = await testProcess.status();
if (!typeCheck.success) {
Deno.exit(typeCheck.code);
}
}
const fileText = await Deno.readTextFile(filePath);
const jsSourceCode = await Deno.transpileOnly(
{ [filePath]: fileText },
{ target: "es2020" },
);
const convertedSourceCode = convertTestCode(
jsSourceCode[filePath].source,
filePath,
);
const tempFileName = `${tempDirName}/${filePath.replaceAll("/", "-")}`;
return Deno.writeTextFile(tempFileName, convertedSourceCode);
});
await Promise.all(writeFilePromises);
const testProcessCmd = ["deno", "test", "--no-check"];
flags.forEach((flag) => {
testProcessCmd.push(flag);
});
testProcessCmd.push(tempDirName);
const testProcess = Deno.run({
cmd: testProcessCmd,
});
await testProcess.status();
const tempDirRemoveProcess = Deno.run({
cmd: ["rm", "-rf", tempDirName],
});
await tempDirRemoveProcess.status();