Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 19 additions & 9 deletions src/hooks/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,38 @@ import type { EventEmitter } from 'node:events'
import { exit } from 'node:process'
import { projectDependenciesHook } from '../hook'

type PackageManager = 'npm' | 'bun' | 'deno' | 'pnpm' | 'yarn'

const knownPackageManagers: { [key: string]: string } = {
export const knownPackageManagerNames = [
'npm',
'bun',
'deno',
'pnpm',
'yarn',
] as const
export type PackageManager = (typeof knownPackageManagerNames)[number]

const knownPackageManagers: Record<PackageManager, string> = {
npm: 'npm install',
bun: 'bun install',
deno: 'deno install',
pnpm: 'pnpm install',
yarn: 'yarn',
}

export const knownPackageManagerNames = Object.keys(knownPackageManagers)
const currentPackageManager = getCurrentPackageManager()

// Deno and Netlify need no dependency installation step
const excludeTemplate = ['deno', 'netlify']

export type EventMap = {
dependencies: unknown[]
packageManager: unknown[]
packageManager: [PackageManager]
completed: unknown[]
}

const registerInstallationHook = (
template: string,
installArg: boolean | undefined,
pmArg: string | undefined,
pmArg: PackageManager | undefined,
emitter: EventEmitter<EventMap>,
) => {
if (excludeTemplate.includes(template)) {
Expand Down Expand Up @@ -82,14 +88,14 @@ const registerInstallationHook = (
return
}

let packageManager: string
let packageManager: PackageManager

if (pmArg && installedPackageManagerNames.includes(pmArg)) {
if (pmArg) {
packageManager = pmArg
} else {
packageManager = await select({
message: 'Which package manager do you want to use?',
choices: installedPackageManagerNames.map((template: string) => ({
choices: installedPackageManagerNames.map((template) => ({
title: template,
value: template,
})),
Expand All @@ -111,6 +117,10 @@ const registerInstallationHook = (
try {
await spawn(command, args, {
cwd: directoryPath,
// On Windows, stderr from cmd.exe is encoded in the OEM code page (e.g. CP932),
// which causes garbled text when Node.js reads it as UTF-8.
// Using 'inherit' pipes stderr directly to the terminal to avoid this.
stderr: 'inherit',
})
} catch (error: unknown) {
if (error instanceof SubprocessError) {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
knownPackageManagerNames,
registerInstallationHook,
} from './hooks/dependencies'
import type { EventMap } from './hooks/dependencies'
import type { EventMap, PackageManager } from './hooks/dependencies'

const [major, minor] = version.split('.')
const ref = `v${major}.${minor}`
Expand Down Expand Up @@ -78,7 +78,7 @@ program
.action(main)

type ArgOptions = {
pm?: string
pm?: PackageManager
offline: boolean
install?: boolean
template?: string
Expand Down Expand Up @@ -156,7 +156,7 @@ async function main(
// Default package manager
let packageManager = pm ?? 'npm'
emitter.addListener('packageManager', (pm) => {
packageManager = String(pm)
packageManager = pm
})

registerInstallationHook(templateName, install, pm, emitter)
Expand Down