Skip to content

Instantly share code, notes, and snippets.

@gunzip
Created March 6, 2026 07:22
Show Gist options
  • Select an option

  • Save gunzip/65772b08600bb9eed5ba62089ff26a81 to your computer and use it in GitHub Desktop.

Select an option

Save gunzip/65772b08600bb9eed5ba62089ff26a81 to your computer and use it in GitHub Desktop.
import fs from 'node:fs';
import path from 'node:path';
// Nx internals are shipped with nx; these imports work inside an Nx workspace.
import { createProjectGraphAsync } from 'nx/src/project-graph/project-graph';
import { readNxJson } from 'nx/src/config/configuration';
import { ReleaseClient } from 'nx/src/command-line/release';
const OUT_PATH = '.nx-release/version-data.json';
function ensureDir(p) {
fs.mkdirSync(p, { recursive: true });
}
const nxJson = readNxJson();
const releaseConfig = nxJson.release ?? {};
// Important: ignoreNxJsonConfig=false means it will merge with nx.json release config.
// We'll pass {} here and let it use nx.json as source of truth.
const client = new ReleaseClient({}, false);
const projectGraph = await createProjectGraphAsync({ exitOnError: true });
// Compute versions without writing/changing anything
const res = await client.releaseVersion({
dryRun: true,
verbose: false,
// This must match how you version in PR; if you rely on version plans, keep specifier undefined
// so it resolves version plans.
// You can also pass filters here if needed:
// projects: [...],
// groups: [...],
});
// The shape of `res` includes versioning info; we want a normalized `{[projectName]: {newVersion}}`
// Depending on Nx versions, it might be `res.projectsVersionData` or similar.
// If this fails, log res once and adjust mapping (only once).
const versionData =
res.projectsVersionData ??
res.versionData ??
res.projectVersionData ??
null;
if (!versionData) {
throw new Error(
`Could not find versionData in releaseVersion() result. Keys: ${Object.keys(
res ?? {}
).join(', ')}`
);
}
const projectsOut = {};
for (const [projectName, v] of Object.entries(versionData)) {
const node = projectGraph.nodes[projectName];
const projectRoot = node?.data?.root;
projectsOut[projectName] = {
projectRoot,
newVersion: v?.newVersion ?? null,
};
}
ensureDir(path.dirname(OUT_PATH));
fs.writeFileSync(
OUT_PATH,
JSON.stringify(
{
generatedAt: new Date().toISOString(),
projects: projectsOut,
},
null,
2
) + '\n',
'utf8'
);
console.log(`Wrote ${OUT_PATH}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment