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

@@ -14,6 +14,7 @@ const argv = yargs
.usage('$0 [args]') .usage('$0 [args]')
.options({ .options({
newVersion: {key: 'new-version', alias: 'v', type: 'string'}, newVersion: {key: 'new-version', alias: 'v', type: 'string'},
dryRun: {key: 'dry-run', alias: 'd', type: 'boolean'},
}) })
.help().argv; .help().argv;

View File

@@ -15,6 +15,7 @@ const argv = yargs
.options({ .options({
newVersion: {key: 'new-version', alias: 'v', type: 'string'}, newVersion: {key: 'new-version', alias: 'v', type: 'string'},
proxy: {key: 'proxy', alias: 'p', type: 'string'}, proxy: {key: 'proxy', alias: 'p', type: 'string'},
dryRun: {key: 'dry-run', alias: 'd', type: 'boolean'},
}) })
.help().argv; .help().argv;

View File

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