Skip to content

Commit d29ee76

Browse files
authored
devops: publish npm packages in dependency order and wait for them to go live (#43933)
1 parent edeb0bd commit d29ee76

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎utils/publish_all_packages.sh‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,31 @@ else
7373
exit 1
7474
fi
7575

76+
# A successful `npm publish` only means the registry accepted the package: it is processed
77+
# asynchronously and may show up minutes later, or never (playwright-core@1.65.0 never did,
78+
# leaving playwright@1.65.0 on @latest pointing to a missing dependency).
79+
function wait_until_available {
80+
local name="$1"
81+
local attempt
82+
for attempt in $(seq 60); do
83+
if [[ "$(npm view "${name}@${VERSION}" version --prefer-online 2>/dev/null)" == "${VERSION}" ]]; then
84+
return 0
85+
fi
86+
sleep 15
87+
done
88+
echo "ERROR: ${name}@${VERSION} was accepted by npm but did not become available within 15 minutes"
89+
return 1
90+
}
91+
7692
echo "==================== Publishing version ${VERSION} ================"
7793
node ./utils/workspace.js --ensure-consistent
78-
node ./utils/workspace.js --list-public-package-paths | while read package
79-
do
80-
npm publish --access=public ${package} --tag="${NPM_PUBLISH_TAG}"
94+
# Packages are listed in dependency order. Wait for each one to go live before publishing the next,
95+
# so nothing is published (and tagged) while a package it depends on is missing.
96+
PACKAGE_PATHS=$(node ./utils/workspace.js --list-public-package-paths)
97+
for package in ${PACKAGE_PATHS}; do
98+
name=$(node -p "require('${package}/package.json').name")
99+
npm publish --access=public "${package}" --tag="${NPM_PUBLISH_TAG}"
100+
wait_until_available "${name}"
81101
done
82102

83103
echo "Done."

‎utils/workspace.js‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ class Workspace {
5757
return this._packages;
5858
}
5959

60+
/**
61+
* Public packages, each listed after the workspace packages it depends on.
62+
* @returns {PWPackage[]}
63+
*/
64+
publicPackagesInDependencyOrder() {
65+
const ordered = new Set();
66+
const visit = pkg => {
67+
for (const dep of Object.keys(pkg.packageJSON.dependencies || {}))
68+
this._packages.filter(p => p.name === dep).forEach(visit);
69+
ordered.add(pkg);
70+
};
71+
this._packages.forEach(visit);
72+
return [...ordered].filter(pkg => !pkg.isPrivate);
73+
}
74+
6075
async version() {
6176
const workspacePackageJSON = await readJSON(path.join(this._rootDir, 'package.json'));
6277
return workspacePackageJSON.version;
@@ -219,10 +234,9 @@ async function parseCLI() {
219234
die(`\n ERROR: package-lock.json lockfileVersion must be 3`);
220235
},
221236
'--list-public-package-paths': () => {
222-
for (const pkg of workspace.packages()) {
223-
if (!pkg.isPrivate)
224-
console.log(pkg.path);
225-
}
237+
// Dependency order: publishing relies on it to never publish a package before its dependencies.
238+
for (const pkg of workspace.publicPackagesInDependencyOrder())
239+
console.log(pkg.path);
226240
},
227241
'--get-version': async (version) => {
228242
console.log(await workspace.version());

0 commit comments

Comments
 (0)