Skip to content

Commit

Permalink
refactor(cli): more reliable package manager detection types (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewvolk authored Feb 15, 2024
1 parent afae7e3 commit 650256a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions packages/create-catalyst/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { writeEnv } from '../utils/write-env';
const exec = promisify(execCallback);

export const create = async (options: CreateCommandOptions) => {
const pm = options.packageManager;
const { packageManager } = options;
const bigCommerceApiUrl = `https://api.${options.bigcommerceHostname}`;
const bigCommerceAuthUrl = `https://login.${options.bigcommerceHostname}`;
const { projectName, projectDir } = await projectConfig({
Expand All @@ -33,9 +33,9 @@ export const create = async (options: CreateCommandOptions) => {

await cloneCatalyst({ projectDir, projectName, ghRef: options.ghRef });

console.log(`\nUsing ${chalk.bold(pm)}\n`);
console.log(`\nUsing ${chalk.bold(packageManager)}\n`);

await installDependencies(projectDir, pm);
await installDependencies(projectDir, packageManager);

console.log(
[
Expand Down Expand Up @@ -133,17 +133,17 @@ export const create = async (options: CreateCommandOptions) => {
customerImpersonationToken,
});

console.log(`\nUsing ${chalk.bold(pm)}\n`);
console.log(`\nUsing ${chalk.bold(packageManager)}\n`);

await installDependencies(projectDir, pm);
await installDependencies(projectDir, packageManager);

await spinner(exec(`${pm} run codegen`, { cwd: projectDir }), {
await spinner(exec(`${packageManager} run codegen`, { cwd: projectDir }), {
text: 'Generating GraphQL types...',
successText: 'GraphQL types generated successfully',
failText: (err) => chalk.red(`Failed to generate GraphQL types: ${err.message}`),
});

await spinner(exec(`${pm} run lint --fix`, { cwd: projectDir }), {
await spinner(exec(`${packageManager} run lint --fix`, { cwd: projectDir }), {
text: 'Linting to validate generated types...',
successText: 'GraphQL types validated successfully',
failText: (err) => chalk.red(`Failed to validate GraphQL types: ${err.message}`),
Expand All @@ -152,6 +152,6 @@ export const create = async (options: CreateCommandOptions) => {
console.log(
`\n${chalk.green('Success!')} Created '${projectName}' at '${projectDir}'\n`,
'\nNext steps:\n',
chalk.yellow(`\ncd ${projectName} && ${pm} run dev\n`),
chalk.yellow(`\ncd ${projectName} && ${packageManager} run dev\n`),
);
};
4 changes: 2 additions & 2 deletions packages/create-catalyst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import PACKAGE_INFO from '../package.json';

import { create } from './commands/create';
import { init } from './commands/init';
import { getPackageManager } from './utils/pm';
import { getPackageManager, packageManagerChoices } from './utils/pm';

if (!satisfies(process.version, PACKAGE_INFO.engines.node)) {
console.error(
Expand Down Expand Up @@ -55,7 +55,7 @@ const createCommand = program
)
.addOption(
new Option('--package-manager <pm>', 'Override detected package manager')
.choices(['npm', 'pnpm', 'yarn'] as const)
.choices(packageManagerChoices)
.default(getPackageManager())
.hideHelp(),
)
Expand Down
4 changes: 2 additions & 2 deletions packages/create-catalyst/src/utils/pm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

export type PackageManager = 'npm' | 'pnpm' | 'yarn';

export function getPackageManager(packageManager?: PackageManager): PackageManager {
if (packageManager) return packageManager;
export const packageManagerChoices: PackageManager[] = ['npm', 'pnpm', 'yarn'];

export function getPackageManager(): PackageManager {
const userAgent = process.env.npm_config_user_agent || '';

if (userAgent.startsWith('yarn')) {
Expand Down

0 comments on commit 650256a

Please sign in to comment.