forked from prisma/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset-version.ts
More file actions
executable file
·89 lines (73 loc) · 2.9 KB
/
Copy pathset-version.ts
File metadata and controls
executable file
·89 lines (73 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env node
import { execSync } from 'node:child_process';
import fs from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { participatesInLockstep, rewriteWorkspaceDeps } from './set-version-utils.ts';
const rootDir = path.dirname(path.dirname(fileURLToPath(import.meta.url)));
const version = process.argv[2];
if (!version) {
const script = path.relative(process.cwd(), process.argv[1]);
console.error(`Usage: node ${script} <version>`);
console.error(`Example: node ${script} 0.1.0-dev.123`);
process.exit(1);
}
interface PnpmPackage {
name: string;
version: string;
path: string;
private: boolean;
}
interface PackageJson {
name: string;
version: string;
private?: boolean;
[key: string]: unknown;
}
const output = execSync('pnpm list -r --json', {
cwd: rootDir,
encoding: 'utf-8',
});
const workspacePackages: PnpmPackage[] = JSON.parse(output);
let updatedCount = 0;
// Every workspace package — publishable, private, and the workspace
// root — gets the same version. Lockstep is the invariant that lets a
// single read of the root `package.json` answer "what version are we
// shipping right now?"; if private packages drifted, that invariant
// would be silently violated by direct invocations of this script.
for (const pkg of workspacePackages) {
const packageJsonPath = path.join(pkg.path, 'package.json');
const content = await fs.readFile(packageJsonPath, 'utf-8');
const packageJson: PackageJson = JSON.parse(content);
packageJson.version = version;
rewriteWorkspaceDeps(packageJson, version);
await fs.writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`);
console.log(`Updated ${pkg.name} to ${version}`);
updatedCount++;
}
// Project-boundary manifests (tracked package.json files that are not
// workspace members but carry `workspace:` pins, e.g. the per-database
// halves of examples/bundle-size) version in lockstep too. Without this
// sweep they go stale on every bump and fail at install once the old
// version leaves the registry.
const memberPaths = new Set(workspacePackages.map((pkg) => path.join(pkg.path, 'package.json')));
const trackedManifests = execSync("git ls-files -- '*package.json'", {
cwd: rootDir,
encoding: 'utf-8',
})
.split('\n')
.filter(Boolean)
.map((rel) => path.join(rootDir, rel))
.filter((abs) => !memberPaths.has(abs));
for (const manifestPath of trackedManifests) {
const packageJson: PackageJson = JSON.parse(await fs.readFile(manifestPath, 'utf-8'));
if (!participatesInLockstep(packageJson)) continue;
packageJson.version = version;
rewriteWorkspaceDeps(packageJson, version);
await fs.writeFile(manifestPath, `${JSON.stringify(packageJson, null, 2)}\n`);
console.log(
`Updated ${path.relative(rootDir, manifestPath)} (project-boundary manifest) to ${version}`,
);
updatedCount++;
}
console.log(`\nDone! Updated ${updatedCount} packages.`);