-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.ts
108 lines (90 loc) · 2.53 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
import * as prompts from "@clack/prompts";
import chalk from "chalk";
import { parseArgs } from "node:util";
import { fromZodError } from "zod-validation-error";
import { createRerunSuggestion } from "../create/createRerunSuggestion.js";
import { create } from "../create/index.js";
import { initialize } from "../initialize/index.js";
import { migrate } from "../migrate/index.js";
import { logLine } from "../shared/cli/lines.js";
import { StatusCodes } from "../shared/codes.js";
import { logHelpText } from "./help.js";
import { getVersionFromPackageJson } from "./packageJson.js";
import { promptForMode } from "./promptForMode.js";
const operationMessage = (verb: string) =>
`Operation ${verb}. Exiting - maybe another time? 👋`;
export async function bin(args: string[]) {
console.clear();
const version = await getVersionFromPackageJson();
const introPrompts = [
chalk.greenBright(`✨ Welcome to`),
chalk.bgGreenBright.black(`create-typescript-app`),
chalk.greenBright(`${version}! ✨`),
].join(" ");
const introWarnings = [
chalk.yellow(
"⚠️ This template is early stage, opinionated, and not endorsed by the TypeScript team. ⚠️",
),
chalk.yellow(
"⚠️ If any tooling it sets displeases you, you can always remove that portion manually. ⚠️",
),
];
const { values } = parseArgs({
args,
options: {
help: {
short: "h",
type: "boolean",
},
mode: { type: "string" },
version: {
short: "v",
type: "boolean",
},
},
strict: false,
});
if (values.help) {
logHelpText([introPrompts, ...introWarnings]);
return 0;
}
if (values.version) {
console.log(version);
return 0;
}
prompts.intro(introPrompts);
logLine();
logLine(introWarnings[0]);
logLine(introWarnings[1]);
const { mode, options: promptedOptions } = await promptForMode(
!!values.auto,
values.mode,
);
if (typeof mode !== "string") {
prompts.outro(chalk.red(mode?.message ?? operationMessage("cancelled")));
return 1;
}
const runners = { create, initialize, migrate };
const { code, error, options } = await runners[mode](args, promptedOptions);
prompts.log.info(
[
chalk.italic(`Tip: to run again with the same input values, use:`),
chalk.blue(createRerunSuggestion(options)),
].join(" "),
);
if (code) {
logLine();
if (error) {
logLine(
chalk.red(typeof error === "string" ? error : fromZodError(error)),
);
logLine();
}
prompts.cancel(
code === StatusCodes.Cancelled
? operationMessage("cancelled")
: operationMessage("failed"),
);
}
return code;
}