mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
74a90c566c
Co-authored-by: Pooya Parsa <pooya@pi0.io>
95 lines
2.4 KiB
TypeScript
95 lines
2.4 KiB
TypeScript
import { promises as fsp } from 'node:fs'
|
|
import { resolve } from 'pathe'
|
|
import { globby } from 'globby'
|
|
|
|
export interface Dep {
|
|
name: string,
|
|
range: string,
|
|
type: string
|
|
}
|
|
|
|
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
|
|
export type Package = ThenArg<ReturnType<typeof loadPackage>>
|
|
|
|
export async function loadPackage (dir: string) {
|
|
const pkgPath = resolve(dir, 'package.json')
|
|
const data = JSON.parse(await fsp.readFile(pkgPath, 'utf-8').catch(() => '{}'))
|
|
const save = () => fsp.writeFile(pkgPath, JSON.stringify(data, null, 2) + '\n')
|
|
|
|
const updateDeps = (reviver: (dep: Dep) => Dep | void) => {
|
|
for (const type of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {
|
|
if (!data[type]) { continue }
|
|
for (const e of Object.entries(data[type])) {
|
|
const dep: Dep = { name: e[0], range: e[1] as string, type }
|
|
delete data[type][dep.name]
|
|
const updated = reviver(dep) || dep
|
|
data[updated.type] = data[updated.type] || {}
|
|
data[updated.type][updated.name] = updated.range
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
dir,
|
|
data,
|
|
save,
|
|
updateDeps
|
|
}
|
|
}
|
|
|
|
export async function loadWorkspace (dir: string) {
|
|
const workspacePkg = await loadPackage(dir)
|
|
const pkgDirs = (await globby(['packages/*'], { onlyDirectories: true })).sort()
|
|
|
|
const packages: Package[] = []
|
|
|
|
for (const pkgDir of pkgDirs) {
|
|
const pkg = await loadPackage(pkgDir)
|
|
if (!pkg.data.name) { continue }
|
|
packages.push(pkg)
|
|
}
|
|
|
|
const find = (name: string) => {
|
|
const pkg = packages.find(pkg => pkg.data.name === name)
|
|
if (!pkg) {
|
|
throw new Error('Workspace package not found: ' + name)
|
|
}
|
|
return pkg
|
|
}
|
|
|
|
const rename = (from: string, to: string) => {
|
|
find(from).data._name = find(from).data.name
|
|
find(from).data.name = to
|
|
for (const pkg of packages) {
|
|
pkg.updateDeps((dep) => {
|
|
if (dep.name === from && !dep.range.startsWith('npm:')) {
|
|
dep.range = 'npm:' + to + '@' + dep.range
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
const setVersion = (name: string, newVersion: string) => {
|
|
find(name).data.version = newVersion
|
|
for (const pkg of packages) {
|
|
pkg.updateDeps((dep) => {
|
|
if (dep.name === name) {
|
|
dep.range = newVersion
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
const save = () => Promise.all(packages.map(pkg => pkg.save()))
|
|
|
|
return {
|
|
dir,
|
|
workspacePkg,
|
|
packages,
|
|
save,
|
|
find,
|
|
rename,
|
|
setVersion
|
|
}
|
|
}
|