Skip to content

Commit d07d602

Browse files
author
Ryan Clements
authored
Merge pull request #47 from tristonarmstrong/package-manager-selection
Package manager selection
2 parents 9be3426 + b9881bd commit d07d602

3 files changed

Lines changed: 22 additions & 35660 deletions

File tree

src/commands/init.command.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Command } from 'commander';
22
import fs from 'fs-extra';
33
import path from 'path';
4-
import { PackageManagerName, detectPackageManager } from 'nypm';
4+
import { PackageManager, PackageManagerName, installDependencies } from 'nypm';
55
import { fileURLToPath } from 'url';
66
import { execa } from 'execa';
77
import Enquirer from 'enquirer';
@@ -26,6 +26,7 @@ export default new Command('init')
2626
const workspacePath = path.join(process.cwd(), workspaceName);
2727

2828
const dbUrl = await promptForDbUrl();
29+
2930
await copyTemplate({
3031
template: { name: templateName, path: templatePath },
3132
workspace: { name: workspaceName, path: workspacePath },
@@ -155,27 +156,29 @@ const renameGitignoreFiles = async (workspacePath: string) => {
155156
);
156157
};
157158

158-
// Needed until https://github.com/unjs/nypm/issues/115 is resolved
159-
const pmToInstallCommandMap: Record<PackageManagerName, [string, string[]]> = {
160-
npm: ['npm', ['ci']],
161-
yarn: ['yarn', ['install', '--frozen-lockfile']],
162-
bun: ['bun', ['install', '--frozen-lockfile']],
163-
pnpm: ['pnpm', ['install', '--frozen-lockfile']],
159+
type PMTypePromptResult = Pick<PackageManager, 'name'>;
160+
const promptForPackageManagerSelect = async (): Promise<PMTypePromptResult> => {
161+
const temp = await Enquirer.prompt<PMTypePromptResult>({
162+
type: 'select',
163+
name: 'name' satisfies keyof PMTypePromptResult,
164+
message: "Select the package manager you'd like to use:",
165+
choices: ['npm', 'bun', 'pnpm', 'yarn'] satisfies PackageManagerName[],
166+
// This is to workaround a bug
167+
// https://github.com/enquirer/enquirer/issues/121
168+
result(choice) {
169+
return (this as any).map(choice)[choice];
170+
},
171+
});
172+
return temp;
164173
};
165174

166175
const installDependenciesWithMessage = async (workspacePath: string) => {
176+
const pm = await promptForPackageManagerSelect();
167177
const spinner = ora('Installing dependencies').start();
168-
const pm = await detectPackageManager(workspacePath);
169-
if (!pm) {
170-
throw new Error(
171-
'No package manager found in the workspace. Unable to install dependencies.',
172-
);
173-
}
174-
const [command, args] = pmToInstallCommandMap[pm.name];
175-
await execa(command, args, {
178+
await installDependencies({
179+
packageManager: pm.name,
176180
cwd: workspacePath,
177-
/* Ignore output */
178-
stdio: 'pipe',
181+
silent: true,
179182
});
180183
spinner.succeed('Installed dependencies');
181184
};

0 commit comments

Comments
 (0)