Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix smoke tests to account for new release versions within a workspace #8143

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions scripts/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { log } = require('proc-log')
const pacote = require('pacote')
const { read } = require('read')
const Table = require('cli-table3')
const { run, git, npm, pkg: cli, spawn } = require('./util.js')
const { run, git, npm, pkgPath: cliPath, pkg: cli, spawn } = require('./util.js')
const fs = require('fs').promises

const resetdeps = () => npm('run', 'resetdeps')

Expand Down Expand Up @@ -49,22 +50,40 @@ const versionNotExists = async ({ name, version }) => {
const getPublishes = async ({ force }) => {
const publishPackages = []

for (const { pkg } of await cli.mapWorkspaces({ public: true })) {
for (const { pkg, pkgPath } of await cli.mapWorkspaces({ public: true })) {
const updatePkg = async (cb) => {
const data = JSON.parse(await fs.readFile(pkgPath, 'utf8'))
const result = cb(data)
await fs.writeFile(pkgPath, JSON.stringify(result, null, 2))
return result
}

if (force || await versionNotExists(pkg)) {
publishPackages.push({
workspace: true,
workspace: `--workspace=${pkg.name}`,
name: pkg.name,
version: pkg.version,
dependencies: pkg.dependencies,
devDependencies: pkg.devDependencies,
tag: await getWorkspaceTag(pkg),
updatePkg,
})
}
}

if (force || await versionNotExists(cli)) {
publishPackages.push({
workspace: '',
name: cli.name,
version: cli.version,
tag: `next-${semver.major(cli.version)}`,
dependencies: cli.dependencies,
devDependencies: cli.devDependencies,
updatePkg: async (cb) => {
const result = cb(cli)
await fs.writeFile(cliPath, JSON.stringify(result, null, 2))
return result
},
})
}

Expand Down Expand Up @@ -128,6 +147,36 @@ const main = async (opts) => {
}

let count = -1

if (smokePublish) {
// when we have a smoke test run we'd want to bump the version or else npm will throw an error even with dry-run
// this is the equivlent of running `npm version prerelease`, but ensureing all internally used workflows are bumped
for (const publish of publishes) {
const { version } = await publish.updatePkg((pkg) => ({ ...pkg, version: `${pkg.version}-smoke.0` }))
for (const ipublish of publishes) {
if (ipublish.dependencies?.[publish.name]) {
await ipublish.updatePkg((pkg) => ({
...pkg,
dependencies: {
...pkg.dependencies,
[publish.name]: version,
},
}))
}
if (ipublish.devDependencies?.[publish.name]) {
await ipublish.updatePkg((pkg) => ({
...pkg,
devDependencies: {
...pkg.devDependencies,
[publish.name]: version,
},
}))
}
}
}
await npm('install')
}

for (const publish of publishes) {
log.info(`Publishing ${publish.name}@${publish.version} to ${publish.tag} ${count++}/${publishes.length}`)
const workspace = publish.workspace && `--workspace=${publish.name}`
Expand All @@ -142,8 +191,6 @@ const main = async (opts) => {
}

if (smokePublish) {
// when we have a smoke test run we'd want to bump the version or else npm will throw an error even with dry-run
await npm('version', 'prerelease', workspace, '--preid=smoke', '--ignore-scripts', '--no-git-tag-version')
await publishPkg('--dry-run', '--ignore-scripts')
} else {
await publishPkg(
Expand Down
7 changes: 5 additions & 2 deletions scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ const mapWorkspaces = require('@npmcli/map-workspaces')
const EOL = '\n'
const CWD = resolve(__dirname, '..')

const rootPkgPath = join(CWD, 'package.json')
const pkg = require(join(CWD, 'package.json'))
pkg.mapWorkspaces = async ({ public = false } = {}) => {
const ws = []
for (const [name, path] of await mapWorkspaces({ pkg })) {
const pkgJson = require(join(path, 'package.json'))
const pkgPath = join(path, 'package.json')
const pkgJson = require(pkgPath)

if (public && pkgJson.private) {
continue
}

ws.push({ name, path, pkg: pkgJson })
ws.push({ name, path, pkgPath, pkg: pkgJson })
}
return ws
}
Expand Down Expand Up @@ -205,6 +207,7 @@ const run = async (main, { redact } = {}) => {
module.exports = {
CWD,
pkg,
pkgPath: rootPkgPath,
run,
fs,
spawn,
Expand Down