import { exec } from 'node:child_process'
import fs from 'node:fs/promises'
import path from 'node:path'
import prettier from 'prettier'
exec('pnpm --silent --filter=!./playgrounds/* -r exec pwd', async (err, stdout) => {
if (err) {
console.error(err)
process.exit(1)
}
let paths = stdout
.trim()
.split('\n')
.map((x) => path.resolve(x, 'package.json'))
for (let path of paths) {
let pkg = await fs.readFile(path, 'utf8').then(JSON.parse)
if (!pkg.version?.includes('-')) continue
let shouldUpdate = false
for (let group of [
'dependencies',
'devDependencies',
'peerDependencies',
'optionalDependencies',
]) {
for (let [name, version] of Object.entries(pkg[group] ?? {})) {
if (version === 'workspace:*' || !version.startsWith('workspace:')) continue
pkg[group][name] = 'workspace:*'
shouldUpdate = true
}
}
if (shouldUpdate) {
await fs.writeFile(
path,
await prettier
.format(JSON.stringify(pkg, null, 2), { filepath: path })
.then((x) => `${x.trim()}\n`),
)
}
}
console.log('Done.')
})