()
| 438 | } |
| 439 | |
| 440 | async function init() { |
| 441 | const argTargetDir = argv._[0] |
| 442 | ? formatTargetDir(String(argv._[0])) |
| 443 | : undefined |
| 444 | const argTemplate = argv.template |
| 445 | const argOverwrite = argv.overwrite |
| 446 | const argImmediate = argv.immediate |
| 447 | const argInteractive = argv.interactive |
| 448 | const argEslint = argv.eslint |
| 449 | |
| 450 | const help = argv.help |
| 451 | if (help) { |
| 452 | console.log(helpMessage) |
| 453 | return |
| 454 | } |
| 455 | |
| 456 | const interactive = argInteractive ?? process.stdin.isTTY |
| 457 | |
| 458 | // Detect AI agent environment for better agent experience (AX) |
| 459 | const { isAgent } = await determineAgent() |
| 460 | if (isAgent && interactive) { |
| 461 | console.log( |
| 462 | '\nTo create in one go, run: create-vite <DIRECTORY> --no-interactive --template <TEMPLATE>\n', |
| 463 | ) |
| 464 | } |
| 465 | |
| 466 | const pkgInfo = pkgFromUserAgent(process.env.npm_config_user_agent) |
| 467 | const cancel = () => prompts.cancel('Operation cancelled') |
| 468 | |
| 469 | // 1. Get project name and target dir |
| 470 | let targetDir = argTargetDir |
| 471 | if (!targetDir) { |
| 472 | if (interactive) { |
| 473 | const projectName = await prompts.text({ |
| 474 | message: 'Project name:', |
| 475 | defaultValue: defaultTargetDir, |
| 476 | placeholder: defaultTargetDir, |
| 477 | validate: (value) => { |
| 478 | return !value || formatTargetDir(value).length > 0 |
| 479 | ? undefined |
| 480 | : 'Invalid project name' |
| 481 | }, |
| 482 | }) |
| 483 | if (prompts.isCancel(projectName)) return cancel() |
| 484 | targetDir = formatTargetDir(projectName) |
| 485 | } else { |
| 486 | targetDir = defaultTargetDir |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | // 2. Handle directory if exist and not empty |
| 491 | if (fs.existsSync(targetDir) && !isEmpty(targetDir)) { |
| 492 | let overwrite: 'yes' | 'no' | 'ignore' | undefined = argOverwrite |
| 493 | ? 'yes' |
| 494 | : undefined |
| 495 | if (!overwrite) { |
| 496 | if (interactive) { |
| 497 | const res = await prompts.select({ |
no test coverage detected