Skip to content

Commit

Permalink
human readable ids (#56)
Browse files Browse the repository at this point in the history
* use human readable ids
  • Loading branch information
Noviny committed May 31, 2019
1 parent c00e65e commit 7399648
Show file tree
Hide file tree
Showing 21 changed files with 48 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .changeset/cuddly-eggs-burn/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"releases": [{ "name": "@changesets/cli", "type": "patch" }],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/cuddly-eggs-burn/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make ids human readable
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# changesets 🦋
# 🦋 changesets 🦋

> A way to manage your versioning and changelogs with a focus on mono-repos
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
"cli-table": "^0.3.1",
"detect-indent": "^6.0.0",
"fs-extra": "^7.0.1",
"get-workspaces": "^0.2.1",
"fuzzy": "^0.1.3",
"get-workspaces": "^0.2.1",
"globby": "^9.2.0",
"human-id": "^1.0.2",
"inquirer": "^3.3.0",
"inquirer-checkbox-plus-prompt": "^1.0.1",
"lodash.startcase": "^4.4.0",
Expand Down
27 changes: 14 additions & 13 deletions packages/cli/src/commands/add/__tests__/writeChangeset.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import fs from "fs-extra";
import path from "path";
import writeChangeset from "../writeChangeset";

import humanId from "human-id";

jest.mock("human-id");

const simpleChangeset = {
summary: "This is a summary",
releases: [{ name: "pkg-a", type: "minor" }],
Expand All @@ -21,7 +25,10 @@ describe("simple project", () => {
jest.clearAllMocks();
});
it("should write a changeset", async () => {
const changesetID = await writeChangeset(simpleChangeset, { cwd });
const changesetID = "ascii";
humanId.mockReturnValueOnce(changesetID);

await writeChangeset(simpleChangeset, { cwd });

const mdPath = path.join(cwd, ".changeset", changesetID, "changes.md");
const jsonPath = path.join(cwd, ".changeset", changesetID, "changes.json");
Expand All @@ -35,6 +42,9 @@ describe("simple project", () => {
expect(json).toEqual(rest);
});
it("should clean up empty folders", async () => {
const changesetID = "ascii";
humanId.mockReturnValueOnce(changesetID);

const emptyDirPath = path.join(cwd, ".changeset/empty-dir");

await fs.mkdir(emptyDirPath);
Expand All @@ -43,23 +53,14 @@ describe("simple project", () => {
expect(fs.pathExistsSync(emptyDirPath)).toBe(false);
});
it("should leave folders with contents", async () => {
const changesetID = "ascii";
humanId.mockReturnValueOnce(changesetID);

const fullFilePath = path.join(cwd, ".changeset/full-dir/changes.md");

await fs.ensureFile(fullFilePath);
await writeChangeset(simpleChangeset, { cwd });

expect(fs.pathExistsSync(fullFilePath)).toBe(true);
});
it("should not write the same changeset twice", async () => {
let message;
await writeChangeset(simpleChangeset, { cwd });
try {
await writeChangeset(simpleChangeset, { cwd });
} catch (e) {
message = e.message;
}
expect(message).toBe(
"A changeset with the unique ID 2b479644 already exists"
);
});
});
20 changes: 8 additions & 12 deletions packages/cli/src/commands/add/writeChangeset.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import fs from "fs-extra";
import path from "path";
import crypto from "crypto";
import prettier from "prettier";
import pkgDir from "pkg-dir";
import humanId from "human-id";

import { removeEmptyFolders } from "../../utils/removeFolders";
import getChangesetBase from "../../utils/getChangesetBase";

const getID = data => {
const hash = crypto.createHash("sha256");

hash.update(JSON.stringify(data));
return hash.digest("hex");
};

async function writeChangeset(changesetData, opts) {
const cwd = opts.cwd || process.cwd();

Expand All @@ -22,10 +16,12 @@ async function writeChangeset(changesetData, opts) {
const changesetBase = await getChangesetBase(cwd);

// Worth understanding that the ID merely needs to be a unique hash to avoid git conflicts
// There is no significance to using a hash of the changeset info aside from the fringe
// benefit that it stops adding the same changeset twice.
// If this is discovered to be bad or a more meaningful pattern is discovered, replacing this is fine.
const changesetID = getID(changesetData).slice(0, 8);
// experimenting with human readable ids to make finding changesets easier
const changesetID = humanId({
separator: "-",
capitalize: false
});

const prettierConfig = await prettier.resolveConfig(dir);

const newFolderPath = path.resolve(changesetBase, changesetID);
Expand Down
16 changes: 13 additions & 3 deletions packages/cli/src/commands/status/__tests__/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import path from "path";
import writeChangeset from "../../add/writeChangeset";
import status from "..";

import humanId from "human-id";

jest.mock("human-id");

const simpleChangeset = {
summary: "This is a summary",
releases: [
Expand All @@ -19,14 +23,14 @@ const simpleReleaseObj = {
{
name: "pkg-a",
type: "minor",
changesets: ["b5340909"],
changesets: ["ascii"],
commits: [],
version: "1.1.0"
},
{
name: "pkg-b",
type: "patch",
changesets: ["b5340909"],
changesets: ["ascii"],
commits: [],
version: "1.0.1"
}
Expand All @@ -40,7 +44,7 @@ const simpleReleaseObj = {
{ name: "pkg-a", type: "minor" },
{ name: "pkg-b", type: "patch" }
],
id: "b5340909",
id: "ascii",
dependents: [{ name: "pkg-b", type: "none", dependencies: [] }]
}
]
Expand All @@ -61,6 +65,9 @@ describe("status", () => {
});

it("should get the status for a simple changeset and return the release object", async () => {
const changesetID = "ascii";
humanId.mockReturnValueOnce(changesetID);

await writeChangesets([simpleChangeset], cwd);
const releaseObj = await status({ cwd });
expect(releaseObj).toEqual(simpleReleaseObj);
Expand All @@ -76,6 +83,9 @@ describe("status", () => {
it("should respect the output flag", async () => {
const output = "nonsense.json";

const changesetID = "ascii";
humanId.mockReturnValueOnce(changesetID);

await writeChangesets([simpleChangeset], cwd);
const probsUndefined = await status({ cwd, output });

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,11 @@ https-proxy-agent@^2.2.1:
agent-base "^4.1.0"
debug "^3.1.0"

human-id@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/human-id/-/human-id-1.0.2.tgz#e654d4b2b0d8b07e45da9f6020d8af17ec0a5df3"
integrity sha512-UNopramDEhHJD+VR+ehk8rOslwSfByxPIZyJRfV739NDhN5LF1fa1MqnzKm2lGTQRjNrjK19Q5fhkgIfjlVUKw==

iconv-lite@0.4.24, iconv-lite@^0.4.17, iconv-lite@^0.4.24, iconv-lite@^0.4.4:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
Expand Down

0 comments on commit 7399648

Please sign in to comment.