Skip to content

Commit

Permalink
chore: add setup-overrides script
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed May 2, 2024
1 parent d8304bb commit 46a8739
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions scripts/setup-overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import assert from 'node:assert'
import fs from 'node:fs'
import { dirname } from 'node:path'
import fg from 'fast-glob'

// usage:
// npx tsx scripts/setup-overrides.ts <target-package.json>

async function main() {
const [targetPath] = process.argv.slice(2)
assert.ok(fs.existsSync(targetPath))

const pkgPaths = await fg('./packages/*/package.json', { absolute: true })
const overrides: Record<string, string> = {}
for (const pkgPath of pkgPaths) {
const pkg = await readJson(pkgPath)
overrides[pkg.name] = `file:${dirname(pkgPath)}`
}

await editJson(targetPath, (data) => {
Object.assign((data.pnpm ??= {}).overrides ??= {}, overrides)
return data
})
}

async function readJson(file: string) {
return JSON.parse(await fs.promises.readFile(file, 'utf-8'))
}

async function editJson(file: string, edit: (data: any) => any) {
const data = await readJson(file)
await fs.promises.writeFile(file, JSON.stringify(edit(data), null, 2))
}

main()

0 comments on commit 46a8739

Please sign in to comment.