Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
157 lines
3.6 KiB
TypeScript
157 lines
3.6 KiB
TypeScript
import findUp from 'find-up'
|
|
import execa from 'execa'
|
|
import { basename } from 'node:path'
|
|
|
|
export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun'
|
|
|
|
export function getPkgManager(baseDir: string): PackageManager {
|
|
try {
|
|
const lockFile = findUp.sync(
|
|
[
|
|
'package-lock.json',
|
|
'yarn.lock',
|
|
'pnpm-lock.yaml',
|
|
'bun.lock',
|
|
'bun.lockb',
|
|
],
|
|
{ cwd: baseDir }
|
|
)
|
|
if (lockFile) {
|
|
switch (basename(lockFile)) {
|
|
case 'package-lock.json':
|
|
return 'npm'
|
|
case 'yarn.lock':
|
|
return 'yarn'
|
|
case 'pnpm-lock.yaml':
|
|
return 'pnpm'
|
|
case 'bun.lock':
|
|
case 'bun.lockb':
|
|
return 'bun'
|
|
default:
|
|
return 'npm'
|
|
}
|
|
}
|
|
// No lock file found, default to npm
|
|
return 'npm'
|
|
} catch {
|
|
return 'npm'
|
|
}
|
|
}
|
|
|
|
export function uninstallPackage(
|
|
packageToUninstall: string,
|
|
pkgManager?: PackageManager
|
|
) {
|
|
pkgManager ??= getPkgManager(process.cwd())
|
|
if (!pkgManager) throw new Error('Failed to find package manager')
|
|
|
|
let command = 'uninstall'
|
|
if (pkgManager === 'yarn') {
|
|
command = 'remove'
|
|
}
|
|
|
|
try {
|
|
execa.sync(pkgManager, [command, packageToUninstall], {
|
|
stdio: 'inherit',
|
|
shell: true,
|
|
})
|
|
} catch (error) {
|
|
throw new Error(
|
|
`Failed to uninstall "${packageToUninstall}". Please uninstall it manually.`,
|
|
{ cause: error }
|
|
)
|
|
}
|
|
}
|
|
|
|
const ADD_CMD_FLAG = {
|
|
npm: 'install',
|
|
yarn: 'add',
|
|
pnpm: 'add',
|
|
bun: 'add',
|
|
}
|
|
|
|
const DEV_DEP_FLAG = {
|
|
npm: '--save-dev',
|
|
yarn: '--dev',
|
|
pnpm: '--save-dev',
|
|
bun: '--dev',
|
|
}
|
|
|
|
export function installPackages(
|
|
packageToInstall: string[],
|
|
options: {
|
|
packageManager?: PackageManager
|
|
silent?: boolean
|
|
dev?: boolean
|
|
} = {}
|
|
) {
|
|
if (packageToInstall.length === 0) return
|
|
|
|
const {
|
|
packageManager = getPkgManager(process.cwd()),
|
|
silent = false,
|
|
dev = false,
|
|
} = options
|
|
|
|
if (!packageManager) throw new Error('Failed to find package manager')
|
|
|
|
const addCmd = ADD_CMD_FLAG[packageManager]
|
|
const devDepFlag = dev ? DEV_DEP_FLAG[packageManager] : undefined
|
|
|
|
const installFlags = [addCmd]
|
|
if (devDepFlag) {
|
|
installFlags.push(devDepFlag)
|
|
}
|
|
try {
|
|
execa.sync(packageManager, [...installFlags, ...packageToInstall], {
|
|
// Keeping stderr since it'll likely be relevant later when it fails.
|
|
stdio: silent ? ['ignore', 'ignore', 'inherit'] : 'inherit',
|
|
shell: true,
|
|
})
|
|
} catch (error) {
|
|
throw new Error(
|
|
`Failed to install "${packageToInstall}". Please install it manually.`,
|
|
{ cause: error }
|
|
)
|
|
}
|
|
}
|
|
|
|
export function runInstallation(
|
|
packageManager: PackageManager,
|
|
options: { cwd: string }
|
|
) {
|
|
try {
|
|
execa.sync(packageManager, ['install'], {
|
|
cwd: options.cwd,
|
|
env: {
|
|
...process.env,
|
|
// In case NODE_ENV=production is set, we still want dev dependencies to
|
|
// be installed. Otherwise we won't be able to check for peer dependencies.
|
|
// --production=false is not implemented by every package manager.
|
|
NODE_ENV: 'development',
|
|
},
|
|
stdio: 'inherit',
|
|
shell: true,
|
|
})
|
|
} catch (error) {
|
|
throw new Error('Failed to install dependencies', { cause: error })
|
|
}
|
|
}
|
|
|
|
export function addPackageDependency(
|
|
packageJson: Record<string, any>,
|
|
name: string,
|
|
version: string,
|
|
dev: boolean
|
|
): void {
|
|
if (dev) {
|
|
packageJson.devDependencies = packageJson.devDependencies || {}
|
|
} else {
|
|
packageJson.dependencies = packageJson.dependencies || {}
|
|
}
|
|
|
|
const deps = dev ? packageJson.devDependencies : packageJson.dependencies
|
|
|
|
deps[name] = version
|
|
}
|