diff --git a/packages/@sanity/cli/src/actions/init-project/initProject.ts b/packages/@sanity/cli/src/actions/init-project/initProject.ts index c0e838dac2e..9407c35e860 100644 --- a/packages/@sanity/cli/src/actions/init-project/initProject.ts +++ b/packages/@sanity/cli/src/actions/init-project/initProject.ts @@ -21,6 +21,8 @@ import { installNewPackages, } from '../../packageManager' import { + ALLOWED_PACKAGE_MANAGERS, + allowedPackageManagersString, getPartialEnvWithNpmPath, type PackageManager, } from '../../packageManager/packageManagerChoice' @@ -133,6 +135,7 @@ export default async function initSanity( const useGit = typeof commitMessage === 'undefined' ? true : Boolean(commitMessage) const bareOutput = cliFlags.bare const env = cliFlags.env + const packageManager = cliFlags['package-manager'] let defaultConfig = cliFlags['dataset-default'] let showDefaultConfigPrompt = !defaultConfig @@ -458,8 +461,6 @@ export default async function initSanity( // eslint-disable-next-line no-process-exit process.exit(0) - - return } // eslint-disable-next-line @typescript-eslint/no-shadow @@ -542,15 +543,35 @@ export default async function initSanity( // Bootstrap Sanity, creating required project files, manifests etc await bootstrapTemplate(templateOptions, context) - // Now for the slow part... installing dependencies - const pkgManager = await getPackageManagerChoice(outputPath, { - prompt, - interactive: unattended ? false : isInteractive, - }) + let pkgManager: PackageManager + + // If the user has specified a package manager, and it's allowed use that + if (packageManager && ALLOWED_PACKAGE_MANAGERS.includes(packageManager)) { + pkgManager = packageManager + } else { + // Otherwise, try to find the most optimal package manager to use + pkgManager = ( + await getPackageManagerChoice(outputPath, { + prompt, + interactive: unattended ? false : isInteractive, + }) + ).chosen - trace.log({step: 'selectPackageManager', selectedOption: pkgManager.chosen}) + // only log warning if a package manager flag is passed + if (packageManager) { + output.warn( + chalk.yellow( + `Given package manager "${packageManager}" is not supported. Supported package managers are ${allowedPackageManagersString}.`, + ), + ) + output.print(`Using ${pkgManager} as package manager`) + } + } + + trace.log({step: 'selectPackageManager', selectedOption: pkgManager}) - await installDeclaredPackages(outputPath, pkgManager.chosen, context) + // Now for the slow part... installing dependencies + await installDeclaredPackages(outputPath, pkgManager, context) // Try initializing a git repository if (useGit) { @@ -585,7 +606,7 @@ export default async function initSanity( bun: 'bun dev', manual: 'npm run dev', } - const devCommand = devCommandMap[pkgManager.chosen] + const devCommand = devCommandMap[pkgManager] const isCurrentDir = outputPath === process.cwd() if (isCurrentDir) { diff --git a/packages/@sanity/cli/src/commands/init/initCommand.ts b/packages/@sanity/cli/src/commands/init/initCommand.ts index 89e41bb5c28..2863f626b38 100644 --- a/packages/@sanity/cli/src/commands/init/initCommand.ts +++ b/packages/@sanity/cli/src/commands/init/initCommand.ts @@ -3,6 +3,10 @@ import {detectFrameworkRecord, LocalFileSystemDetector} from '@vercel/fs-detecto import initPlugin from '../../actions/init-plugin/initPlugin' import initProject from '../../actions/init-project/initProject' +import { + allowedPackageManagersString, + type PackageManager, +} from '../../packageManager/packageManagerChoice' import {type CliCommandDefinition} from '../../types' const helpText = ` @@ -22,6 +26,7 @@ Options --project-plan Optionally select a plan for a new project --coupon Optionally select a coupon for a new project (cannot be used with --project-plan) --no-typescript Do not use TypeScript for template files + --package-manager Specify which package manager to use [allowed: ${allowedPackageManagersString}] Examples # Initialize a new project, prompt for required information along the way @@ -80,6 +85,8 @@ export interface InitFlags { 'reconfigure'?: boolean 'organization'?: string + + 'package-manager'?: PackageManager } export const initCommand: CliCommandDefinition = { diff --git a/packages/@sanity/cli/src/packageManager/packageManagerChoice.ts b/packages/@sanity/cli/src/packageManager/packageManagerChoice.ts index 6e1c0ac21a8..8d419d6e783 100644 --- a/packages/@sanity/cli/src/packageManager/packageManagerChoice.ts +++ b/packages/@sanity/cli/src/packageManager/packageManagerChoice.ts @@ -9,6 +9,11 @@ import {isInteractive} from '../util/isInteractive' export type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun' | 'manual' +export const ALLOWED_PACKAGE_MANAGERS: PackageManager[] = ['npm', 'yarn', 'pnpm', 'bun', 'manual'] +export const allowedPackageManagersString = ALLOWED_PACKAGE_MANAGERS.map((pm) => `"${pm}"`).join( + ' | ', +) + const EXPERIMENTAL = ['bun'] /**