Skip to content

Commit 34933eb

Browse files
committed
fix(cli): support create vite with custom command
1 parent 697d1f0 commit 34933eb

3 files changed

Lines changed: 63 additions & 21 deletions

File tree

packages/global/snap-tests/new-monorepo/snap.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ solid-ts solid
2525
qwik-ts qwik
2626

2727
> vp new --template vanilla-ts --monorepo --pm pnpm --git false --overwrite hello-vite-plus # create monorepo
28+
┌ Vite+ The Unified Toolchain for the Web
2829
29-
◇ Scaffolding project in hello-vite-plus/apps/website...
30+
◇ Scaffolding project with vanilla-ts in hello-vite-plus/apps/website...
3031
3132
└ Done. Now run:
3233

@@ -49,8 +50,9 @@ tsconfig.json
4950
vite.config.ts
5051

5152
> cd hello-vite-plus && vp new --app --template vanilla-ts apps/my-app # add app
53+
┌ Vite+ The Unified Toolchain for the Web
5254
53-
◇ Scaffolding project in apps/my-app...
55+
◇ Scaffolding project with vanilla-ts in apps/my-app...
5456
5557
└ Done. Now run:
5658

@@ -64,8 +66,9 @@ my-app
6466
website
6567

6668
> cd hello-vite-plus && vp new --lib packages/my-lib # add lib
69+
┌ Vite+ The Unified Toolchain for the Web
6770
68-
◇ Scaffolding project in packages/my-lib...
71+
◇ Scaffolding library in packages/my-lib...
6972
7073
└ Done. Now run:
7174

@@ -78,8 +81,9 @@ website
7881
my-lib
7982

8083
> cd hello-vite-plus && vp new --lib tools/my-tool # add lib new not exists tools directory
84+
┌ Vite+ The Unified Toolchain for the Web
8185
82-
◇ Scaffolding project in tools/my-tool...
86+
◇ Scaffolding library in tools/my-tool...
8387
8488
└ Done. Now run:
8589

packages/global/snap-tests/new-singlerepo/snap.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
> vp new --template vanilla-ts --monorepo false --pm pnpm --git false --overwrite hello-vite-plus # create singlerepo
2+
┌ Vite+ The Unified Toolchain for the Web
23
3-
◇ Scaffolding project in hello-vite-plus...
4+
◇ Scaffolding project with vanilla-ts in hello-vite-plus...
45
56
└ Done. Now run:
67

packages/global/src/new.ts

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ async function init() {
369369

370370
const cancel = () => prompts.cancel('Operation cancelled');
371371

372+
prompts.intro(`${blueBright('Vite+')} The Unified Toolchain for the Web`);
373+
372374
// --app and --lib should inside monorepo
373375
if (isCreateAppOrLib) {
374376
const monorepoRoot = findMonorepoRoot();
@@ -525,37 +527,63 @@ async function init() {
525527
const rawRoot = path.join(cwd, rawTargetDir);
526528
const cdProjectName = path.relative(cwd, rawRoot);
527529
const appOrLibName = path.relative(cwd, root);
528-
prompts.log.step(`Scaffolding project in ${appOrLibName}...`);
530+
const isMonorepo = argCreateMonorepo ?? isCreateAppOrLib;
529531

530532
// 5. Create project
531533
if (argCreateLib) {
534+
prompts.log.step(`Scaffolding library in ${appOrLibName}...`);
532535
// create a library project
533536
initMonorepoLib(root, packageName);
534537
} else {
538+
prompts.log.step(`Scaffolding project with ${template} in ${appOrLibName}...`);
535539
// use create-vite to create a app project
536540
const createViteBin = fileURLToPath(import.meta.resolve('create-vite/index.js'));
537-
const { status, stderr, stdout } = spawn.sync('node', [
538-
createViteBin,
539-
'--template',
540-
template!,
541-
targetDir,
542-
], {
543-
// stdio: 'inherit',
544-
stdio: 'pipe',
545-
});
546-
if (status && status > 0) {
547-
console.error(stderr.toString());
548-
console.error(stdout.toString());
549-
process.exit(status);
541+
const { customCommand } = FRAMEWORKS.flatMap((f) => f.variants).find((v) => v.name === template) ?? {};
542+
if (customCommand) {
543+
// print the custom command
544+
prompts.log.info(customCommand.replace('TARGET_DIR', appOrLibName) + ' ...');
545+
const cwd = path.dirname(root);
546+
// fs.mkdirSync(cwd, { recursive: true });
547+
const appName = isMonorepo ? path.basename(targetDir) : targetDir;
548+
const createViteArgs = [
549+
createViteBin,
550+
'--overwrite',
551+
'--template',
552+
template!,
553+
appName,
554+
];
555+
const { status } = spawn.sync('node', createViteArgs, {
556+
stdio: 'inherit',
557+
cwd,
558+
});
559+
if (status && status > 0) {
560+
process.exit(status);
561+
}
562+
} else {
563+
const createViteArgs = [
564+
createViteBin,
565+
'--overwrite',
566+
'--template',
567+
template!,
568+
targetDir,
569+
];
570+
const { status, stderr, stdout } = spawn.sync('node', createViteArgs, {
571+
stdio: 'pipe',
572+
});
573+
if (status && status > 0) {
574+
prompts.log.error(stderr.toString());
575+
prompts.log.error(stdout.toString());
576+
process.exit(status);
577+
}
550578
}
551579

552-
await fixPackageJsonForVitePlus(root, selectedPackageManager, argCreateMonorepo ?? isCreateAppOrLib);
580+
await fixPackageJsonForVitePlus(root, selectedPackageManager, isMonorepo);
553581
}
554582

555583
// first init, ask user to init git
556584
if (!isCreateAppOrLib) {
557585
const initGit = typeof argGit === 'boolean' ? argGit : await prompts.confirm({
558-
message: `Init git? (git init ${cdProjectName})`,
586+
message: `Initialize git repository? (git init ${cdProjectName})`,
559587
initialValue: true,
560588
});
561589
if (prompts.isCancel(initGit)) return cancel();
@@ -738,6 +766,10 @@ async function fixPackageJsonForVitePlus(projectDir: string, selectedPackageMana
738766
if (!pkg.scripts?.ready) {
739767
pkg.scripts.ready = 'vite lint --type-aware && vite run build && vite test --passWithNoTests';
740768
}
769+
// fix empty pkg.name
770+
if (!pkg.name) {
771+
pkg.name = path.basename(projectDir);
772+
}
741773
return JSON.stringify(pkg, null, 2) + '\n';
742774
});
743775

@@ -750,6 +782,11 @@ async function fixPackageJsonForVitePlus(projectDir: string, selectedPackageMana
750782
copy(path.join(pkgRoot, 'templates/config/.npmrc'), path.join(projectDir, '.npmrc'));
751783
}
752784
}
785+
786+
// remove package-lock.json when package manager is not npm
787+
if (selectedPackageManager !== 'npm') {
788+
fs.rmSync(path.join(projectDir, 'package-lock.json'), { force: true });
789+
}
753790
}
754791

755792
async function setPackageManager(projectDir: string, packageManager: string) {

0 commit comments

Comments
 (0)