Skip to content

Commit

Permalink
fix: automates latest-rc build of sfdx-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
WillieRuemmele committed Oct 12, 2021
1 parent 79d2280 commit 9f78931
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
77 changes: 77 additions & 0 deletions src/commands/cli/latestrc/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { SfdxCommand } from '@salesforce/command';
import { exec, ExecOptions } from 'shelljs';
import { ensureString } from '@salesforce/ts-types';
import { Env } from '@salesforce/kit';
import { Octokit } from '@octokit/core';
import { bold } from 'chalk';
import { SinglePackageRepo } from '../../../repository';

export default class build extends SfdxCommand {
public async run(): Promise<void> {
const auth = ensureString(
new Env().getString('GH_TOKEN') ?? new Env().getString('GITHUB_TOKEN'),
'GH_TOKEN is required to be set in the environment'
);
// get the current version and implement the patch version for a default rc build
const repo = await SinglePackageRepo.create({ ux: this.ux });

const nextRCVersion = repo.package.getNextRCVersion();
repo.nextVersion = nextRCVersion;

this.ux.log(`starting on main and will checkout ${repo.nextVersion}`);

// start the latest-rc build process on a clean main branch
this.exec('git checkout main');
this.exec('git pull');
this.exec(`git checkout -b ${nextRCVersion}`);

// bump the version in the pjson to the new latest-rc
this.ux.log(`setting the version to ${nextRCVersion}`);
repo.package.setNextVersion(nextRCVersion);
repo.package.packageJson.version = nextRCVersion;

// bump resolution deps
this.ux.log('bumping resolutions in the package.json to their "latest"');
repo.package.bumpResolutions('latest');

// pin the pinned dependencies
this.ux.log('pinning dependencies in pinnedDependencies to "latest-rc"');
repo.package.pinDependencyVersions('latest-rc');
repo.package.writePackageJson();

// compare the command-snapshot and regenerate if they're changes, they'll be part of the PR
try {
this.exec('yarn snapshot-compare', { fatal: true, silent: true });
} catch {
this.exec('yarn snapshot-generate');
}

this.exec('yarn install');

// commit package.json/yarn.lock and potentially command-snapshot changes
this.exec('git add .');
this.exec(`git commit -m "chore(latest-rc): bump to ${nextRCVersion}"`);
this.exec(`git push --set-upstream origin ${nextRCVersion}`);

const octokit = new Octokit({ auth });
await octokit.request('POST /repos/salesforcecli/sfdx-cli/pulls', {
owner: 'salesforcecli',
repo: 'sfdx-cli',
head: nextRCVersion,
base: 'main',
title: `Release v${nextRCVersion} as latest-rc`,
body: 'Building latest-rc',
});
}

private exec(cmd: string, options: ExecOptions = { silent: true }): void {
this.log(bold(cmd));
exec(cmd, options);
}
}
28 changes: 27 additions & 1 deletion src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as semver from 'semver';
import { cli } from 'cli-ux';
import { exec, pwd } from 'shelljs';
import { fs, Logger, SfdxError } from '@salesforce/core';
import { AsyncOptionalCreatable, findKey } from '@salesforce/kit';
import { AsyncOptionalCreatable, findKey, toNumber } from '@salesforce/kit';
import { AnyJson, get, Nullable } from '@salesforce/ts-types';
import { Registry } from './registry';

Expand All @@ -21,6 +21,7 @@ export type PackageJson = {
scripts: Record<string, string>;
files?: string[];
pinnedDependencies?: string[];
resolutions?: Record<string, string>;
repository?: string;
sfdx?: PackageJsonSfdxProperty;
} & AnyJson;
Expand Down Expand Up @@ -123,6 +124,31 @@ export class Package extends AsyncOptionalCreatable {
fs.writeJsonSync(pkgJsonPath, this.packageJson);
}

public bumpResolutions(tag: string): void {
if (!this.packageJson.resolutions) {
throw new SfdxError('Bumping resolutions requires property "resolutions" to be present in package.json');
}

Object.keys(this.packageJson.resolutions).map((key: string) => {
const result = exec(`npm view ${key} dist-tags ${this.registry.getRegistryParameter()} --json`, {
silent: true,
});
const versions = JSON.parse(result.stdout) as Record<string, string>;
this.packageJson.resolutions[key] = versions[tag];
});
}

public getNextRCVersion(): string {
const result = exec(`npm view ${this.packageJson.name} dist-tags ${this.registry.getRegistryParameter()} --json`, {
silent: true,
});
const versions = JSON.parse(result.stdout) as Record<string, string>;

const versionParts = versions['latest-rc'].split('.');
const newPatch = toNumber(versionParts[1]) + 1;
return `${versionParts[0]}.${newPatch}.0`;
}

public pinDependencyVersions(targetTag: string): ChangedPackageVersions {
// get the list of dependencies to hardcode
if (!this.packageJson.pinnedDependencies) {
Expand Down

0 comments on commit 9f78931

Please sign in to comment.