Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { DownloadTemplateResult } from 'giget'
import type { PackageManagerName } from 'nypm'

import { existsSync } from 'node:fs'
import process from 'node:process'

import { defineCommand } from 'citty'
Expand Down Expand Up @@ -75,14 +76,50 @@ export default defineCommand({
process.exit(1)
}

let templateDownloadPath = resolve(cwd, ctx.args.dir)
let shouldForce = Boolean(ctx.args.force)

// Prompt the user if the template download directory already exists
// when no `--force` flag is provided
const shouldVerify = !shouldForce && existsSync(templateDownloadPath)
if (shouldVerify) {
const selectedAction = await logger.prompt(
`The directory \`${templateDownloadPath}\` already exists. What would you like to do?`,
{
type: 'select',
options: ['Override its contents', 'Select different directory', 'Abort'],
},
)

switch (selectedAction) {
case 'Override its contents':
shouldForce = true
break

case 'Select different directory': {
const dir = await logger.prompt('Please specify a different directory:', {
type: 'text',
})
if (dir && typeof dir === 'string') {
templateDownloadPath = resolve(cwd, dir)
}
break
}

// 'Abort' or Ctrl+C
default:
logger.info('Initialization aborted.')
process.exit(1)
}
}

// Download template
let template: DownloadTemplateResult

try {
template = await downloadTemplate(templateName, {
dir: ctx.args.dir,
cwd,
force: Boolean(ctx.args.force),
dir: templateDownloadPath,
force: shouldForce,
offline: Boolean(ctx.args.offline),
preferOffline: Boolean(ctx.args.preferOffline),
registry: process.env.NUXI_INIT_REGISTRY || DEFAULT_REGISTRY,
Expand Down
Loading