MCPcopy
hub / github.com/vitejs/vite / init

Function init

packages/create-vite/src/index.ts:440–727  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

438}
439
440async 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({

Callers 2

index.tsFile · 0.70
worker.jsFile · 0.50

Calls 15

formatTargetDirFunction · 0.85
pkgFromUserAgentFunction · 0.85
isEmptyFunction · 0.85
isValidPackageNameFunction · 0.85
toValidPackageNameFunction · 0.85
getFullCustomCommandFunction · 0.85
getLabelFunction · 0.85
writeFunction · 0.85
setupReactCompilerFunction · 0.85
setupEslintFunction · 0.85
getInstallCommandFunction · 0.85
getRunCommandFunction · 0.85

Tested by

no test coverage detected