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

Commit

Permalink
feat: add bump still unfinished
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Jul 24, 2021
1 parent 9d415a1 commit d4ae294
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 109 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
# UNDER DEVELOPMENT

# mass-publish

A barebones bulk NPM publisher.

## Getting Started

You can install the CLI from NPM as a devDependency for your project.

```bash
npm install mass-publish -D
```

Then automatically generate `mass-publish.json` which will be used as the main configuration. It will also keep track of the last published commit for detecting changes.

```bash
npx mass-publish init
```

## Configuration

```js
{
packages: ["packages/"], // Directories to check for changes
ignoreExtension: [".json"], // Ignore differneces with file extension of a certain type (Optional)
commitMessage: "chore: release new versions", // Commit message on publish
commitFrom: "fullsha", // Commit SHA to compare differences from. This is automatically updated on every publish.
commitTo: "fullsha", // Commit SHA to compare differences to. Default is the head commit (Optional)
noVerify: false, // Refer to changed command flag
autobump: false, // Refer to bump command flag
yes: false, // Refer to bump command flag
}
```

Reference: [`config.ts`](https://github.com/fontsource/mass-publish/blob/main/src/changed/interfaces/config.ts)

## Commands

## How It Works

1. Use Nodegit to compare the last known mass-publish commit (or custom hash) with the latest commits and determine the changes from the diff.
1. Use isomorphic-git to compare the last known mass-publish commit (or custom hash) with the latest commits and determine the changes from the diff.
2. Bump packages and validate they are publishable by comparing versions on the NPM registry using package-json.
3. Use pacote and libnpmpublish to pack and publish the packages using an async queue.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"posttest": "eslint . --ext .ts --config .eslintrc",
"prepack": "rimraf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
"command": "./bin/run",
"version": "oclif-dev readme && git add README.md"
"version": "oclif-dev readme && git add README.md",
"test": "jest"
},
"oclif": {
"commands": "./lib/commands",
Expand Down Expand Up @@ -53,10 +54,10 @@
"@oclif/dev-cli": "^1.26.0",
"@types/jest": "^26.0.24",
"@types/jsonfile": "^6.0.1",
"@types/node": "^16.4.0",
"@types/node": "^16.4.1",
"@types/nodegit": "^0.27.3",
"@types/pacote": "^11.1.1",
"@types/semver": "^7.3.7",
"@types/semver": "^7.3.8",
"@typescript-eslint/eslint-plugin": "^4.28.4",
"@typescript-eslint/parser": "^4.28.4",
"eslint": "^7.31.0",
Expand Down
17 changes: 11 additions & 6 deletions src/bump/bump-check.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import chalk from "chalk";
import * as latestVersion from "./re-export";
import latestVersion from "latest-version";
import { mocked } from "ts-jest/utils";

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

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

jest.mock("latest-version");

describe("Bump check", () => {
let results: any;

Expand All @@ -18,7 +21,8 @@ describe("Bump check", () => {
afterEach(() => jest.restoreAllMocks());

test("No updates needed", async () => {
jest.spyOn(latestVersion, "latestVersion").mockResolvedValue("1.0.0");
const mockedLatestVersion = mocked(latestVersion, false);
mockedLatestVersion.mockResolvedValue("1.0.0");

const validList: BumpObject[] = [
{
Expand All @@ -33,7 +37,8 @@ describe("Bump check", () => {
});

test("Updates needed", async () => {
jest.spyOn(latestVersion, "latestVersion").mockResolvedValue("1.0.1");
const mockedLatestVersion = mocked(latestVersion, false);
mockedLatestVersion.mockResolvedValue("1.0.1");

const bumpList: BumpObject[] = [
{
Expand All @@ -52,10 +57,10 @@ describe("Bump check", () => {

const checkedList = await bumpCheck(bumpList);
expect(checkedList).toEqual(validList);
expect(results).toEqual(
expect(results).toEqual([
chalk.red(
"test-1 version mismatch. NPM version 1.0.1. Bump value 1.0.1. Auto-bumping..."
)
);
),
]);
});
});
9 changes: 7 additions & 2 deletions src/bump/bump-check.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from "chalk";
import PQueue from "p-queue";
import semver from "semver";
import { latestVersion } from "./re-export";
import latestVersion from "latest-version";

import { bumpValue } from "./bump-value";
import log from "../utils/log";
Expand All @@ -16,7 +16,7 @@ const validate = async (item: BumpObject) => {
try {
// Get latest version from NPM registry and compare if bumped version is greater than NPM
npmVersion = await latestVersion(item.packageFile.name);
if (semver.gt(item.bumpedVersion, npmVersion)) {
if (semver.gt(item.bumpedVersion as string, npmVersion)) {
return item;
}
} catch {
Expand All @@ -28,6 +28,11 @@ const validate = async (item: BumpObject) => {
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;
}
Expand Down
21 changes: 16 additions & 5 deletions src/bump/bump-write.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import chalk from "chalk";
import jsonfile from "jsonfile";
import path from "path";

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

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

await jsonfile.writeFile(
path.join(item.packagePath, "package.json"),
newPackageFile
);
await jsonfile.writeFile(
path.join(item.packagePath, "package.json"),
newPackageFile
);
} else {
log(
chalk.red(
`Did not write ${item.packageFile.name} due to failed bumpedVersion.`
)
);
}
};

// Returns an array of promises that need to be resolved
const bumpWrite = (bumpList: BumpObject[]): Promise<void[]> => {
const promises = [];
for (const item of bumpList) {
Expand Down
63 changes: 7 additions & 56 deletions src/bump/bump.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,9 @@
import async from "async";
import chalk from "chalk";
import latestVersion from "latest-version";
import path from "path";
import jsonfile from "jsonfile";
import semver from "semver";
import { bumpCheck } from "./bump-check";
import { bumpValue } from "./bump-value";
import { bumpWrite } from "./bump-write";
import { createBumpObject } from "./create-bump";

import log from "../utils/log";
import { pathToPackage } from "../utils/path-to-package";
import type { BumpObject } from "./interfaces/bump-object";

import { PackageJson } from "../utils/interfaces/package-json";
import { QueueObject } from "./interfaces/queue-object";
import { BumpObject } from "./interfaces/bump-object";

const processQueue = async ({
packagePath,
packageFile,
bumpArg,
verify,
}: QueueObject) => {
let npmVersion: string | false;
// Get latest version from NPM registry
try {
npmVersion = await latestVersion(packageFile.name);
} catch {
// Assume package isn't published
npmVersion = false;
}

// Bump current package.json
const currentVersion = packageFile.version;
const bumpedVersion = bumpValue(currentVersion, bumpArg);

if (bumpedVersion && npmVersion) {
// Compare if bumped version is greater than version on NPM
if (semver.gt(bumpedVersion, npmVersion)) {
await writeUpdate(packageFile, packagePath, bumpedVersion);
} else {
// Try bumping one more time this time
const newBumpedVersion = bumpValue(bumpedVersion, "patch");
if (newBumpedVersion && semver.gt(newBumpedVersion, npmVersion)) {
await writeUpdate(packageFile, packagePath, newBumpedVersion);
} else {
log(
chalk.red(
`${packageFile.name} failed to bump. Specified bump version is less than NPM version.`
)
);
}
}
} else {
log(
chalk.red(
`${packageFile.name} failed to bump. Possible failure due to bump value or incompatible existing package.json value.`
)
);
}
};
export { bumpCheck, bumpValue, bumpWrite, createBumpObject };
export type { BumpObject };
33 changes: 33 additions & 0 deletions src/bump/create-bump.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { mocked } from "ts-jest/utils";
import { createBumpObject } from "./create-bump";
import { pathToPackage } from "../utils/path-to-package";

jest.mock("../utils/path-to-package");

describe("Create bump object", () => {
test("Success", () => {
const mockedPathPackage = mocked(pathToPackage);
mockedPathPackage.mockReturnValue([
{ name: "test-1", version: "1.0.0" },
{ name: "test-2", version: "1.1.1" },
]);

const diffExample = ["packages/test-1", "packages/test-2"];
const result = createBumpObject(diffExample, "patch");

const validResult = [
{
packageFile: { name: "test-1", version: "1.0.0" },
packagePath: "packages/test-1",
bumpedVersion: "1.0.1",
},
{
packageFile: { name: "test-2", version: "1.1.1" },
packagePath: "packages/test-2",
bumpedVersion: "1.1.2",
},
];

expect(result).toEqual(validResult);
});
});
35 changes: 35 additions & 0 deletions src/bump/create-bump.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { bumpValue } from "./bump-value";
import { pathToPackage } from "../utils/path-to-package";

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

const createBumpObject = (diff: string[], bumpArg: string): BumpObject[] => {
const bumpObjectArr: BumpObject[] = [];
const packageJsons = pathToPackage(diff);

diff.forEach((filePath, index) => {
const bumpedVersion = bumpValue(packageJsons[index].version, bumpArg);
if (bumpedVersion) {
const bumpObject: BumpObject = {
packageFile: packageJsons[index],
packagePath: filePath,
bumpedVersion,
};

bumpObjectArr.push(bumpObject);
} else {
const bumpObject: BumpObject = {
packageFile: packageJsons[index],
packagePath: filePath,
bumpedVersion,
failedValidation: true,
};

bumpObjectArr.push(bumpObject);
}
});

return bumpObjectArr;
};

export { createBumpObject };
2 changes: 1 addition & 1 deletion src/bump/interfaces/bump-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { PackageJson } from "../../utils/interfaces/package-json";
export interface BumpObject {
packageFile: PackageJson;
packagePath: string;
bumpedVersion: string;
bumpedVersion: string | false;
failedValidation?: boolean;
}
8 changes: 0 additions & 8 deletions src/bump/interfaces/queue-object.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/bump/re-export.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/changed/changed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { readConfig } from "./read-config";
import { findDiff } from "./find-diff";

export { findDiff, readConfig };
2 changes: 2 additions & 0 deletions src/changed/find-diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from "path";

import { findDiff } from "./find-diff";

jest.setTimeout(50_000);

describe("Find diff", () => {
test("Find test package difference #1 - no change", () => {
const exampleConfig = {
Expand Down
28 changes: 28 additions & 0 deletions src/commands/bump.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import chalk from "chalk";
import path from "path";
import { mocked } from "ts-jest/utils";

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

jest.mock("../bump/bump-write.ts");

describe("Bump command", () => {
let result: any;

beforeEach(() => {
result = [];
jest
.spyOn(process.stdout, "write")
.mockImplementation(val => result.push(String(val).trim()));

const mockedBumpWrite = mocked(bumpWrite);
mockedBumpWrite.mockResolvedValue([]);
});

afterEach(() => jest.restoreAllMocks());

test("Success", async () => {
await Bump.run(["patch"]);
});
});
Loading

0 comments on commit d4ae294

Please sign in to comment.