2022-04-15 15:19:05 +00:00
|
|
|
import { promises as fsp } from 'node:fs'
|
|
|
|
import { execSync } from 'node:child_process'
|
2022-08-30 08:40:09 +00:00
|
|
|
import { $fetch } from 'ohmyfetch'
|
2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2022-01-13 18:21:49 +00:00
|
|
|
import { globby } from 'globby'
|
2021-07-01 12:58:27 +00:00
|
|
|
|
2022-08-26 15:47:29 +00:00
|
|
|
interface Dep {
|
|
|
|
name: string,
|
|
|
|
range: string,
|
|
|
|
type: string
|
|
|
|
}
|
|
|
|
|
2021-07-01 12:58:27 +00:00
|
|
|
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')
|
|
|
|
|
2022-08-26 15:47:29 +00:00
|
|
|
const updateDeps = (reviver: (dep: Dep) => Dep | void) => {
|
2021-07-01 12:58:27 +00:00
|
|
|
for (const type of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {
|
|
|
|
if (!data[type]) { continue }
|
|
|
|
for (const e of Object.entries(data[type])) {
|
2022-08-26 15:47:29 +00:00
|
|
|
const dep: Dep = { name: e[0], range: e[1] as string, type }
|
2021-07-01 12:58:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
|
|
|
|
type Package = ThenArg<ReturnType<typeof loadPackage>>
|
|
|
|
|
|
|
|
async function loadWorkspace (dir: string) {
|
|
|
|
const workspacePkg = await loadPackage(dir)
|
2022-07-21 10:46:50 +00:00
|
|
|
const pkgDirs = (await globby(workspacePkg.data.workspaces || [], { onlyDirectories: true })).sort()
|
2021-07-01 12:58:27 +00:00
|
|
|
|
|
|
|
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) => {
|
2022-04-20 10:33:33 +00:00
|
|
|
find(from).data._name = find(from).data.name
|
2021-07-01 12:58:27 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main () {
|
|
|
|
const workspace = await loadWorkspace(process.cwd())
|
|
|
|
|
|
|
|
const commit = execSync('git rev-parse --short HEAD').toString('utf-8').trim()
|
|
|
|
const date = Math.round(Date.now() / (1000 * 60))
|
|
|
|
|
2022-08-30 08:40:09 +00:00
|
|
|
const nuxtPkg = workspace.find('nuxt')
|
|
|
|
const nitroInfo = await $fetch('https://registry.npmjs.org/nitropack-edge')
|
|
|
|
const latestNitro = nitroInfo['dist-tags'].latest
|
|
|
|
nuxtPkg.data.dependencies.nitropack = `npm:nitropack-edge@^${latestNitro}`
|
|
|
|
|
2021-07-01 12:58:27 +00:00
|
|
|
for (const pkg of workspace.packages.filter(p => !p.data.private)) {
|
|
|
|
workspace.setVersion(pkg.data.name, `${pkg.data.version}-${date}.${commit}`)
|
2022-04-20 08:52:39 +00:00
|
|
|
const newname = pkg.data.name === 'nuxt' ? 'nuxt3' : (pkg.data.name + '-edge')
|
|
|
|
workspace.rename(pkg.data.name, newname)
|
2021-07-01 12:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await workspace.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|