This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-tstyche.js
More file actions
executable file
·157 lines (117 loc) · 4.46 KB
/
Copy pathcreate-tstyche.js
File metadata and controls
executable file
·157 lines (117 loc) · 4.46 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
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
import { execSync } from "node:child_process";
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import readline from "node:readline";
import { styleText } from "node:util";
/**
* @param {string} question
* @returns {Promise<boolean>}
*/
async function promptBoolean(question) {
const questionText = `${styleText("green", "?")} ${question} ${styleText("gray", "[y/N] · ")}`;
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
return new Promise((resolve) => {
rl.question(questionText, (answer) => {
rl.close();
// Moves the cursor one line up and erases that line.
process.stdout.write("\u001B[1A\u001B[0K");
process.stdout.write(questionText);
if (["y", "yes"].includes(answer.trim().toLowerCase())) {
process.stdout.write("yes\n");
resolve(true);
} else {
process.stdout.write("no\n");
resolve(false);
}
});
});
}
function resolvePackageManager() {
const userAgent = process.env["npm_config_user_agent"];
if (userAgent?.includes("yarn")) {
return "yarn";
}
if (userAgent?.includes("pnpm")) {
return "pnpm";
}
if (userAgent?.includes("npm")) {
return "npm";
}
}
async function generate() {
const currentDirectory = path.resolve(".");
const packageConfigFile = path.resolve("package.json");
const packageManager = resolvePackageManager();
if (!existsSync(packageConfigFile)) {
console.error(
`${styleText("red", "Error:")} Cannot not find 'package.json' in ${styleText("grey", currentDirectory)}`,
);
if (packageManager != null) {
const initCommand = `${packageManager} init`;
console.error(`To create one, run ${styleText("blue", initCommand)}.`);
}
console.error("");
return;
}
if (packageManager != null) {
const tstychePackage = `tstyche@${process.argv.includes("--next") ? "next" : "latest"}`;
const addCommand = `${packageManager} add -D`;
execSync(`${addCommand} ${tstychePackage}`, { stdio: "ignore" });
// TODO Report errors, perhaps?
console.info(`${styleText("green", "+")} The ${styleText("grey", tstychePackage)} package was installed.`);
} else {
console.error(`${styleText("red", "× fail")} Failed to install the 'tstyche' package.`);
console.error("");
console.error(`${styleText("red", "Error:")} Unknown package manager. Try installing manually.`);
}
console.error("");
const configFile = path.resolve("tstyche.config.json");
const configFileText = `// For documentation, see: https://tstyche.org/reference/config-file
{
"$schema": "https://tstyche.org/schemas/config.json",
"testFileMatch": ["**/*.tst.*"]
}
`;
if (existsSync(configFile)) {
console.info(
`${styleText("yellow", "- skip")} Config file already exists in ${styleText("grey", currentDirectory)}.`,
);
} else {
await fs.writeFile(configFile, configFileText);
console.info(`${styleText("green", "+")} Config file was written to ${styleText("grey", configFile)}.`);
}
console.info("");
const shouldAddExamples = await promptBoolean("Add example test files?");
console.info("");
if (!shouldAddExamples) {
return;
}
const sourceDirectory = new URL("examples/", import.meta.url);
const examplesDirectory = path.resolve("tstyche-examples");
await fs.cp(sourceDirectory, examplesDirectory, { recursive: true });
console.info(
`${styleText("green", "+")} Example test files were written to ${styleText("grey", examplesDirectory)}.`,
);
console.info("");
if (packageManager != null) {
const execCommand = `${packageManager === "npm" ? "npx" : packageManager}`;
console.info(`${styleText("blue", "i")} Try out the following commands:`);
console.info("");
console.info(` ${styleText("blue", `${execCommand} tstyche`)}`);
console.info(" Run all tests.");
console.info("");
console.info(` ${styleText("blue", `${execCommand} tstyche examples/overload`)}`);
console.info(" Only run the matching test file.");
console.info("");
console.info(` ${styleText("blue", `${execCommand} tstyche --target '5.4 || 5.6.2 || >=5.8'`)}`);
console.info(" Test against specific versions of TypeScript.");
console.info("");
console.info(` ${styleText("blue", `${execCommand} tstyche --watch`)}`);
console.info(" Run all tests in watch mode.");
console.info("");
}
}
await generate();