Skip to content

Commit

Permalink
Noviny/add read and parse packages for new format (#123)
Browse files Browse the repository at this point in the history
* initial release

Co-Authored-By: Mitchell Hamilton <mitchell@hamil.town>
  • Loading branch information
Noviny and emmatown committed Jul 17, 2019
1 parent b59d038 commit 6d11989
Show file tree
Hide file tree
Showing 37 changed files with 673 additions and 26 deletions.
9 changes: 9 additions & 0 deletions .changeset/afraid-planets-carry/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"releases": [
{ "name": "@changesets/git", "type": "minor" },
{ "name": "@changesets/parse", "type": "minor" },
{ "name": "@changesets/read", "type": "minor" },
{ "name": "@changesets/types", "type": "minor" }
],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/afraid-planets-carry/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Initial Release
4 changes: 4 additions & 0 deletions .changeset/warm-baboons-occur/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/warm-baboons-occur/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move `git` module to be its own external module
56 changes: 56 additions & 0 deletions V2-PLANS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# V2 PLanning

List of planned changes that will require a breaking change to implement. We are fine having multiple majors if something from this list is ready to ship before the others.

## New changeset format

We are changing the format of changesets to be a single file, and not include

### New packages:

- this package reads from disc or writes to disc

~ this package does not touch disc

C has code for it
T has tests

- ~CT`parse` parse a changeset file contents into a json object `parse(fileContents) => NewChangeset`
- +C`read` read in changesets from `disc read(cwd) => files[].map(parse)`
<!-- - +CT`determineDependents` takes releases, workspaces and dependencies, and returns dependencies that need to be added to the list of dependencies to create a valid ReleasePlan - must be called recursively to 'bottom out' updating dependents
<!-- - +`applyLinks` takes dependents and linked packages, then updates dependents where needed - returns the dependents and if they have been updated --> -->
- ~C`assembleReleasePlan` `assembleReleasePlan(NewChangeset[], Workspace[], DependentsGraph config) => ReleasePlan`, basically takes in the needed info and then repeatedly determines dependents and applies links until neither cause an update
- +C`getReleasePlan` performs `getReleasePlan(cwd) => assembleReleasePlan(all-the-things)` - it's a composition of other packages
- +`applyReleasePlan(releasePlan, cwd)` applies a given release plan to the cwd
- +`apply` `apply(cwd)` assembles release plan, and then applies the release plan (including removing files) (this is just bump, but I keep using the word apply)

Some notes:

I think applyLinks and determineDependents will never be particularly useful on their own - both sort of require you to 'bottom out'
afterwards. It's possible that these should just be functions inside `assembleReleasePlan`, which are tested by themselves, but not
shipped by themselves.

I haven't made a version of `apply` that doesn't touch the disc, as the info you are looking for (new versions of everything) now
exists in the `ReleasePlan` itself in the modified dependents object. Just didn't see much value in it.

Plan is to get all these packages singing nicely together then do a separate PR that implemements them into the CLI.

---

Old functions packagized for consumption outside of `cli` - not really talking about these, trying not
to change how they work for this PR

- getDependentsGraph
- getDependencyGraph
- git
- types

## Rename `bump` to `apply`

This ended up being wrong - we're trying to eliminate `bump` from our code entirely. It's not a useful term.

## Redo config options + packagize changelog formats

Condense them, and make them a JSON only format, that can reference modules

NB: While this will be a major version change, I'm happy to push this one out to a v3. I am not worried about burning major versions.
7 changes: 7 additions & 0 deletions fixtures/broken-changeset/.changeset/broken-changeset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

"cool-package": minor

--

Everything is wrong
5 changes: 5 additions & 0 deletions fixtures/easy-read/.changeset/basic-changeset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cool-package": minor
---

Nice simple summary
5 changes: 5 additions & 0 deletions fixtures/multiple-changesets/.changeset/first-changeset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cool-package": minor
---

Nice simple summary
5 changes: 5 additions & 0 deletions fixtures/multiple-changesets/.changeset/second-changeset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"best-package": patch
---

I'm amazed we needed to update the best package, because it was already the best
Empty file.
Empty file.
1 change: 1 addition & 0 deletions fixtures/with-readme/.changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NOPE
5 changes: 5 additions & 0 deletions fixtures/with-readme/.changeset/basic-changeset.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cool-package": minor
---

Nice simple summary
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
"@babel/preset-flow": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
"@babel/runtime": "^7.4.4",
"@types/child-process-promise": "^2.2.1",
"@types/fs-extra": "^5.1.0",
"@types/jest": "^24.0.12",
"@types/js-yaml": "^3.12.1",
"@types/meow": "^5.0.0",
"@types/semver": "^6.0.0",
"@types/uuid": "^3.4.4",
"@typescript-eslint/eslint-plugin": "^1.9.0",
"@typescript-eslint/parser": "^1.9.0",
"boxen": "^1.3.0",
"chalk": "^2.1.0",
"child-process-promise": "^2.2.1",
"cli-table": "^0.3.1",
"codecov": "^3.5.0",
"dataloader": "^1.4.0",
Expand All @@ -57,6 +60,7 @@
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-standard": "^4.0.0",
"fixturez": "^1.1.0",
"fs-extra": "^7.0.1",
"fuzzy": "^0.1.3",
"globby": "^9.2.0",
Expand All @@ -67,6 +71,7 @@
"jest": "^24.7.1",
"jest-fixtures": "^0.5.0",
"jest-junit": "^6.4.0",
"js-yaml": "^3.13.1",
"lodash.startcase": "^4.4.0",
"meow": "^5.0.0",
"node-fetch": "^2.5.0",
Expand All @@ -82,9 +87,7 @@
"term-size": "^2.1.0",
"tty-table": "^2.7.0",
"typescript": "^3.4.5",
"uuid": "^3.3.2",
"@types/child-process-promise": "^2.2.1",
"child-process-promise": "^2.2.1"
"uuid": "^3.3.2"
},
"preconstruct": {
"packages": [
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@babel/runtime": "^7.4.4",
"@types/semver": "^6.0.0",
"@types/uuid": "^3.4.4",
"@changesets/git": "^0.0.0",
"boxen": "^1.3.0",
"chalk": "^2.1.0",
"cli-table": "^0.3.1",
Expand Down Expand Up @@ -47,4 +48,4 @@
"jest-fixtures": "^0.5.0",
"strip-ansi": "^5.2.0"
}
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/commands/add/__tests__/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { copyFixtureIntoTempDir } from "jest-fixtures";
import stripAnsi from "strip-ansi";
import { askCheckboxPlus, askConfirm, askQuestion } from "../../../utils/cli";
import * as git from "../../../utils/git";
import * as git from "@changesets/git";

import addChangeset from "..";
import writeChangeset from "../writeChangeset";

jest.mock("../../../utils/logger");
jest.mock("../../../utils/cli");
jest.mock("../../../utils/git");
jest.mock("@changesets/git");
jest.mock("../writeChangeset");
writeChangeset.mockImplementation(() => Promise.resolve("abcdefg"));
git.commit.mockImplementation(() => Promise.resolve(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { copyFixtureIntoTempDir } from "jest-fixtures";
import stripAnsi from "strip-ansi";
import { askCheckboxPlus, askConfirm, askQuestion } from "../../../utils/cli";
import * as git from "../../../utils/git";
import * as git from "@changesets/git";
import addChangeset from "..";
import writeChangeset from "../writeChangeset";

Expand All @@ -16,7 +16,7 @@ import writeChangeset from "../writeChangeset";

jest.mock("../../../utils/logger");
jest.mock("../../../utils/cli");
jest.mock("../../../utils/git");
jest.mock("@changesets/git");
jest.mock("../../../commands/add/writeChangeset");

// This is some sad flow hackery
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/add/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "path";
import fs from "fs-extra";

import * as cli from "../../utils/cli";
import * as git from "../../utils/git";
import * as git from "@changesets/git";
import logger from "../../utils/logger";

import writeChangeset from "./writeChangeset";
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/bump/__tests__/consume.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { copyFixtureIntoTempDir } from "jest-fixtures";
import fs from "fs-extra";
import path from "path";
import versionCommand from "../index";
import * as git from "../../../utils/git";
import * as git from "@changesets/git";
import logger from "../../../utils/logger";
import writeChangeset from "../../add/writeChangeset";

Expand All @@ -12,7 +12,7 @@ import writeChangeset from "../../add/writeChangeset";
const consoleError = console.error;

jest.mock("../../../utils/cli");
jest.mock("../../../utils/git");
jest.mock("@changesets/git");
jest.mock("../../../utils/logger");

git.add.mockImplementation(() => Promise.resolve(true));
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/bump/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import semver from "semver";
import * as bolt from "../../utils/bolt-replacements";

import logger from "../../utils/logger";
import * as git from "../../utils/git";
import * as git from "@changesets/git";
import createRelease from "../../utils/createRelease";
import createReleaseCommit from "./createReleaseCommit";
import { removeFolders, removeEmptyFolders } from "../../utils/removeFolders";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { copyFixtureIntoTempDir } from "jest-fixtures";

import publishPackages from "../publishPackages";
import * as git from "../../../utils/git";
import * as git from "@changesets/git";
import runRelease from "..";

jest.mock("../../../utils/cli");
jest.mock("../../../utils/git");
jest.mock("@changesets/git");
jest.mock("../../../utils/logger");
jest.mock("../publishPackages");

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/release/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import publishPackages from "./publishPackages";
import logger from "../../utils/logger";
import * as git from "../../utils/git";
import * as git from "@changesets/git";
import resolveUserConfig from "../../utils/resolveConfig";
import { defaultConfig } from "../../utils/constants";
import { ExitError } from "../../utils/errors";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/status/__tests__/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const simpleReleaseObj = {
};

jest.mock("../../../utils/logger");
jest.mock("../../../utils/git");
jest.mock("@changesets/git");

const writeChangesets = (commits, cwd) => {
return Promise.all(commits.map(commit => writeChangeset(commit, { cwd })));
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils/getChangesets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs-extra";
import path from "path";

import * as git from "./git";
import * as git from "@changesets/git";
import { Changeset } from "./types";

// TODO take in cwd, and fetch changesetBase ourselves
Expand Down
17 changes: 17 additions & 0 deletions packages/git/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@changesets/git",
"version": "0.0.0",
"description": "Some git helpers that changesets use to get information",
"main": "dist/git.cjs.js",
"module": "dist/git.esm.js",
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/master/packages/git",
"dependencies": {
"projector-spawn": "^1.0.1",
"get-workspaces": "^0.4.0",
"pkg-dir": "^4.1.0"
},
"devDependencies": {
"jest-fixtures": "^0.5.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
tag,
getChangedPackagesSinceMaster,
getChangedChangesetFilesSinceMaster
} from "../git";
} from "./";

describe("git", () => {
let cwd;
let cwd: string;
beforeEach(async () => {
cwd = await copyFixtureIntoTempDir(__dirname, "with-git");
await spawn("git", ["init"], { cwd });
Expand Down Expand Up @@ -208,7 +208,9 @@ describe("git", () => {
const changedPackages = await getChangedPackagesSinceMaster(cwd);

expect(changedPackages).toHaveLength(2);
// @ts-ignore
expect(changedPackages[0].name).toEqual("pkg-a");
// @ts-ignore
expect(changedPackages[1].name).toEqual("pkg-b");
});
});
Expand Down
28 changes: 22 additions & 6 deletions packages/cli/src/utils/git.ts → packages/git/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import spawn from "projector-spawn";
import path from "path";
import { getProjectDirectory } from "./getProjectDirectory";
// @ts-ignore
import * as bolt from "./bolt-replacements";
import getWorkspaces from "get-workspaces";
import pkgDir from "pkg-dir";

// TODO: Currently getting this working, need to decide how to
// share projectDir between packages if that's what we need
async function getProjectDirectory(cwd: string) {
const projectDir = await pkgDir(cwd);
if (!projectDir) {
throw new Error("Could not find project directory");
}
return projectDir;
}

async function getMasterRef(cwd: string) {
const gitCmd = await spawn("git", ["rev-parse", "master"], { cwd });
Expand Down Expand Up @@ -46,7 +55,7 @@ async function getChangedFilesSince(
ref: string,
cwd: string,
fullPath = false
) {
): Promise<Array<string>> {
// First we need to find the commit where we diverged from `ref` at using `git merge-base`
let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
const divergedAt = cmd.stdout.trim();
Expand All @@ -61,7 +70,7 @@ async function getChangedFilesSince(
async function getChangedChangesetFilesSinceMaster(
cwd: string,
fullPath = false
) {
): Promise<Array<string>> {
const ref = await getMasterRef(cwd);
// First we need to find the commit where we diverged from `ref` at using `git merge-base`
let cmd = await spawn("git", ["merge-base", ref, "HEAD"], { cwd });
Expand All @@ -83,7 +92,14 @@ async function getChangedChangesetFilesSinceMaster(
async function getChangedPackagesSinceCommit(commitHash: string, cwd: string) {
const changedFiles = await getChangedFilesSince(commitHash, cwd, true);
const projectDir = await getProjectDirectory(cwd);
const workspaces = await bolt.getWorkspaces({ cwd });
let workspaces = await getWorkspaces({
cwd,
tools: ["yarn", "bolt", "root"]
});
if (workspaces === null) {
workspaces = [];
}

const allPackages = workspaces.map(pkg => ({
...pkg,
relativeDir: path.relative(projectDir, pkg.dir)
Expand Down
17 changes: 17 additions & 0 deletions packages/parse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@changesets/parse",
"version": "0.0.0",
"description": "Parse a changeset file's contents into a usable json object",
"main": "dist/parse.cjs.js",
"module": "dist/parse.esm.js",
"license": "MIT",
"repository": "https://github.com/changesets/changesets/tree/master/packages/read",
"dependencies": {
"js-yaml": "^3.13.1",
"@changesets/types": "^0.0.0"
},
"devDependencies": {
"jest-fixtures": "^0.5.0",
"outdent": "^0.5.0"
}
}
Loading

0 comments on commit 6d11989

Please sign in to comment.