-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathcreate-project.ts
239 lines (214 loc) · 6.1 KB
/
create-project.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
import os from "os"
import path from "path"
import { initOptionsSchema } from "@/src/commands/init"
import { fetchRegistry } from "@/src/registry/api"
import { getPackageManager } from "@/src/utils/get-package-manager"
import { handleError } from "@/src/utils/handle-error"
import { highlighter } from "@/src/utils/highlighter"
import { logger } from "@/src/utils/logger"
import { spinner } from "@/src/utils/spinner"
import { execa } from "execa"
import fs from "fs-extra"
import prompts from "prompts"
import { z } from "zod"
const MONOREPO_TEMPLATE_URL =
"https://codeload.github.com/shadcn-ui/ui/tar.gz/main"
export async function createProject(
options: Pick<
z.infer<typeof initOptionsSchema>,
"cwd" | "force" | "srcDir" | "components"
>
) {
options = {
srcDir: false,
...options,
}
let projectType: "next" | "monorepo" = "next"
let projectName: string = "my-app"
let nextVersion = "15.1.0"
const isRemoteComponent =
options.components?.length === 1 &&
!!options.components[0].match(/\/chat\/b\//)
if (options.components && isRemoteComponent) {
try {
const [result] = await fetchRegistry(options.components)
const { meta } = z
.object({
meta: z.object({
nextVersion: z.string(),
}),
})
.parse(result)
nextVersion = meta.nextVersion
} catch (error) {
logger.break()
handleError(error)
}
}
if (!options.force) {
const { type, name } = await prompts([
{
type: "select",
name: "type",
message: `The path ${highlighter.info(
options.cwd
)} does not contain a package.json file.\n Would you like to start a new project?`,
choices: [
{ title: "Next.js", value: "next" },
{ title: "Next.js (Monorepo)", value: "monorepo" },
],
initial: 0,
},
{
type: "text",
name: "name",
message: "What is your project named?",
initial: projectName,
format: (value: string) => value.trim(),
validate: (value: string) =>
value.length > 128
? `Name should be less than 128 characters.`
: true,
},
])
projectType = type
projectName = name
}
const packageManager = await getPackageManager(options.cwd, {
withFallback: true,
})
const projectPath = `${options.cwd}/${projectName}`
// Check if path is writable.
try {
await fs.access(options.cwd, fs.constants.W_OK)
} catch (error) {
logger.break()
logger.error(`The path ${highlighter.info(options.cwd)} is not writable.`)
logger.error(
`It is likely you do not have write permissions for this folder or the path ${highlighter.info(
options.cwd
)} does not exist.`
)
logger.break()
process.exit(1)
}
if (fs.existsSync(path.resolve(options.cwd, projectName, "package.json"))) {
logger.break()
logger.error(
`A project with the name ${highlighter.info(projectName)} already exists.`
)
logger.error(`Please choose a different name and try again.`)
logger.break()
process.exit(1)
}
if (projectType === "next") {
await createNextProject(projectPath, {
version: nextVersion,
cwd: options.cwd,
packageManager,
srcDir: !!options.srcDir,
})
}
if (projectType === "monorepo") {
await createMonorepoProject(projectPath, {
packageManager,
})
}
return {
projectPath,
projectName,
projectType,
}
}
async function createNextProject(
projectPath: string,
options: {
version: string
cwd: string
packageManager: string
srcDir: boolean
}
) {
const createSpinner = spinner(
`Creating a new Next.js project. This may take a few minutes.`
).start()
// Note: pnpm fails here. Fallback to npx with --use-PACKAGE-MANAGER.
const args = [
"--tailwind",
"--eslint",
"--typescript",
"--app",
options.srcDir ? "--src-dir" : "--no-src-dir",
"--no-import-alias",
`--use-${options.packageManager}`,
]
if (options.version.startsWith("15")) {
args.push("--turbopack")
}
try {
await execa(
"npx",
[`create-next-app@${options.version}`, projectPath, "--silent", ...args],
{
cwd: options.cwd,
}
)
} catch (error) {
logger.break()
logger.error(
`Something went wrong creating a new Next.js project. Please try again.`
)
process.exit(1)
}
createSpinner?.succeed("Creating a new Next.js project.")
}
async function createMonorepoProject(
projectPath: string,
options: {
packageManager: string
}
) {
const createSpinner = spinner(
`Creating a new Next.js monorepo. This may take a few minutes.`
).start()
try {
// Get the template.
const templatePath = path.join(os.tmpdir(), `shadcn-template-${Date.now()}`)
await fs.ensureDir(templatePath)
const response = await fetch(MONOREPO_TEMPLATE_URL)
if (!response.ok) {
throw new Error(`Failed to download template: ${response.statusText}`)
}
// Write the tar file
const tarPath = path.resolve(templatePath, "template.tar.gz")
await fs.writeFile(tarPath, Buffer.from(await response.arrayBuffer()))
await execa("tar", [
"-xzf",
tarPath,
"-C",
templatePath,
"--strip-components=2",
"ui-main/templates/monorepo-next",
])
const extractedPath = path.resolve(templatePath, "monorepo-next")
await fs.move(extractedPath, projectPath)
await fs.remove(templatePath)
// Run install.
await execa(options.packageManager, ["install"], {
cwd: projectPath,
})
// Try git init.
const cwd = process.cwd()
await execa("git", ["--version"], { cwd: projectPath })
await execa("git", ["init"], { cwd: projectPath })
await execa("git", ["add", "-A"], { cwd: projectPath })
await execa("git", ["commit", "-m", "Initial commit"], {
cwd: projectPath,
})
await execa("cd", [cwd])
createSpinner?.succeed("Creating a new Next.js monorepo.")
} catch (error) {
createSpinner?.fail("Something went wrong creating a new Next.js monorepo.")
handleError(error)
}
}