|
| 1 | +import { promises as fsp } from 'fs' |
| 2 | +import { execSync } from 'child_process' |
| 3 | +import { resolve } from 'pathe' |
| 4 | +import { globby } from 'globby' |
| 5 | + |
| 6 | +// Temporary forked from nuxt/framework |
| 7 | + |
| 8 | +async function loadPackage (dir: string) { |
| 9 | + const pkgPath = resolve(dir, 'package.json') |
| 10 | + const data = JSON.parse(await fsp.readFile(pkgPath, 'utf-8').catch(() => '{}')) |
| 11 | + const save = () => fsp.writeFile(pkgPath, JSON.stringify(data, null, 2) + '\n') |
| 12 | + |
| 13 | + const updateDeps = (reviver: Function) => { |
| 14 | + for (const type of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) { |
| 15 | + if (!data[type]) { continue } |
| 16 | + for (const e of Object.entries(data[type])) { |
| 17 | + const dep = { name: e[0], range: e[1], type } |
| 18 | + delete data[type][dep.name] |
| 19 | + const updated = reviver(dep) || dep |
| 20 | + data[updated.type] = data[updated.type] || {} |
| 21 | + data[updated.type][updated.name] = updated.range |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + return { |
| 27 | + dir, |
| 28 | + data, |
| 29 | + save, |
| 30 | + updateDeps |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type ThenArg<T> = T extends PromiseLike<infer U> ? U : T |
| 35 | +type Package = ThenArg<ReturnType<typeof loadPackage>> |
| 36 | + |
| 37 | +async function loadWorkspace (dir: string) { |
| 38 | + const workspacePkg = await loadPackage(dir) |
| 39 | + const pkgDirs = await globby(workspacePkg.data.workspaces || [], { onlyDirectories: true }) |
| 40 | + |
| 41 | + const packages: Package[] = [workspacePkg] |
| 42 | + |
| 43 | + for (const pkgDir of pkgDirs) { |
| 44 | + const pkg = await loadPackage(pkgDir) |
| 45 | + if (!pkg.data.name) { continue } |
| 46 | + packages.push(pkg) |
| 47 | + } |
| 48 | + |
| 49 | + const find = (name: string) => { |
| 50 | + const pkg = packages.find(pkg => pkg.data.name === name) |
| 51 | + if (!pkg) { |
| 52 | + throw new Error('Workspace package not found: ' + name) |
| 53 | + } |
| 54 | + return pkg |
| 55 | + } |
| 56 | + |
| 57 | + const rename = (from: string, to: string) => { |
| 58 | + find(from).data.name = to |
| 59 | + for (const pkg of packages) { |
| 60 | + pkg.updateDeps((dep) => { |
| 61 | + if (dep.name === from && !dep.range.startsWith('npm:')) { |
| 62 | + dep.range = 'npm:' + to + '@' + dep.range |
| 63 | + } |
| 64 | + }) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const setVersion = (name: string, newVersion: string) => { |
| 69 | + find(name).data.version = newVersion |
| 70 | + for (const pkg of packages) { |
| 71 | + pkg.updateDeps((dep) => { |
| 72 | + if (dep.name === name) { |
| 73 | + dep.range = newVersion |
| 74 | + } |
| 75 | + }) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const save = () => Promise.all(packages.map(pkg => pkg.save())) |
| 80 | + |
| 81 | + return { |
| 82 | + dir, |
| 83 | + workspacePkg, |
| 84 | + packages, |
| 85 | + save, |
| 86 | + find, |
| 87 | + rename, |
| 88 | + setVersion |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +async function main () { |
| 93 | + const workspace = await loadWorkspace(process.cwd()) |
| 94 | + |
| 95 | + const commit = execSync('git rev-parse --short HEAD').toString('utf-8').trim() |
| 96 | + const date = Math.round(Date.now() / (1000 * 60)) |
| 97 | + |
| 98 | + for (const pkg of workspace.packages.filter(p => !p.data.private)) { |
| 99 | + workspace.setVersion(pkg.data.name, `${pkg.data.version}-${date}.${commit}`) |
| 100 | + workspace.rename(pkg.data.name, pkg.data.name + '-edge') |
| 101 | + } |
| 102 | + |
| 103 | + await workspace.save() |
| 104 | +} |
| 105 | + |
| 106 | +main().catch((err) => { |
| 107 | + // eslint-disable-next-line no-console |
| 108 | + console.error(err) |
| 109 | + process.exit(1) |
| 110 | +}) |
0 commit comments