-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.ts
374 lines (348 loc) · 12.7 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import * as p from "@clack/prompts";
import chalk from "chalk";
import { Command } from "commander";
import { CREATE_T3_APP, DEFAULT_APP_NAME } from "~/consts.js";
import {
databaseProviders,
type AvailablePackages,
type DatabaseProvider,
} from "~/installers/index.js";
import { getVersion } from "~/utils/getT3Version.js";
import { getUserPkgManager } from "~/utils/getUserPkgManager.js";
import { IsTTYError } from "~/utils/isTTYError.js";
import { logger } from "~/utils/logger.js";
import { validateAppName } from "~/utils/validateAppName.js";
import { validateImportAlias } from "~/utils/validateImportAlias.js";
interface CliFlags {
noGit: boolean;
noInstall: boolean;
default: boolean;
importAlias: string;
/** @internal Used in CI. */
CI: boolean;
/** @internal Used in CI. */
tailwind: boolean;
/** @internal Used in CI. */
trpc: boolean;
/** @internal Used in CI. */
prisma: boolean;
/** @internal Used in CI. */
drizzle: boolean;
/** @internal Used in CI. */
nextAuth: boolean;
/** @internal Used in CI. */
appRouter: boolean;
}
interface CliResults {
appName: string;
packages: AvailablePackages[];
flags: CliFlags;
databaseProvider: DatabaseProvider;
}
const defaultOptions: CliResults = {
appName: DEFAULT_APP_NAME,
packages: ["nextAuth", "prisma", "tailwind", "trpc"],
flags: {
noGit: false,
noInstall: false,
default: false,
CI: false,
tailwind: false,
trpc: false,
prisma: false,
drizzle: false,
nextAuth: false,
importAlias: "~/",
appRouter: false,
},
databaseProvider: "sqlite",
};
export const runCli = async (): Promise<CliResults> => {
const cliResults = defaultOptions;
const program = new Command()
.name(CREATE_T3_APP)
.description("A CLI for creating web applications with the t3 stack")
.argument(
"[dir]",
"The name of the application, as well as the name of the directory to create"
)
.option(
"--noGit",
"Explicitly tell the CLI to not initialize a new git repo in the project",
false
)
.option(
"--noInstall",
"Explicitly tell the CLI to not run the package manager's install command",
false
)
.option(
"-y, --default",
"Bypass the CLI and use all default options to bootstrap a new t3-app",
false
)
/** START CI-FLAGS */
/**
* @experimental Used for CI E2E tests. If any of the following option-flags are provided, we
* skip prompting.
*/
.option("--CI", "Boolean value if we're running in CI", false)
/** @experimental - Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"--tailwind [boolean]",
"Experimental: Boolean value if we should install Tailwind CSS. Must be used in conjunction with `--CI`.",
(value) => !!value && value !== "false"
)
/** @experimental Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"--nextAuth [boolean]",
"Experimental: Boolean value if we should install NextAuth.js. Must be used in conjunction with `--CI`.",
(value) => !!value && value !== "false"
)
/** @experimental - Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"--prisma [boolean]",
"Experimental: Boolean value if we should install Prisma. Must be used in conjunction with `--CI`.",
(value) => !!value && value !== "false"
)
/** @experimental - Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"--drizzle [boolean]",
"Experimental: Boolean value if we should install Drizzle. Must be used in conjunction with `--CI`.",
(value) => !!value && value !== "false"
)
/** @experimental - Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"--trpc [boolean]",
"Experimental: Boolean value if we should install tRPC. Must be used in conjunction with `--CI`.",
(value) => !!value && value !== "false"
)
/** @experimental - Used for CI E2E tests. Used in conjunction with `--CI` to skip prompting. */
.option(
"-i, --import-alias",
"Explicitly tell the CLI to use a custom import alias",
defaultOptions.flags.importAlias
)
.option(
"--dbProvider [provider]",
`Choose a database provider to use. Possible values: ${databaseProviders.join(
", "
)}`,
defaultOptions.flags.importAlias
)
.option(
"--appRouter [boolean]",
"Explicitly tell the CLI to use the new Next.js app router",
(value) => !!value && value !== "false"
)
/** END CI-FLAGS */
.version(getVersion(), "-v, --version", "Display the version number")
.addHelpText(
"afterAll",
`\n The t3 stack was inspired by ${chalk
.hex("#E8DCFF")
.bold(
"@t3dotgg"
)} and has been used to build awesome fullstack applications like ${chalk
.hex("#E24A8D")
.underline("https://ping.gg")} \n`
)
.parse(process.argv);
// FIXME: TEMPORARY WARNING WHEN USING YARN 3. SEE ISSUE #57
if (process.env.npm_config_user_agent?.startsWith("yarn/3")) {
logger.warn(` WARNING: It looks like you are using Yarn 3. This is currently not supported,
and likely to result in a crash. Please run create-t3-app with another
package manager such as pnpm, npm, or Yarn Classic.
See: https://github.com/t3-oss/create-t3-app/issues/57`);
}
// Needs to be separated outside the if statement to correctly infer the type as string | undefined
const cliProvidedName = program.args[0];
if (cliProvidedName) {
cliResults.appName = cliProvidedName;
}
cliResults.flags = program.opts();
/** @internal Used for CI E2E tests. */
if (cliResults.flags.CI) {
cliResults.packages = [];
if (cliResults.flags.trpc) cliResults.packages.push("trpc");
if (cliResults.flags.tailwind) cliResults.packages.push("tailwind");
if (cliResults.flags.prisma) cliResults.packages.push("prisma");
if (cliResults.flags.drizzle) cliResults.packages.push("drizzle");
if (cliResults.flags.nextAuth) cliResults.packages.push("nextAuth");
if (cliResults.flags.prisma && cliResults.flags.drizzle) {
// We test a matrix of all possible combination of packages in CI. Checking for impossible
// combinations here and exiting gracefully is easier than changing the CI matrix to exclude
// invalid combinations. We are using an "OK" exit code so CI continues with the next combination.
logger.warn("Incompatible combination Prisma + Drizzle. Exiting.");
process.exit(0);
}
cliResults.databaseProvider = cliResults.packages.includes("drizzle")
? "planetscale"
: "sqlite";
return cliResults;
}
if (cliResults.flags.default) {
return cliResults;
}
// Explained below why this is in a try/catch block
try {
if (process.env.TERM_PROGRAM?.toLowerCase().includes("mintty")) {
logger.warn(` WARNING: It looks like you are using MinTTY, which is non-interactive. This is most likely because you are
using Git Bash. If that's that case, please use Git Bash from another terminal, such as Windows Terminal. Alternatively, you
can provide the arguments from the CLI directly: https://create.t3.gg/en/installation#experimental-usage to skip the prompts.`);
throw new IsTTYError("Non-interactive environment");
}
// if --CI flag is set, we are running in CI mode and should not prompt the user
const pkgManager = getUserPkgManager();
const project = await p.group(
{
...(!cliProvidedName && {
name: () =>
p.text({
message: "What will your project be called?",
defaultValue: cliProvidedName,
validate: validateAppName,
}),
}),
language: () => {
return p.select({
message: "Will you be using TypeScript or JavaScript?",
options: [
{ value: "typescript", label: "TypeScript" },
{ value: "javascript", label: "JavaScript" },
],
initialValue: "typescript",
});
},
_: ({ results }) =>
results.language === "javascript"
? p.note(chalk.redBright("Wrong answer, using TypeScript instead"))
: undefined,
styling: () => {
return p.confirm({
message: "Will you be using Tailwind CSS for styling?",
});
},
trpc: () => {
return p.confirm({
message: "Would you like to use tRPC?",
});
},
authentication: () => {
return p.select({
message: "What authentication provider would you like to use?",
options: [
{ value: "none", label: "None" },
{ value: "next-auth", label: "NextAuth.js" },
// Maybe later
// { value: "clerk", label: "Clerk" },
],
initialValue: "none",
});
},
database: () => {
return p.select({
message: "What database ORM would you like to use?",
options: [
{ value: "none", label: "None" },
{ value: "prisma", label: "Prisma" },
{ value: "drizzle", label: "Drizzle" },
],
initialValue: "none",
});
},
appRouter: () => {
return p.confirm({
message:
chalk.bgCyan(" EXPERIMENTAL ") +
" Would you like to use Next.js App Router?",
initialValue: false,
});
},
databaseProvider: ({ results }) => {
if (results.database === "none") return;
return p.select({
message: "What database provider would you like to use?",
options: [
{ value: "sqlite", label: "SQLite (LibSQL)" },
{ value: "mysql", label: "MySQL" },
{ value: "postgres", label: "PostgreSQL" },
{ value: "planetscale", label: "PlanetScale" },
],
initialValue: "sqlite",
});
},
...(!cliResults.flags.noGit && {
git: () => {
return p.confirm({
message:
"Should we initialize a Git repository and stage the changes?",
initialValue: !defaultOptions.flags.noGit,
});
},
}),
...(!cliResults.flags.noInstall && {
install: () => {
return p.confirm({
message:
`Should we run '${pkgManager}` +
(pkgManager === "yarn" ? `'?` : ` install' for you?`),
initialValue: !defaultOptions.flags.noInstall,
});
},
}),
importAlias: () => {
return p.text({
message: "What import alias would you like to use?",
defaultValue: defaultOptions.flags.importAlias,
placeholder: defaultOptions.flags.importAlias,
validate: validateImportAlias,
});
},
},
{
onCancel() {
process.exit(1);
},
}
);
const packages: AvailablePackages[] = [];
if (project.styling) packages.push("tailwind");
if (project.trpc) packages.push("trpc");
if (project.authentication === "next-auth") packages.push("nextAuth");
if (project.database === "prisma") packages.push("prisma");
if (project.database === "drizzle") packages.push("drizzle");
return {
appName: project.name ?? cliResults.appName,
packages,
databaseProvider:
(project.databaseProvider as DatabaseProvider) || "sqlite",
flags: {
...cliResults.flags,
appRouter: project.appRouter ?? cliResults.flags.appRouter,
noGit: !project.git || cliResults.flags.noGit,
noInstall: !project.install || cliResults.flags.noInstall,
importAlias: project.importAlias ?? cliResults.flags.importAlias,
},
};
} catch (err) {
// If the user is not calling create-t3-app from an interactive terminal, inquirer will throw an IsTTYError
// If this happens, we catch the error, tell the user what has happened, and then continue to run the program with a default t3 app
if (err instanceof IsTTYError) {
logger.warn(`
${CREATE_T3_APP} needs an interactive terminal to provide options`);
const shouldContinue = await p.confirm({
message: `Continue scaffolding a default T3 app?`,
initialValue: true,
});
if (!shouldContinue) {
logger.info("Exiting...");
process.exit(0);
}
logger.info(`Bootstrapping a default T3 app in ./${cliResults.appName}`);
} else {
throw err;
}
}
return cliResults;
};