Dry-run for version bumping and npm publishing scripts

Summary: To make testing easier, I've implemented dry-run for version bumping and npm publishing scripts

Reviewed By: mweststrate

Differential Revision: D21721142

fbshipit-source-id: 72fc5a78bfc102a8fca9087decfbed6ebbda5e51
This commit is contained in:
Anton Nikolaev
2020-05-26 06:39:49 -07:00
committed by Facebook GitHub Bot
parent dec5045fca
commit 6005d039f7
3 changed files with 57 additions and 11 deletions

View File

@@ -60,8 +60,14 @@ export async function getWorkspaces(): Promise<Workspaces> {
};
}
export async function bumpVersions({newVersion}: {newVersion?: string}) {
return await bumpWorkspaceVersions(await getWorkspaces(), newVersion);
export async function bumpVersions({
newVersion,
dryRun,
}: {
newVersion?: string;
dryRun?: boolean;
}) {
return await bumpWorkspaceVersions(await getWorkspaces(), newVersion, dryRun);
}
async function savePackageJson({dir, json}: Package) {
@@ -71,6 +77,7 @@ async function savePackageJson({dir, json}: Package) {
}
function updateDependencies(
name: string,
dependencies: {[key: string]: string},
packagesToUpdate: string[],
newVersion: string,
@@ -84,6 +91,9 @@ function updateDependencies(
dependencies[packageName] !== undefined &&
dependencies[packageName] !== newVersion
) {
console.log(
`Updated dependency of ${name}: ${packageName} from version ${dependencies[packageName]} to version ${newVersion}`,
);
dependencies[packageName] = newVersion;
updated = true;
}
@@ -94,6 +104,7 @@ function updateDependencies(
async function bumpWorkspaceVersions(
{rootPackage, packages}: Workspaces,
newVersion?: string,
dryRun?: boolean,
): Promise<string> {
newVersion = newVersion || (rootPackage.json.version as string);
const allPackages = [rootPackage, ...packages];
@@ -104,24 +115,51 @@ async function bumpWorkspaceVersions(
const {dir, json} = pkg;
let changed = false;
if (json.version !== newVersion && !isPlugin(dir)) {
console.log(
`Bumping version of ${pkg.json.name} from ${json.version} to ${newVersion}`,
);
json.version = newVersion;
changed = true;
}
if (updateDependencies(json.dependencies, localPackageNames, newVersion)) {
changed = true;
}
if (
updateDependencies(json.devDependencies, localPackageNames, newVersion)
updateDependencies(
json.name,
json.dependencies,
localPackageNames,
newVersion,
)
) {
changed = true;
}
if (
updateDependencies(json.peerDependencies, localPackageNames, newVersion)
updateDependencies(
json.name,
json.devDependencies,
localPackageNames,
newVersion,
)
) {
changed = true;
}
if (
updateDependencies(
json.name,
json.peerDependencies,
localPackageNames,
newVersion,
)
) {
changed = true;
}
if (changed) {
await savePackageJson(pkg);
if (dryRun) {
console.log(
`DRYRUN: skipping saving changed package.json for ${pkg.json.name}`,
);
} else {
console.log(`Saving changed package.json for ${pkg.json.name}`);
await savePackageJson(pkg);
}
}
}
return newVersion;
@@ -130,19 +168,25 @@ async function bumpWorkspaceVersions(
export async function publishPackages({
newVersion,
proxy,
dryRun,
}: {
newVersion?: string;
proxy?: string;
dryRun?: boolean;
}) {
const workspaces = await getWorkspaces();
const version = await bumpWorkspaceVersions(workspaces, newVersion);
const version = await bumpWorkspaceVersions(workspaces, newVersion, dryRun);
let cmd = `yarn publish --new-version ${version}`;
if (proxy) {
cmd += ` --http-proxy ${proxy} --https-proxy ${proxy}`;
}
const publicPackages = workspaces.packages.filter((pkg) => !pkg.json.private);
for (const pkg of publicPackages) {
console.log(`Publishing ${pkg.json.name}...`);
execSync(cmd, {cwd: pkg.dir, stdio: 'inherit'});
if (dryRun) {
console.log(`DRYRUN: Skipping npm publishing for ${pkg.json.name}`);
} else {
console.log(`Publishing ${pkg.json.name}...`);
execSync(cmd, {cwd: pkg.dir, stdio: 'inherit'});
}
}
}