Skip to content

Commit

Permalink
'initial commit'
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Sep 16, 2019
1 parent 8db2bfe commit 2a6d43c
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"presets": ["@babel/preset-env", "@babel/preset-typescript"]
"presets": [
["@babel/preset-env", { "targets": { "node": 10 } }],
"@babel/preset-typescript"
]
}
14 changes: 8 additions & 6 deletions dist/index.js

Large diffs are not rendered by default.

63 changes: 19 additions & 44 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,8 @@ import * as github from "@actions/github";
import fs from "fs-extra";
import getWorkspaces, { Workspace } from "get-workspaces";
import path from "path";
import { getChangelogEntry } from "./utils";

async function execWithOutput(
command: string,
args?: string[],
options?: { ignoreReturnCode?: boolean }
) {
let myOutput = "";
let myError = "";

return {
code: await exec(command, args, {
listeners: {
stdout: (data: Buffer) => {
myOutput += data.toString();
},
stderr: (data: Buffer) => {
myError += data.toString();
}
},

...options
}),
stdout: myOutput,
stderr: myError
};
}
import { getChangelogEntry, execWithOutput, getChangedPackages } from "./utils";
import * as semver from "semver";

(async () => {
let githubToken = process.env.GITHUB_TOKEN;
Expand Down Expand Up @@ -209,24 +184,23 @@ async function execWithOutput(
}
if (shouldBump) {
await exec("git", ["reset", "--hard", github.context.sha]);
let changesetsCliPkgJson = await fs.readJson(
require.resolve("@changesets/cli/package.json")
);
let cmd = semver.lt(changesetsCliPkgJson.version, "2.0.0")
? "bump"
: "version";
let output = await execWithOutput("node", [
"./node_modules/.bin/changeset",
"bump"
cmd
]);
let searchQuery = `repo:${repo}+state:open+head:changeset-release+base:${defaultBranch}`;
let searchResultPromise = octokit.search.issuesAndPullRequests({
q: searchQuery
});
let changedWorkspaces = await getChangedPackages(process.cwd());

let prBodyPromise = (async () => {
let updateChangelogRegex = /Updated file (.+)/;
let changelogFilenames = [];
for (let line of output.stdout.split("\n")) {
let match = line.match(updateChangelogRegex);
if (match === null) {
continue;
}
changelogFilenames.push(match[1]);
}
return (
`This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and ${
publishScript
Expand All @@ -237,24 +211,25 @@ async function execWithOutput(
# Releases
` +
(await Promise.all(
changelogFilenames.map(async filename => {
let [pkgJsonContent, changelogContents] = await Promise.all([
fs.readJson(path.join(path.dirname(filename), "package.json")),
fs.readFile(filename, "utf8")
]);
changedWorkspaces.map(async workspace => {
let changelogContents = await fs.readFile(
path.join(workspace.dir, "CHANGELOG.md"),
"utf8"
);

let entry = getChangelogEntry(
changelogContents,
pkgJsonContent.version
workspace.config.version
);
return {
highestLevel: entry.highestLevel,
content:
`## ${pkgJsonContent.name}@${pkgJsonContent.version}\n\n` +
`## ${workspace.name}@${workspace.config.version}\n\n` +
entry.content
};
})
))
.filter(x => x)
.sort((a, b) => b.highestLevel - a.highestLevel)
.map(x => x.content)
.join("\n ")
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/fs-extra": "^8.0.0",
"@types/jest": "^24.0.18",
"@types/node": "^12.7.1",
"@types/semver": "^6.0.2",
"babel-jest": "^24.9.0",
"fs-extra": "^8.1.0",
"get-workspaces": "^0.4.0",
Expand All @@ -30,6 +31,8 @@
"mdast-util-to-string": "^1.0.6",
"remark-parse": "^7.0.1",
"remark-stringify": "^7.0.3",
"semver": "^6.3.0",
"tempy": "^0.3.0",
"unified": "^8.3.2"
},
"husky": {
Expand Down
28 changes: 27 additions & 1 deletion utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getChangelogEntry, BumpLevels } from "./utils";
import { getChangelogEntry, BumpLevels, getChangedPackages } from "./utils";
import tempy from "tempy";
import { exec } from "@actions/exec";
import * as fs from "fs-extra";
import path from "path";

let changelog = `# @keystone-alpha/email
Expand Down Expand Up @@ -78,3 +82,25 @@ test("it works", () => {
expect(entry.content).toMatchSnapshot();
expect(entry.highestLevel).toBe(BumpLevels.patch);
});

test("getChangedPackages works", async () => {
let dir = tempy.directory();
await exec("git", ["init"], { cwd: dir });
await fs.writeJson(path.join(dir, "package.json"), {
name: "some-package",
version: "1.0.0"
});
await exec("git", ["add", "."]);
await exec("git", ["commit", "-m", "'initial commit'"]);
let emptyWorkspaces = await getChangedPackages(dir);
expect(emptyWorkspaces).toEqual([]);
let workspace = {
name: "some-package",
version: "2.0.0"
};
await fs.writeJson(path.join(dir, "package.json"), workspace);
let workspaces = await getChangedPackages(dir);
expect(workspaces).toEqual([
{ name: "some-package", dir, config: workspace }
]);
});
57 changes: 57 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
// @ts-ignore
import mdastToString from "mdast-util-to-string";
import { exec } from "@actions/exec";
import * as core from "@actions/core";
import getWorkspaces, { Workspace } from "get-workspaces";
import path from "path";

export const BumpLevels = {
dep: 0,
Expand All @@ -11,6 +15,33 @@ export const BumpLevels = {
major: 3
} as const;

export async function getChangedPackages(cwd: string) {
let workspaces = await getWorkspaces({
cwd,
tools: ["bolt", "yarn", "root"]
});

if (!workspaces) {
core.setFailed("Could not find workspaces");
return process.exit(1);
}

let workspacesByDirectory = new Map(workspaces.map(x => [x.dir, x]));

let output = await execWithOutput("git", ["diff", "--only-names"], { cwd });
let names = output.stdout.split("\n");
let changedWorkspaces: Workspace[] = [];
for (let name of names) {
let dirname = path.dirname(name);
let workspace = workspacesByDirectory.get(dirname);
if (workspace !== undefined) {
changedWorkspaces.push(workspace);
}
}

return changedWorkspaces;
}

export function getChangelogEntry(changelog: string, version: string) {
let ast = unified()
.use(remarkParse)
Expand Down Expand Up @@ -66,3 +97,29 @@ export function getChangelogEntry(changelog: string, version: string) {
highestLevel: highestLevel
};
}

export async function execWithOutput(
command: string,
args?: string[],
options?: { ignoreReturnCode?: boolean; cwd?: string }
) {
let myOutput = "";
let myError = "";

return {
code: await exec(command, args, {
listeners: {
stdout: (data: Buffer) => {
myOutput += data.toString();
},
stderr: (data: Buffer) => {
myError += data.toString();
}
},

...options
}),
stdout: myOutput,
stderr: myError
};
}
36 changes: 36 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,11 @@
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8"
integrity sha512-ce5d3q03Ex0sy4R14722Rmt6MT07Ua+k4FwDfdcToYJcMKNtRVQvJ6JCAPdAmAnbRb6CsX6aYb9m96NGod9uTw==

"@types/semver@^6.0.2":
version "6.0.2"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.0.2.tgz#5e8b09f0e4af53034b1d0fb9977a277847836205"
integrity sha512-G1Ggy7/9Nsa1Jt2yiBR2riEuyK2DFNnqow6R7cromXPMNynackRY1vqFTLz/gwnef1LHokbXThcPhqMRjUbkpQ==

"@types/stack-utils@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
Expand Down Expand Up @@ -2345,6 +2350,11 @@ crypto-browserify@^3.11.0:
randombytes "^2.0.0"
randomfill "^1.0.3"

crypto-random-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=

[email protected], css-color-names@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.4.tgz#808adc2e79cf84738069b646cb20ec27beb629e0"
Expand Down Expand Up @@ -6989,6 +6999,20 @@ tar@^4:
safe-buffer "^5.1.2"
yallist "^3.0.3"

temp-dir@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-1.0.0.tgz#0a7c0ea26d3a39afa7e0ebea9c1fc0bc4daa011d"
integrity sha1-CnwOom06Oa+n4OvqnB/AvE2qAR0=

tempy@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/tempy/-/tempy-0.3.0.tgz#6f6c5b295695a16130996ad5ab01a8bd726e8bf8"
integrity sha512-WrH/pui8YCwmeiAoxV+lpRH9HpRtgBhSR2ViBPgpGb/wnYDzp21R4MN45fsCGvLROvY67o3byhJRYRONJyImVQ==
dependencies:
temp-dir "^1.0.0"
type-fest "^0.3.1"
unique-string "^1.0.0"

terser@^3.7.3:
version "3.17.0"
resolved "https://registry.yarnpkg.com/terser/-/terser-3.17.0.tgz#f88ffbeda0deb5637f9d24b0da66f4e15ab10cb2"
Expand Down Expand Up @@ -7178,6 +7202,11 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

type-fest@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.3.1.tgz#63d00d204e059474fe5e1b7c011112bbd1dc29e1"
integrity sha512-cUGJnCdr4STbePCgqNFbpVNCepa+kAVohJs1sLhxzdH+gnEoOd8VhbYa7pD3zZYGiURWM2xzEII3fQcRizDkYQ==

type-fest@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b"
Expand Down Expand Up @@ -7299,6 +7328,13 @@ uniqs@^2.0.0:
resolved "https://registry.yarnpkg.com/uniqs/-/uniqs-2.0.0.tgz#ffede4b36b25290696e6e165d4a59edb998e6b02"
integrity sha1-/+3ks2slKQaW5uFl1KWe25mOawI=

unique-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a"
integrity sha1-nhBXzKhRq7kzmPizOuGHuZyuwRo=
dependencies:
crypto-random-string "^1.0.0"

unist-util-is@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-3.0.0.tgz#d9e84381c2468e82629e4a5be9d7d05a2dd324cd"
Expand Down

0 comments on commit 2a6d43c

Please sign in to comment.