diff --git a/.changeset/neat-kangaroos-turn.md b/.changeset/neat-kangaroos-turn.md new file mode 100644 index 0000000000..6f1572d096 --- /dev/null +++ b/.changeset/neat-kangaroos-turn.md @@ -0,0 +1,5 @@ +--- +'@backstage/cli': patch +--- + +Tweaked workspace packaging to not rewrite existing `package.json` files. diff --git a/packages/cli/src/lib/packager/copyPackageDist.ts b/packages/cli/src/lib/packager/copyPackageDist.ts index 439b087fd7..b704148542 100644 --- a/packages/cli/src/lib/packager/copyPackageDist.ts +++ b/packages/cli/src/lib/packager/copyPackageDist.ts @@ -67,18 +67,21 @@ export async function copyPackageDist(packageDir: string, targetDir: string) { delete pkg.optionalDependencies; } - // Write the modified package.json so that the file listing is correct - await fs.writeJson(pkgPath, pkg, { encoding: 'utf8', spaces: 2 }); - // Lists all dist files, respecting .npmignore, files field in package.json, etc. - const filePaths = await npmPackList({ path: packageDir }); + const filePaths = await npmPackList({ + path: packageDir, + // This makes sure we use the update package.json when listing files + packageJsonCache: new Map([[resolvePath(packageDir, 'package.json'), pkg]]), + }); await fs.ensureDir(targetDir); for (const filePath of filePaths.sort()) { - await fs.copy( - resolvePath(packageDir, filePath), - resolvePath(targetDir, filePath), - ); + const target = resolvePath(targetDir, filePath); + if (filePath === 'package.json') { + await fs.writeJson(target, pkg, { encoding: 'utf8', spaces: 2 }); + } else { + await fs.copy(resolvePath(packageDir, filePath), target); + } } if (publishConfig.alphaTypes) { @@ -87,7 +90,4 @@ export async function copyPackageDist(packageDir: string, targetDir: string) { if (publishConfig.betaTypes) { await writeReleaseStageEntrypoint(pkg, 'beta', targetDir); } - - // Restore package.json - await fs.writeFile(pkgPath, pkgContent, 'utf8'); }