Skip to content

Commit

Permalink
Merge pull request #2075 from kelyvin/main
Browse files Browse the repository at this point in the history
Support --use-version argument with shipit
  • Loading branch information
hipstersmoothie committed Sep 15, 2021
2 parents 0b7399f + 41063f6 commit 9f1a57d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 42 deletions.
22 changes: 14 additions & 8 deletions packages/cli/src/parse-args.ts
Expand Up @@ -223,6 +223,14 @@ const force: AutoOption = {
config: true,
};

const useVersion: AutoOption = {
name: "use-version",
type: String,
group: "main",
description:
"Version number to publish as. Defaults to reading from the package definition for the platform.",
}

interface AutoCommand extends Command {
/** Options for the command */
options?: AutoOption[];
Expand Down Expand Up @@ -469,7 +477,7 @@ export const commands: AutoCommand[] = [
group: "Release Commands",
description: endent`
Create a GitHub release for a tag. Defaults to last tag in branch.
> NOTE: The tag must already be pushed to GitHub. If it isn't GitHub will create a tag pointing to the "to" option value.
`,
options: [
Expand All @@ -491,13 +499,7 @@ export const commands: AutoCommand[] = [
description:
"Git revision (tag, commit sha, ...) to end release notes at. Defaults to HEAD.",
},
{
name: "use-version",
type: String,
group: "main",
description:
"Version number to publish as. Defaults to reading from the package definition for the platform.",
},
useVersion,
baseBranch,
prerelease,
],
Expand Down Expand Up @@ -525,6 +527,10 @@ export const commands: AutoCommand[] = [
examples: ["{green $} auto shipit"],
options: [
...latestCommandArgs,
{
...useVersion,
description: `${useVersion.description} Currently only supported for the **npm plugin**.`
},
{
name: "only-graduate-with-release-label",
type: Boolean,
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/auto-args.ts
Expand Up @@ -107,12 +107,18 @@ export type IPRBodyOptions = Omit<ICommentOptions, "edit" | "delete">;

export type ILatestOptions = BaseBranch &
DryRunOption &
Partial<AuthorInformation> &
NonNullable<AutoRc["latest"]> &
NoVersionPrefix &
ChangelogTitle &
QuietOption &
ReleaseCalculationOptions;
ReleaseCalculationOptions &
Partial<AuthorInformation> &
Partial<RepoInformation> & {
/** Commit to start calculating the release from */
from?: string;
/** Override the version to release */
useVersion?: string;
};

export type IShipItOptions = ILatestOptions & NonNullable<AutoRc["shipit"]>;

Expand Down
69 changes: 44 additions & 25 deletions packages/core/src/auto.ts
Expand Up @@ -119,7 +119,7 @@ const makeDetail = (summary: string, body: string) => endent`
<details>
<summary>${summary}</summary>
<br />
${body}
</details>
`;
Expand Down Expand Up @@ -195,6 +195,8 @@ export interface IAutoHooks {
from: string;
/** Commit to end calculating the version from */
to: string;
/** Override the version to release */
useVersion?: string;
/** The version being released */
newVersion: string;
/** Whether the release being made is a prerelease */
Expand Down Expand Up @@ -243,6 +245,8 @@ export interface IAutoHooks {
QuietOption & {
/** The semver bump to apply */
bump: SEMVER;
/** Override the version to release */
useVersion?: string;
}
]
>;
Expand All @@ -254,6 +258,8 @@ export interface IAutoHooks {
{
/** The semver bump that was applied in the version hook */
bump: SEMVER;
/** Override the version to release */
useVersion?: string;
}
]
>;
Expand Down Expand Up @@ -536,13 +542,17 @@ export default class Auto {
private loadDefaultBehavior() {
this.hooks.makeRelease.tapPromise("Default", async (options) => {
if (options.dryRun) {
const bump = await this.getVersion({ from: options.from });
let releaseVersion;

if (options.useVersion) {
releaseVersion = options.useVersion;
} else {
const bump = await this.getVersion({ from: options.from });
releaseVersion = inc(options.newVersion, bump as ReleaseType);
}

this.logger.log.info(
`Would have created a release on GitHub for version: ${inc(
options.newVersion,
bump as ReleaseType
)}`
`Would have created a release on GitHub for version: ${releaseVersion}`
);
this.logger.log.note(
'The above version would only get released if ran with "shipit" or a custom script that bumps the version using the "version" command'
Expand Down Expand Up @@ -594,7 +604,7 @@ export default class Auto {
this.logger.log.error(
endent`
Found configuration errors:
${errors.map(formatError).join("\n")}
`,
"\n"
Expand Down Expand Up @@ -783,7 +793,7 @@ export default class Auto {
"auto" version: v${getAutoVersion()}
"git" version: v${gitVersion.replace('git version ', '')}
"node" version: ${process.version.trim()}${
access['x-github-enterprise-version']
access['x-github-enterprise-version']
? `\nGHE version: v${access['x-github-enterprise-version']}\n`
: '\n'}
${chalk.underline.white('Project Information:')}
Expand Down Expand Up @@ -977,8 +987,8 @@ export default class Auto {
this.logger.log.error(
endent`
Could not detect PR number. pr-check must be run from either a PR or have the PR number supplied via the --pr flag.
In some CIs your branch might be built before you open a PR and posting the canary version will fail. In this case subsequent builds should succeed.
In some CIs your branch might be built before you open a PR and posting the canary version will fail. In this case subsequent builds should succeed.
`
);
process.exit(1);
Expand Down Expand Up @@ -1207,7 +1217,7 @@ export default class Auto {
None of the plugins that you are using implement the \`canary\` command!
"canary" releases are versions that are used solely to test changes. They make sense on some platforms (ex: npm) but not all!
If you think your package manager has the ability to support canaries please file an issue or submit a pull request,
`);
process.exit(0);
Expand Down Expand Up @@ -1692,6 +1702,9 @@ export default class Auto {
options: ILatestOptions & {
/** Internal option to shipit from a certain tag or commit */
from?: string;

/** Override the version to release */
useVersion?: string;
}
): Promise<ShipitInfo | undefined> {
if (!this.git || !this.release) {
Expand All @@ -1700,9 +1713,9 @@ export default class Auto {

const bump = await this.getVersion(options);

this.logger.log.success(`Calculated version bump: ${bump || "none"}`);
this.logger.log.success(`Calculated version bump: ${options.useVersion || bump || "none"}`);

if (bump === "") {
if (bump === "" && !options.useVersion) {
this.logger.log.info("No version published.");
return;
}
Expand All @@ -1725,6 +1738,7 @@ export default class Auto {
this.logger.verbose.info("Calling version hook");
await this.hooks.version.promise({
bump,
useVersion: options.useVersion,
dryRun: options.dryRun,
quiet: options.quiet,
});
Expand All @@ -1733,7 +1747,10 @@ export default class Auto {

if (!options.dryRun) {
this.logger.verbose.info("Calling publish hook");
await this.hooks.publish.promise({ bump });
await this.hooks.publish.promise({
bump,
useVersion: options.useVersion
});
this.logger.verbose.info("Calling after publish hook");
await this.hooks.afterPublish.promise();
}
Expand All @@ -1753,8 +1770,8 @@ export default class Auto {
this.logger.log.error(
endent`
Could not detect PR number. ${command} must be run from either a PR or have the PR number supplied via the --pr flag.
In some CIs your branch might be built before you open a PR and posting the canary version will fail. In this case subsequent builds should succeed.
In some CIs your branch might be built before you open a PR and posting the canary version will fail. In this case subsequent builds should succeed.
`
);

Expand Down Expand Up @@ -1805,6 +1822,7 @@ export default class Auto {
from ||
(isPrerelease && latestTagInBranch) ||
(await this.git.getLatestRelease());

let calculatedBump = await this.release.getSemverBump(lastRelease);

// For next releases we also want to take into account any labels on
Expand Down Expand Up @@ -1924,7 +1942,7 @@ export default class Auto {
Could not find any tags in the local repository. Exiting early.
The "release" command creates GitHub releases for tags that have already been created in your repo.
If there are no tags there is nothing to release. If you don't use "shipit" ensure you tag your releases with the new version number.
`,
"\n"
Expand Down Expand Up @@ -1991,6 +2009,7 @@ export default class Auto {
dryRun,
from: lastRelease,
to: to || (await this.git.getSha()),
useVersion,
isPrerelease,
newVersion,
fullReleaseNotes: releaseNotes,
Expand Down Expand Up @@ -2073,14 +2092,14 @@ export default class Auto {
} else {
this.logger.log.error(endent`
.autorc author parsing failed!
The author must either be in one of the following formats:
1. Your Name <your_email@mail.com>
2. An object with "name" and "email"
But you supplied:
${author}
`);
}
Expand Down Expand Up @@ -2137,9 +2156,9 @@ export default class Auto {
Name: ${user.name}
Email: ${user.email}
You must do one of the following:
You must do one of the following:
- configure the author for your package manager (ex: set "author" in package.json)
- Set "name" and "email in your .autorc
`,
Expand All @@ -2163,7 +2182,7 @@ export default class Auto {
endent`
Cannot find project owner and repository name!
You must do one of the following:
You must do one of the following:
- configure the repo for your package manager (ex: set "repository" in package.json)
- configure your git remote 'origin' to point to your project on GitHub.
Expand Down
14 changes: 7 additions & 7 deletions plugins/npm/src/index.ts
Expand Up @@ -898,7 +898,7 @@ export default class NPMPlugin implements IPlugin {

auto.hooks.version.tapPromise(
this.name,
async ({ bump, dryRun, quiet }) => {
async ({ bump, useVersion, dryRun, quiet }) => {
const isBaseBranch = branch === auto.baseBranch;

/** Log the version */
Expand All @@ -920,7 +920,7 @@ export default class NPMPlugin implements IPlugin {
await execPromise("npx", [
"lerna",
"version",
bump,
useVersion || bump,
...(await getRegistryArgs()),
...getLegacyAuthArgs(this.legacyAuth, { isMonorepo: true }),
"--yes",
Expand All @@ -938,16 +938,16 @@ export default class NPMPlugin implements IPlugin {

logVersion(canaryPackageList.join("\n"));
} else {
logVersion(inc(monorepoVersion, bump as ReleaseType) || bump);
logVersion(useVersion || inc(monorepoVersion, bump as ReleaseType) || bump);
}

return;
}

const monorepoBump =
isIndependent || !isBaseBranch
? bump
: (await bumpLatest(getMonorepoPackage(), bump)) || bump;
? useVersion || bump
: useVersion || (await bumpLatest(getMonorepoPackage(), bump)) || bump;

await execPromise("npx", [
"lerna",
Expand All @@ -969,8 +969,8 @@ export default class NPMPlugin implements IPlugin {
}

const latestBump = isBaseBranch
? (await bumpLatest(await loadPackageJson(), bump)) || bump
: bump;
? useVersion || (await bumpLatest(await loadPackageJson(), bump)) || bump
: useVersion || bump;

if (dryRun) {
logVersion(latestBump);
Expand Down

0 comments on commit 9f1a57d

Please sign in to comment.