Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): allow the ability to specify package manager in init command #6820

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
installNewPackages,
} from '../../packageManager'
import {
ALLOWED_PACKAGE_MANAGERS,
allowedPackageManagersString,
getPartialEnvWithNpmPath,
type PackageManager,
} from '../../packageManager/packageManagerChoice'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like if the user specifies a disallowed package manager then we silently fall back to an allowed one. Should we warn the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can, what would the copy look like?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

output.print(`Given package manager "${packageManager}" isn't supported. Supported package managers are ${allowedPms}.`)
output.print(`Continuing using ${pkgManager}...`)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this make sense?

Screenshot 2024-05-31 at 10 12 26 AM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks good to me!

(However, we should consider supporting dog).

// 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) {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions packages/@sanity/cli/src/commands/init/initCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -22,6 +26,7 @@ Options
--project-plan <name> Optionally select a plan for a new project
--coupon <name> 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 <manager> Specify which package manager to use [allowed: ${allowedPackageManagersString}]
binoy14 marked this conversation as resolved.
Show resolved Hide resolved

Examples
# Initialize a new project, prompt for required information along the way
Expand Down Expand Up @@ -80,6 +85,8 @@ export interface InitFlags {
'reconfigure'?: boolean

'organization'?: string

'package-manager'?: PackageManager
}

export const initCommand: CliCommandDefinition<InitFlags> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']

/**
Expand Down
Loading