'initial commit'

This commit is contained in:
mitchellhamilton
2019-09-16 17:18:53 +10:00
parent 8db2bfe48d
commit 2a6d43cef5
7 changed files with 154 additions and 52 deletions
+4 -1
View File
@@ -1,3 +1,6 @@
{ {
"presets": ["@babel/preset-env", "@babel/preset-typescript"] "presets": [
["@babel/preset-env", { "targets": { "node": 10 } }],
"@babel/preset-typescript"
]
} }
+8 -6
View File
File diff suppressed because one or more lines are too long
+19 -44
View File
@@ -4,33 +4,8 @@ import * as github from "@actions/github";
import fs from "fs-extra"; import fs from "fs-extra";
import getWorkspaces, { Workspace } from "get-workspaces"; import getWorkspaces, { Workspace } from "get-workspaces";
import path from "path"; import path from "path";
import { getChangelogEntry } from "./utils"; import { getChangelogEntry, execWithOutput, getChangedPackages } from "./utils";
import * as semver from "semver";
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
};
}
(async () => { (async () => {
let githubToken = process.env.GITHUB_TOKEN; let githubToken = process.env.GITHUB_TOKEN;
@@ -209,24 +184,23 @@ async function execWithOutput(
} }
if (shouldBump) { if (shouldBump) {
await exec("git", ["reset", "--hard", github.context.sha]); 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", [ let output = await execWithOutput("node", [
"./node_modules/.bin/changeset", "./node_modules/.bin/changeset",
"bump" cmd
]); ]);
let searchQuery = `repo:${repo}+state:open+head:changeset-release+base:${defaultBranch}`; let searchQuery = `repo:${repo}+state:open+head:changeset-release+base:${defaultBranch}`;
let searchResultPromise = octokit.search.issuesAndPullRequests({ let searchResultPromise = octokit.search.issuesAndPullRequests({
q: searchQuery q: searchQuery
}); });
let changedWorkspaces = await getChangedPackages(process.cwd());
let prBodyPromise = (async () => { 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 ( return (
`This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and ${ `This PR was opened by the Changesets release GitHub action. When you're ready to do a release, you can merge this and ${
publishScript publishScript
@@ -237,24 +211,25 @@ async function execWithOutput(
# Releases # Releases
` + ` +
(await Promise.all( (await Promise.all(
changelogFilenames.map(async filename => { changedWorkspaces.map(async workspace => {
let [pkgJsonContent, changelogContents] = await Promise.all([ let changelogContents = await fs.readFile(
fs.readJson(path.join(path.dirname(filename), "package.json")), path.join(workspace.dir, "CHANGELOG.md"),
fs.readFile(filename, "utf8") "utf8"
]); );
let entry = getChangelogEntry( let entry = getChangelogEntry(
changelogContents, changelogContents,
pkgJsonContent.version workspace.config.version
); );
return { return {
highestLevel: entry.highestLevel, highestLevel: entry.highestLevel,
content: content:
`## ${pkgJsonContent.name}@${pkgJsonContent.version}\n\n` + `## ${workspace.name}@${workspace.config.version}\n\n` +
entry.content entry.content
}; };
}) })
)) ))
.filter(x => x)
.sort((a, b) => b.highestLevel - a.highestLevel) .sort((a, b) => b.highestLevel - a.highestLevel)
.map(x => x.content) .map(x => x.content)
.join("\n ") .join("\n ")
+3
View File
@@ -22,6 +22,7 @@
"@types/fs-extra": "^8.0.0", "@types/fs-extra": "^8.0.0",
"@types/jest": "^24.0.18", "@types/jest": "^24.0.18",
"@types/node": "^12.7.1", "@types/node": "^12.7.1",
"@types/semver": "^6.0.2",
"babel-jest": "^24.9.0", "babel-jest": "^24.9.0",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"get-workspaces": "^0.4.0", "get-workspaces": "^0.4.0",
@@ -30,6 +31,8 @@
"mdast-util-to-string": "^1.0.6", "mdast-util-to-string": "^1.0.6",
"remark-parse": "^7.0.1", "remark-parse": "^7.0.1",
"remark-stringify": "^7.0.3", "remark-stringify": "^7.0.3",
"semver": "^6.3.0",
"tempy": "^0.3.0",
"unified": "^8.3.2" "unified": "^8.3.2"
}, },
"husky": { "husky": {
+27 -1
View File
@@ -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 let changelog = `# @keystone-alpha/email
@@ -78,3 +82,25 @@ test("it works", () => {
expect(entry.content).toMatchSnapshot(); expect(entry.content).toMatchSnapshot();
expect(entry.highestLevel).toBe(BumpLevels.patch); 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
View File
@@ -3,6 +3,10 @@ import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify"; import remarkStringify from "remark-stringify";
// @ts-ignore // @ts-ignore
import mdastToString from "mdast-util-to-string"; 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 = { export const BumpLevels = {
dep: 0, dep: 0,
@@ -11,6 +15,33 @@ export const BumpLevels = {
major: 3 major: 3
} as const; } 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) { export function getChangelogEntry(changelog: string, version: string) {
let ast = unified() let ast = unified()
.use(remarkParse) .use(remarkParse)
@@ -66,3 +97,29 @@ export function getChangelogEntry(changelog: string, version: string) {
highestLevel: highestLevel 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
View File
@@ -1358,6 +1358,11 @@
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8" resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.2.tgz#690a1475b84f2a884fd07cd797c00f5f31356ea8"
integrity sha512-ce5d3q03Ex0sy4R14722Rmt6MT07Ua+k4FwDfdcToYJcMKNtRVQvJ6JCAPdAmAnbRb6CsX6aYb9m96NGod9uTw== 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": "@types/stack-utils@^1.0.1":
version "1.0.1" version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
@@ -2345,6 +2350,11 @@ crypto-browserify@^3.11.0:
randombytes "^2.0.0" randombytes "^2.0.0"
randomfill "^1.0.3" 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=
css-color-names@0.0.4, css-color-names@^0.0.4: css-color-names@0.0.4, css-color-names@^0.0.4:
version "0.0.4" version "0.0.4"
resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.4.tgz#808adc2e79cf84738069b646cb20ec27beb629e0" resolved "https://registry.yarnpkg.com/css-color-names/-/css-color-names-0.0.4.tgz#808adc2e79cf84738069b646cb20ec27beb629e0"
@@ -6989,6 +6999,20 @@ tar@^4:
safe-buffer "^5.1.2" safe-buffer "^5.1.2"
yallist "^3.0.3" 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: terser@^3.7.3:
version "3.17.0" version "3.17.0"
resolved "https://registry.yarnpkg.com/terser/-/terser-3.17.0.tgz#f88ffbeda0deb5637f9d24b0da66f4e15ab10cb2" resolved "https://registry.yarnpkg.com/terser/-/terser-3.17.0.tgz#f88ffbeda0deb5637f9d24b0da66f4e15ab10cb2"
@@ -7178,6 +7202,11 @@ type-check@~0.3.2:
dependencies: dependencies:
prelude-ls "~1.1.2" 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: type-fest@^0.6.0:
version "0.6.0" version "0.6.0"
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b" resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b"
@@ -7299,6 +7328,13 @@ uniqs@^2.0.0:
resolved "https://registry.yarnpkg.com/uniqs/-/uniqs-2.0.0.tgz#ffede4b36b25290696e6e165d4a59edb998e6b02" resolved "https://registry.yarnpkg.com/uniqs/-/uniqs-2.0.0.tgz#ffede4b36b25290696e6e165d4a59edb998e6b02"
integrity sha1-/+3ks2slKQaW5uFl1KWe25mOawI= 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: unist-util-is@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-3.0.0.tgz#d9e84381c2468e82629e4a5be9d7d05a2dd324cd" resolved "https://registry.yarnpkg.com/unist-util-is/-/unist-util-is-3.0.0.tgz#d9e84381c2468e82629e4a5be9d7d05a2dd324cd"