-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
137 lines (120 loc) · 3.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
#!/usr/bin/env node
import fs from "node:fs";
import CLI from "./cli";
import download from "./helpers/download";
import installPackages from "./helpers/installPackages";
import logger from "./helpers/logger";
import vscodeExtensionSuggestion from "./helpers/vscodeExtensionSuggestion";
import chalk from "chalk";
const main = async () => {
const cli = new CLI();
await cli.initialize();
const input = await cli.collect();
const isProjectWithSubdirectory = cli.projectsWithSubfolders.reduce(
(prev, curr) => {
return prev || cli.args.template.includes(curr);
},
false,
);
await download(input);
if (input.install) {
if (!isProjectWithSubdirectory) {
await installPackages(input.pkgMgr, `${input.path}/${input.name}`);
} else {
if (cli.args.template.includes("fullstack-simple-chat")) {
await installPackages(
input.pkgMgr,
`${input.path}/${input.name}/client`,
);
await installPackages(
input.pkgMgr,
`${input.path}/${input.name}/server`,
);
}
if (cli.args.template.includes("optimize/starter")) {
await installPackages(input.pkgMgr, `${input.path}/${input.name}`);
}
if (
cli.args.template.includes("product-search-with-typesense") ||
cli.args.template.includes("rest-nextjs-express")
) {
await installPackages(
input.pkgMgr,
`${input.path}/${input.name}/frontend`,
);
await installPackages(
input.pkgMgr,
`${input.path}/${input.name}/backend`,
);
}
}
}
if (input.databaseUrl) {
fs.writeFileSync(
`${input.path}/${input.name}/.env`,
`DATABASE_URL="${input.databaseUrl}"\n`,
{ flag: "a" },
);
if (
input.databaseUrl.startsWith("prisma://") ||
input.databaseUrl.startsWith("prisma+postgres://")
) {
const queryParams = input.databaseUrl.split("?")[1];
const urlParams = new URLSearchParams(queryParams);
const apiKey = urlParams.get("api_key");
fs.writeFileSync(
`${input.path}/${input.name}/.env`,
`PULSE_API_KEY="${apiKey}"\n`,
{ flag: "a" },
);
}
}
if (input.vscode) {
await vscodeExtensionSuggestion(input);
}
cli.addInstruction(
"Navigate into the project directory:",
chalk.hex("#4C51BF")(`cd ${input.path}/${input.name}`),
);
if (!input.install) {
cli.addInstruction(
"Install dependencies:",
chalk.hex("#4C51BF")(`npm install`),
);
}
cli.addInstruction(
"Create and execute initial migration based on `schema.prisma`:",
chalk.hex("#4C51BF")(`npx prisma migrate dev`),
);
logger.success(`
${chalk.bold(`The project is good to go! Next steps:`)}
${chalk.bold(`1. Navigate into ${input.path}/${input.name} to begin.`)}
${chalk.bold(
"2. Refer to the project README for detailed instructions on running the project:",
)}
${
" " +
chalk.bold.underline.blue(
`https://github.com/prisma/prisma-examples/tree/latest/${input.template}`,
)
}
`);
logger.success(
`If you have any feedback about this specific template, we want to hear it!\nSubmit any feedback here: ${chalk.gray.underline(
"https://pris.ly/prisma-examples-feedback",
)} `,
);
};
main().catch((e) => {
if (e instanceof Error) {
logger.error(e.message);
} else {
logger.error(
"Something strange happened... If the problem persists, please create a GitHub issue with the error below 👇🏻",
);
}
if (!process.env.VITEST) {
process.exit(1);
}
});
export default main;