Skip to content

Commit

Permalink
Merge pull request #1155 from intuit/quiet
Browse files Browse the repository at this point in the history
Add --quiet flag
  • Loading branch information
hipstersmoothie committed Apr 17, 2020
2 parents 23019b4 + abc9745 commit adf4e85
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 44 deletions.
12 changes: 12 additions & 0 deletions packages/cli/src/parse-args.ts
Expand Up @@ -80,6 +80,14 @@ const onlyPublishWithReleaseLabel: AutoOption = {
group: "main",
};

const quiet: AutoOption = {
name: "quiet",
alias: "q",
type: Boolean,
description: "Print **only** the result of the command",
group: "global",
};

const defaultOptions: AutoOption[] = [
{
name: "verbose",
Expand Down Expand Up @@ -215,6 +223,7 @@ const latestCommandArgs: AutoOption[] = [
prerelease,
changelogTitle,
changelogCommitMessage,
quiet,
];

export const commands: AutoCommand[] = [
Expand Down Expand Up @@ -414,6 +423,7 @@ export const commands: AutoCommand[] = [
changelogTitle,
changelogCommitMessage,
baseBranch,
quiet,
],
examples: [
{
Expand Down Expand Up @@ -535,6 +545,7 @@ export const commands: AutoCommand[] = [
"Force a canary release, even if the PR is marked to skip the release",
config: true,
},
quiet,
],
},
{
Expand All @@ -557,6 +568,7 @@ export const commands: AutoCommand[] = [
"The message used when attaching the prerelease version to a PR",
config: true,
},
quiet,
],
},
];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/__tests__/auto-canary-local.test.ts
Expand Up @@ -47,5 +47,5 @@ test("shipit should publish canary in locally when not on master", async () => {
auto.hooks.canary.tap("test", canary);

await auto.shipit();
expect(canary).toHaveBeenCalledWith(SEMVER.patch, ".abc");
expect(canary).toHaveBeenCalledWith(SEMVER.patch, "canary.abc");
});
10 changes: 5 additions & 5 deletions packages/core/src/__tests__/auto-in-pr-ci.test.ts
Expand Up @@ -61,7 +61,7 @@ describe("canary in ci", () => {
jest.spyOn(auto.release!, "getCommits").mockImplementation();

await auto.canary();
expect(canary).toHaveBeenCalledWith(SEMVER.patch, ".123.1");
expect(canary).toHaveBeenCalledWith(SEMVER.patch, "canary.123.1");
});

test("comments on PR in CI", async () => {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("canary in ci", () => {
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap("test", (bump, post) => `1.2.4-canary${post}`);
auto.hooks.canary.tap("test", (bump, post) => `1.2.4-${post}`);

await auto.canary({ pr: 123, build: 1, message: "false" });
expect(addToPrBody).not.toHaveBeenCalled();
Expand All @@ -135,7 +135,7 @@ describe("canary in ci", () => {
const addToPrBody = jest.fn();
auto.git!.addToPrBody = addToPrBody;
jest.spyOn(auto.release!, "getCommits").mockImplementation();
auto.hooks.canary.tap("test", (bump, post) => `1.2.4-canary${post}`);
auto.hooks.canary.tap("test", (bump, post) => `1.2.4-${post}`);

const version = await auto.canary({ pr: 456, build: 5 });
expect(version!.newVersion).toBe("1.2.4-canary.456.5");
Expand All @@ -159,7 +159,7 @@ describe("shipit in ci", () => {
auto.hooks.canary.tap("test", canary);

await auto.shipit();
expect(canary).toHaveBeenCalledWith(SEMVER.patch, ".123.1");
expect(canary).toHaveBeenCalledWith(SEMVER.patch, "canary.123.1");
});
});

Expand All @@ -168,7 +168,7 @@ describe("next in ci", () => {
const auto = new Auto({ ...defaults, plugins: [] });

// @ts-ignore
jest.spyOn(console, 'log').mockImplementation();
jest.spyOn(console, "log").mockImplementation();
exec.mockResolvedValue("v1.0.0");
// @ts-ignore
auto.checkClean = () => Promise.resolve(true);
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/__tests__/auto.test.ts
Expand Up @@ -8,6 +8,7 @@ import child from "child_process";

const importMock = jest.fn();

jest.mock("../utils/git-reset.ts");
jest.mock("../utils/load-plugins.ts");
jest.mock("../utils/verify-auth.ts", () => () => true);
jest.mock("import-cwd", () => (path: string) => importMock(path));
Expand Down Expand Up @@ -1066,7 +1067,7 @@ describe("Auto", () => {
jest.spyOn(auto.release!, "getCommits").mockImplementation();

await auto.canary({ pr: 123, build: 1 });
expect(canary).toHaveBeenCalledWith(SEMVER.patch, ".123.1");
expect(canary).toHaveBeenCalledWith(SEMVER.patch, "canary.123.1");
expect(auto.git!.addToPrBody).toHaveBeenCalled();
});

Expand Down Expand Up @@ -1114,7 +1115,7 @@ describe("Auto", () => {
jest.spyOn(auto.release!, "getCommits").mockImplementation();

await auto.canary();
expect(canary).toHaveBeenCalledWith(SEMVER.patch, ".abc");
expect(canary).toHaveBeenCalledWith(SEMVER.patch, "canary.abc");
});

test("doesn't comment if there is an error", async () => {
Expand Down Expand Up @@ -1153,7 +1154,7 @@ describe("Auto", () => {
auto.hooks.canary.tap("test", canary);

await auto.canary();
expect(canary).toHaveBeenCalledWith(SEMVER.patch, ".abcd");
expect(canary).toHaveBeenCalledWith(SEMVER.patch, "canary.abcd");
});

test('should not publish when is present "skip-release" label', async () => {
Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/auto-args.ts
Expand Up @@ -49,6 +49,11 @@ export type IVersionOptions = ReleaseCalculationOptions & {
from?: string;
};

interface Quiet {
/** Print **only** the result of the command */
quiet?: boolean;
}

interface NoVersionPrefix {
/** Whether to prefix the version with a "v" */
noVersionPrefix?: boolean;
Expand Down Expand Up @@ -82,6 +87,7 @@ interface BaseBranch {
export type IChangelogOptions = BaseBranch &
ChangelogTitle &
ChangelogMessage &
Quiet &
DryRun &
NoVersionPrefix &
Partial<AuthorInformation> & {
Expand Down Expand Up @@ -124,6 +130,7 @@ export type IShipItOptions = BaseBranch &
ChangelogMessage &
ChangelogTitle &
DryRun &
Quiet &
Partial<AuthorInformation> &
Partial<RepoInformation> &
ReleaseCalculationOptions & {
Expand All @@ -136,7 +143,7 @@ export type IShipItOptions = BaseBranch &
onlyGraduateWithReleaseLabel?: boolean;
};

export interface ICanaryOptions {
export type ICanaryOptions = Quiet & {
/** Do not actually do anything */
dryRun?: boolean;
/** THe PR to attach the canary to */
Expand All @@ -147,14 +154,14 @@ export interface ICanaryOptions {
message?: string | "false";
/** Always deploy a canary, even if the PR is marked as skip release */
force?: boolean;
}
};

export interface INextOptions {
export type INextOptions = Quiet & {
/** Do not actually do anything */
dryRun?: boolean;
/** The message used when attaching the prerelease version to a PR */
message?: string;
}
};

export interface IInfoOptions {
/** List some of the available plugins */
Expand Down
124 changes: 101 additions & 23 deletions packages/core/src/auto.ts
Expand Up @@ -65,6 +65,7 @@ import {
import { omit } from "./utils/omit";
import { execSync } from "child_process";
import isBinary from "./utils/is-binary";
import { gitReset } from "./utils/git-reset";

const proxyUrl = process.env.https_proxy || process.env.http_proxy;
const env = envCi();
Expand Down Expand Up @@ -324,7 +325,9 @@ export default class Auto {
this.options = options;
this.baseBranch = options.baseBranch || "master";
setLogLevel(
Array.isArray(options.verbose) && options.verbose.length > 1
"quiet" in options && options.quiet
? "quiet"
: Array.isArray(options.verbose) && options.verbose.length > 1
? "veryVerbose"
: options.verbose
? "verbose"
Expand Down Expand Up @@ -1110,10 +1113,32 @@ export default class Auto {
canaryVersion = `${canaryVersion}.${await this.git.getSha(true)}`;
}

canaryVersion = `canary${canaryVersion}`;

if (options.dryRun) {
this.logger.log.warn(
`Published canary identifier would be: "-canary${canaryVersion}"`
);
const lastRelease = await this.git.getLatestRelease();
const current = await this.getCurrentVersion(lastRelease);

if (parse(current)) {
const next = determineNextVersion(
lastRelease,
current,
version,
canaryVersion
);

if (options.quiet) {
console.log(next);
} else {
this.logger.log.warn(`Published canary identifier would be: ${next}`);
}
} else if (options.quiet) {
console.log(`-${canaryVersion}`);
} else {
this.logger.log.warn(
`Published canary identifier would be: "-${canaryVersion}"`
);
}
} else {
this.logger.verbose.info("Calling canary hook");
const result = await this.hooks.canary.promise(version, canaryVersion);
Expand Down Expand Up @@ -1154,7 +1179,11 @@ export default class Auto {
`Published canary version${newVersion ? `: ${newVersion}` : ""}`
);

await execPromise("git", ["reset", "--hard", "HEAD"]);
if (args.quiet) {
console.log(newVersion);
}

await gitReset();
}

let latestTag: string;
Expand Down Expand Up @@ -1236,18 +1265,48 @@ export default class Auto {
calculateSemVerBump(labels, this.semVerLabels!, this.config) ||
SEMVER.patch;

this.logger.log.info("Full Release notes for next release:");
console.log(fullReleaseNotes);
if (!args.quiet) {
this.logger.log.info("Full Release notes for next release:");
console.log(fullReleaseNotes);

if (releaseNotes) {
this.logger.log.info("Release notes for last change in next release");
console.log(releaseNotes);
if (releaseNotes) {
this.logger.log.info("Release notes for last change in next release");
console.log(releaseNotes);
}
}

if (options.dryRun) {
this.logger.log.success(
`Would have created prerelease version with: ${bump} from ${lastTag}`
);
const lastRelease = await this.git.getLatestRelease();
const current = await this.getCurrentVersion(lastRelease);

if (parse(current)) {
const prereleaseBranches = this.config?.prereleaseBranches!;
const branch = getCurrentBranch() || "";
const prereleaseBranch = prereleaseBranches.includes(branch)
? branch
: prereleaseBranches[0];
const prerelease = determineNextVersion(
lastRelease,
current,
bump,
prereleaseBranch
);

if (options.quiet) {
console.log(prerelease);
} else {
this.logger.log.success(
`Would have created prerelease version: ${prerelease}`
);
}
} else if (options.quiet) {
// The following cases could use some work. They are really just there for lerna independent
console.log(`${bump} on ${lastTag}`);
} else {
this.logger.log.success(
`Would have created prerelease version with: ${bump} on ${lastTag}`
);
}

return { newVersion: "", commitsInRelease: commits, context: "next" };
}
Expand Down Expand Up @@ -1300,13 +1359,22 @@ export default class Auto {
}
}

await execPromise("git", ["reset", "--hard", "HEAD"]);
if (options.quiet) {
console.log(newVersion);
}

await gitReset();
return { newVersion, commitsInRelease: commits, context: "next" };
}

/** Force a release to latest and bypass `shipit` safeguards. */
async latest(options: IShipItOptions = {}) {
await this.publishFullRelease(options);
async latest(args: IShipItOptions = {}) {
const options = {
...(this.getCommandDefault("latest") as IShipItOptions),
...args,
};

return this.publishFullRelease(options);
}

/**
Expand Down Expand Up @@ -1373,15 +1441,15 @@ export default class Auto {
await this.hooks.beforeShipIt.promise({ releaseType });

if (releaseType === "latest") {
publishInfo = await this.publishFullRelease(options);
publishInfo = await this.latest(options);
} else if (releaseType === "old") {
publishInfo = await this.oldRelease(options);
} else if (releaseType === "next") {
publishInfo = await this.next(options);
} else {
publishInfo = await this.canary(options);

if (options.dryRun) {
if (options.dryRun && !options.quiet) {
this.logger.log.success(
"Below is what would happen upon merge of the current branch into master"
);
Expand Down Expand Up @@ -1473,7 +1541,7 @@ export default class Auto {
lastRelease
);

await this.makeChangelog(options);
await this.makeChangelog({ ...options, quiet: undefined });

if (!options.dryRun) {
await this.checkClean();
Expand All @@ -1493,10 +1561,16 @@ export default class Auto {
const current = await this.getCurrentVersion(lastRelease);

if (parse(current)) {
this.logger.log.warn(
`Published version would be: ${inc(current, version as ReleaseType)}`
);
const next = inc(current, version as ReleaseType);

if (options.quiet) {
console.log(next);
} else {
this.logger.log.warn(`Published version would be: ${next}`);
}
}
} else if (options.quiet) {
console.log(newVersion);
}

return { newVersion, commitsInRelease, context: "latest" };
Expand Down Expand Up @@ -1608,7 +1682,11 @@ export default class Auto {
return;
}

this.logger.log.info("New Release Notes\n", releaseNotes);
if (args.quiet) {
console.log(releaseNotes);
} else {
this.logger.log.info("New Release Notes\n", releaseNotes);
}

const currentVersion = await this.getCurrentVersion(lastRelease);

Expand Down

0 comments on commit adf4e85

Please sign in to comment.