Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
feat: add autobump flag and import refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Jul 26, 2021
1 parent 2f749b5 commit 45c1978
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 36 deletions.
51 changes: 47 additions & 4 deletions src/bump/bump-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import latestVersion from "latest-version";
import { mocked } from "ts-jest/utils";

import { bumpCheck } from "./bump-check";

import { BumpObject } from "./interfaces/bump-object";
import type { BumpObject } from "./interfaces/bump-object";

jest.mock("latest-version");

Expand Down Expand Up @@ -36,7 +35,24 @@ describe("Bump check", () => {
expect(checkedList).toEqual(validList);
});

test("Updates needed", async () => {
test("NPM unpublished", async () => {
const mockedLatestVersion = mocked(latestVersion, false);
// eslint-disable-next-line unicorn/no-useless-undefined
mockedLatestVersion.mockRejectedValue(undefined);

const validList: BumpObject[] = [
{
packageFile: { name: "test-1", version: "1.0.0" },
packagePath: "src/test",
bumpedVersion: "1.0.1",
},
];

const checkedList = await bumpCheck(validList);
expect(checkedList).toEqual(validList);
});

test("Failed check, no autobump", async () => {
const mockedLatestVersion = mocked(latestVersion, false);
mockedLatestVersion.mockResolvedValue("1.0.1");

Expand All @@ -51,12 +67,39 @@ describe("Bump check", () => {
{
packageFile: { name: "test-1", version: "1.0.0" },
packagePath: "src/test",
bumpedVersion: "1.0.2",
bumpedVersion: "1.0.1",
failedValidation: true,
},
];

const checkedList = await bumpCheck(bumpList);
expect(checkedList).toEqual(validList);
expect(results).toEqual([
"test-1 version mismatch. Failed to bump. Not publishing.",
]);
});

test("Autobump", async () => {
const mockedLatestVersion = mocked(latestVersion, false);
mockedLatestVersion.mockResolvedValue("1.0.1");

const bumpList: BumpObject[] = [
{
packageFile: { name: "test-1", version: "1.0.0" },
packagePath: "src/test",
bumpedVersion: "1.0.1",
},
];
const validList: BumpObject[] = [
{
packageFile: { name: "test-1", version: "1.0.0" },
packagePath: "src/test",
bumpedVersion: "1.0.2",
},
];

const checkedList = await bumpCheck(bumpList, true);
expect(checkedList).toEqual(validList);
expect(results).toEqual([
chalk.red(
"test-1 version mismatch. NPM version 1.0.1. Bump value 1.0.1. Auto-bumping..."
Expand Down
35 changes: 20 additions & 15 deletions src/bump/bump-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import semver from "semver";
import latestVersion from "latest-version";

import { bumpValue } from "./bump-value";
import log from "../utils/log";
import { log } from "../utils/utils";

import { BumpObject } from "./interfaces/bump-object";
import type { BumpObject } from "./bump";

const queue = new PQueue({ concurrency: 6 });
const queue = new PQueue({ concurrency: 12 });

const validate = async (item: BumpObject) => {
const validate = async (item: BumpObject, autoBump: boolean) => {
let npmVersion: string | boolean;
try {
// Get latest version from NPM registry and compare if bumped version is greater than NPM
Expand All @@ -25,15 +25,17 @@ const validate = async (item: BumpObject) => {

// If bumped value is not greater than NPM, auto bump with patch
const newItem = item;
const newVersion = bumpValue(npmVersion, "patch");
if (newVersion) {
log(
chalk.red(
`${newItem.packageFile.name} version mismatch. NPM version ${npmVersion}. Bump value ${item.bumpedVersion}. Auto-bumping...`
)
);
newItem.bumpedVersion = newVersion;
return newItem;
if (autoBump) {
const newVersion = bumpValue(npmVersion, "patch");
if (newVersion) {
log(
chalk.red(
`${newItem.packageFile.name} version mismatch. NPM version ${npmVersion}. Bump value ${item.bumpedVersion}. Auto-bumping...`
)
);
newItem.bumpedVersion = newVersion;
return newItem;
}
}

// If failed to bump again, will not publish
Expand All @@ -46,11 +48,14 @@ const validate = async (item: BumpObject) => {
return newItem;
};

const bumpCheck = async (bumpList: BumpObject[]): Promise<BumpObject[]> => {
const bumpCheck = async (
bumpList: BumpObject[],
autoBump = false
): Promise<BumpObject[]> => {
const checkedList: Promise<BumpObject>[] = [];

for (const item of bumpList) {
const validatedItem = queue.add(() => validate(item));
const validatedItem = queue.add(() => validate(item, autoBump));
checkedList.push(validatedItem);
}

Expand Down
7 changes: 6 additions & 1 deletion src/bump/bump-value.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ describe("Bump Value", () => {
expect(newVersion).toBe("2.5.1");
});

test("False", () => {
test("False input version", () => {
const newVersion = bumpValue("a.b.c", "major");
expect(newVersion).toBeFalsy();
});

test("False bump arg", () => {
const newVersion = bumpValue("1.0.0", "test");
expect(newVersion).toBeFalsy();
});
});
2 changes: 1 addition & 1 deletion src/bump/bump-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import jsonfile from "jsonfile";

import { bumpWrite } from "./bump-write";

import { BumpObject } from "./interfaces/bump-object";
import type { BumpObject } from "./bump";

describe("Bump write function", () => {
test("Should not throw", async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/bump/bump-write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import log from "../utils/log";

const writeUpdate = async (item: BumpObject) => {
const newPackageFile = item.packageFile;
if (item.bumpedVersion) {
if (item.bumpedVersion && !item.failedValidation) {
newPackageFile.version = item.bumpedVersion;

await jsonfile.writeFile(
Expand All @@ -17,7 +17,7 @@ const writeUpdate = async (item: BumpObject) => {
} else {
log(
chalk.red(
`Did not write ${item.packageFile.name} due to failed bumpedVersion.`
`Did not write ${item.packageFile.name} due to failed bump validation.`
)
);
}
Expand Down
5 changes: 2 additions & 3 deletions src/bump/create-bump.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { bumpValue } from "./bump-value";
import { pathToPackage } from "../utils/path-to-package";
import { pathToPackage, isPackageJson } from "../utils/utils";

import { BumpObject } from "./interfaces/bump-object";
import { isPackageJson } from "../utils/interfaces/is-package-json";
import type { BumpObject } from "./bump";

const createBumpObject = (diff: string[], bumpArg: string): BumpObject[] => {
const bumpObjectArr: BumpObject[] = [];
Expand Down
3 changes: 1 addition & 2 deletions src/changed/find-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { walk, TREE } from "isomorphic-git";

import path from "path";

import { Config } from "./interfaces/config";
import { GitWalk } from "./interfaces/git-walk";
import type { Config, GitWalk } from "./changed";

const findDiff = async ({
packages,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class Bump extends Command {
// Skip checking if no verify flag
const checkedObjects = noVerify
? bumpObjects
: await bumpCheck(bumpObjects);
: await bumpCheck(bumpObjects, autoBump);

if (checkedObjects.length === 0) {
throw new CLIError("No packages to update found.");
Expand Down
8 changes: 3 additions & 5 deletions src/commands/changed.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { Command, flags } from "@oclif/command";
import { CLIError } from "@oclif/errors";
import chalk from "chalk";
import { cli } from "cli-ux";

import { CLIError } from "@oclif/errors";
import { findDiff } from "../changed/find-diff";
import { pathToPackage } from "../utils/path-to-package";
import { readConfig } from "../changed/read-config";
import { isPackageJson } from "../utils/interfaces/is-package-json";
import { readConfig, findDiff } from "../changed/changed";
import { pathToPackage, isPackageJson } from "../utils/utils";

export default class Changed extends Command {
static description = "Detects what packages have changed since last publish";
Expand Down
15 changes: 15 additions & 0 deletions src/commands/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import jsonfile from "jsonfile";
import Init from "./init";

test("Init command", async () => {
const results: any = [];
jest
.spyOn(process.stdout, "write")
.mockImplementation(val => results.push(String(val).trim()));

const writeFile = jest.spyOn(jsonfile, "writeFile").mockResolvedValue();

await expect(Init.run()).resolves.not.toThrow();
expect(writeFile).toHaveBeenCalledTimes(1);
expect(results).toEqual(["mass-publish.json has been created."]);
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isPackageJson } from "./is-package-json";
import { PackageJson, Removed } from "./package-json";
import { PackageJson, Removed } from "../interfaces/package-json";

describe("Is package json type guard", () => {
test("PackageJson", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { PackageJson } from "./package-json";
import { PackageJson } from "../interfaces/package-json";

export const isPackageJson = (object: any): object is PackageJson => {
return "name" in object;
Expand Down
13 changes: 13 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import log from "./log";
import { pathToPackage } from "./path-to-package";

import { isPackageJson } from "./guards/is-package-json";

import type {
PackageJson,
PackageJsonObject,
Removed,
} from "./interfaces/package-json";

export { log, pathToPackage, isPackageJson };
export type { PackageJson, PackageJsonObject, Removed };

0 comments on commit 45c1978

Please sign in to comment.