-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathinit.ts
175 lines (160 loc) · 4.57 KB
/
init.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
import { downloadTemplate, startShell } from 'giget'
import type { DownloadTemplateResult } from 'giget'
import { relative, resolve } from 'pathe'
import { consola } from 'consola'
import { installDependencies } from 'nypm'
import type { PackageManagerName } from 'nypm'
import { defineCommand } from 'citty'
import { sharedArgs } from './_shared'
const DEFAULT_REGISTRY
= 'https://raw.githubusercontent.com/nuxt/starter/templates/templates'
const DEFAULT_TEMPLATE_NAME = 'v3'
export default defineCommand({
meta: {
name: 'init',
description: 'Initialize a fresh project',
},
args: {
...sharedArgs,
dir: {
type: 'positional',
description: 'Project directory',
default: '',
},
template: {
type: 'string',
alias: 't',
description: 'Template name',
},
force: {
type: 'boolean',
alias: 'f',
description: 'Override existing directory',
},
offline: {
type: 'boolean',
description: 'Force offline mode',
},
preferOffline: {
type: 'boolean',
description: 'Prefer offline mode',
},
install: {
type: 'boolean',
default: true,
description: 'Skip installing dependencies',
},
gitInit: {
type: 'boolean',
description: 'Initialize git repository',
},
shell: {
type: 'boolean',
description: 'Start shell after installation in project directory',
},
packageManager: {
type: 'string',
description: 'Package manager choice (npm, pnpm, yarn, bun)',
},
},
async run(ctx) {
const cwd = resolve(ctx.args.cwd || '.')
// Get template name
const templateName = ctx.args.template || DEFAULT_TEMPLATE_NAME
if (typeof templateName !== 'string') {
consola.error('Please specify a template!')
process.exit(1)
}
// Download template
let template: DownloadTemplateResult
try {
template = await downloadTemplate(templateName, {
dir: ctx.args.dir,
cwd,
force: Boolean(ctx.args.force),
offline: Boolean(ctx.args.offline),
preferOffline: Boolean(ctx.args.preferOffline),
registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY,
})
}
catch (err) {
if (process.env.DEBUG) {
throw err
}
consola.error((err as Error).toString())
process.exit(1)
}
// Resolve package manager
const packageManagerOptions: PackageManagerName[] = [
'npm',
'pnpm',
'yarn',
'bun',
]
const packageManagerArg = ctx.args.packageManager as PackageManagerName
const selectedPackageManager = packageManagerOptions.includes(
packageManagerArg,
)
? packageManagerArg
: await consola.prompt('Which package manager would you like to use?', {
type: 'select',
options: packageManagerOptions,
})
// Install project dependencies
// or skip installation based on the '--no-install' flag
if (ctx.args.install === false) {
consola.info('Skipping install dependencies step.')
}
else {
consola.start('Installing dependencies...')
try {
await installDependencies({
cwd: template.dir,
packageManager: {
name: selectedPackageManager,
command: selectedPackageManager,
},
})
}
catch (err) {
if (process.env.DEBUG) {
throw err
}
consola.error((err as Error).toString())
process.exit(1)
}
consola.success('Installation completed.')
}
if (ctx.args.gitInit === undefined) {
ctx.args.gitInit = await consola.prompt('Initialize git repository?', {
type: 'confirm',
})
}
if (ctx.args.gitInit) {
consola.info('Initializing git repository...\n')
const { execa } = await import('execa')
await execa('git', ['init', template.dir], {
stdio: 'inherit',
}).catch((err) => {
consola.warn(`Failed to initialize git repository: ${err}`)
})
}
// Display next steps
consola.log(
`\n✨ Nuxt project has been created with the \`${template.name}\` template. Next steps:`,
)
const relativeTemplateDir = relative(process.cwd(), template.dir) || '.'
const nextSteps = [
!ctx.args.shell
&& relativeTemplateDir.length > 1
&& `\`cd ${relativeTemplateDir}\``,
`Start development server with \`${selectedPackageManager} run dev\``,
].filter(Boolean)
for (const step of nextSteps) {
consola.log(` › ${step}`)
}
if (ctx.args.shell) {
startShell(template.dir)
}
},
})