Repository navigation
Expand file tree
/
Copy pathpublish-package.mjs
More file actions
134 lines (117 loc) · 5.5 KB
/
Copy pathpublish-package.mjs
File metadata and controls
134 lines (117 loc) · 5.5 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { execa } from 'execa';
import fs from 'fs/promises';
import glob from 'glob';
import path from 'path';
import semver from 'semver';
const MAX_RETRIES = 3;
const RETRY_DELAY = 10000; // 10 seconds
// Until create-ohif has a stable release, move its `latest` dist-tag to each
// new master beta, so plain `pnpm create ohif` scaffolds the current beta
// rather than the first version ever published (a registry gives a new
// package's first version `latest` even when it is published with --tag
// beta, and later --tag beta publishes leave it there). Only moves `latest`
// forward, and only while it is a prerelease: once a release/* branch
// publishes a stable version this does nothing, and can be removed.
// Never throws, so a tagging problem can't trigger a publish retry.
async function promoteLatestUntilStable(name, version) {
try {
const { stdout } = await execa('npm', ['view', name, 'dist-tags.latest']);
const latest = stdout.trim();
if (latest && !(semver.prerelease(latest) && semver.gt(version, latest))) {
console.log(`Leaving ${name}@latest at ${latest}`);
return;
}
await execa('npm', ['dist-tag', 'add', `${name}@${version}`, 'latest']);
console.log(`Moved ${name}@latest to ${version}`);
} catch (error) {
console.error(`Could not update ${name}@latest: ${error}`);
}
}
async function run() {
const { stdout: branchName } = await execa('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
// Tiered publish surface: only the published SDK packages -- the
// @ohif/* contract surface third-party plugin authors compile and run
// against (publishedSdkPackages in .rspack/pluginExternals.js) -- plus
// tooling are published to npm. App-only extensions/modes/platform packages
// are private:true and are consumed via the git checkout / workspace
// template instead. Note the asymmetry with publish-version.mjs, which
// deliberately keeps stamping EVERY workspace package (private ones
// included) so lockstep versioning is preserved; only the publish set is
// narrowed here. This list must stay equal to SDK_PUBLISHED in
// scripts/verify-tarballs.mjs, whose checkPublishListParity parses this
// array (in CI, before this script runs) and fails the build when the two
// lists drift in either direction.
const packages = [
'platform/core',
'platform/i18n',
'platform/ui-next',
'extensions/default',
'extensions/cornerstone',
// @ohif/app: published as the prebuilt viewer (the built dist folder,
// served as-is with a deployment's own app-config.js). It sits outside
// the plugin contract.
'platform/app',
// create-ohif scaffolder: published tooling alongside the SDK set.
// Ships raw .mjs + templates, no dist UMD, so
// verify-tarballs.mjs lists it under PUBLISHED_TOOLING (allowed
// non-private, exempt from the SDK tarball assertions).
'platform/create-ohif',
];
const rootDir = process.cwd();
for (const packagePathPattern of packages) {
const matchingDirectories = glob.sync(packagePathPattern, { cwd: rootDir });
for (const packageDirectory of matchingDirectories) {
try {
// change back to the root directory
process.chdir(rootDir);
const packageJsonPath = path.join(packageDirectory, 'package.json');
const packageJsonContent = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'));
if (packageJsonContent.private) {
console.log(`Skipping private package at ${packageDirectory}`);
continue;
}
// move to package directory
process.chdir(packageDirectory);
let retries = 0;
while (retries < MAX_RETRIES) {
try {
console.log(`Tying to publishing package at ${packageDirectory}`);
// Use `pnpm publish` (not npm) so the workspace:* specifiers on our
// internal @ohif/* deps are rewritten to the exact version in the
// published tarball. npm would publish the literal "workspace:*",
// which npm/yarn consumers cannot resolve. --no-git-checks because
// the bump commit/tag is created in CI on a possibly-detached ref.
// pnpm is also required because publishConfig field overrides
// (main/module/types/exports) are a pnpm-only feature; npm publish
// would ship the src-pointing dev manifest.
const publishArgs = ['publish', '--no-git-checks'];
if (branchName === 'master') {
publishArgs.push('--tag', 'beta');
}
await execa('pnpm', publishArgs);
console.log(`Successfully published package at ${packageDirectory}`);
if (branchName === 'master' && packageJsonContent.name === 'create-ohif') {
await promoteLatestUntilStable(packageJsonContent.name, packageJsonContent.version);
}
break;
} catch (error) {
retries++;
console.error(
`Failed to publish package at ${packageDirectory} with error ${error}, retrying... (${retries}/${MAX_RETRIES})`
);
await new Promise(resolve => setTimeout(resolve, RETRY_DELAY));
}
}
} catch (error) {
console.error(`An error occurred while processing ${packageDirectory}: ${error}`);
} finally {
process.chdir(rootDir); // Ensure we always move back to the root directory
}
}
}
console.log('Finished');
}
run().catch(err => {
console.error('Error encountered during package publish:', err);
process.exit(1);
});