Skip to content

Instantly share code, notes, and snippets.

@pi0
Last active March 15, 2022 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pi0/185498a1bcdd09561ae4c6ab2eb0d069 to your computer and use it in GitHub Desktop.
Save pi0/185498a1bcdd09561ae4c6ab2eb0d069 to your computer and use it in GitHub Desktop.
Script to migrate to pnpm
#!/bin/env node
import { existsSync } from 'fs'
import fs from 'fs/promises'
import path from 'path'
import { execSync } from 'child_process'
const LATEST_PNPM = '6.32.3'
const dir = path.resolve(process.argv[2] || '.')
const resolve = (...p) => path.resolve(dir, ...p)
const run = cmd => { execSync(cmd, { stdio: 'inherit', cwd: dir }) }
const remove = async (p) => {
if (!existsSync(p)) { return }
console.log('Removing', p)
await fs.rm(resolve(p), { recursive: true }).catch(() => {})
}
console.log('Migrating to pnpm in', dir)
// Remove lock files and node_modules
await remove('yarn.lock')
await remove('package-lock.json')
await remove('.yarn')
await remove('node_modules')
// Update package.json scripts and add packageManager
const pkg = JSON.parse(await fs.readFile(resolve('package.json')))
if (pkg.scripts) {
for (const scriptName in pkg.scripts) {
pkg.scripts[scriptName] = pkg.scripts[scriptName]
.replace(/yarn|^npm|(?<= )npm/g, 'pnpm')
}
}
pkg.packageManager = `pnpm@${LATEST_PNPM}`
console.log('Updating package.json...')
await fs.writeFile(resolve('package.json'), JSON.stringify(pkg, null, 2))
// Install
run('corepack enable')
run('pnpm i')
console.log('Reminder: Please manually ensure all references to yarn and npm are removed from github actions and README.md')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment