Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
build(dev-utils): release script will now automatically create github…
… release
  • Loading branch information
mlaursen committed Feb 18, 2021
1 parent db1aa30 commit 83c2b65
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -16,6 +16,9 @@ packages/*/*.tsbuildinfo
/packages/material-icons/temp
!/packages/documentation/types

# for creating github releases
/.env.local

# Testing
coverage

Expand Down
2 changes: 2 additions & 0 deletions packages/dev-utils/package.json
Expand Up @@ -16,10 +16,12 @@
},
"dependencies": {
"@babel/core": "^7.12.13",
"@octokit/core": "^3.2.5",
"chokidar": "^3.5.0",
"commander": "^7.0.0",
"cpx": "^1.5.0",
"cssnano": "^4.1.10",
"dotenv": "^8.2.0",
"filesize": "^6.1.0",
"fs-extra": "^9.1.0",
"glob": "^7.1.6",
Expand Down
11 changes: 10 additions & 1 deletion packages/dev-utils/src/cli.ts
Expand Up @@ -165,7 +165,16 @@ createCommand("libsize")
themes = true,
forceThemes = false,
stageChanges = false,
}) => libsize({ umd, forceUmd, themes, forceThemes, stageChanges })
}) =>
libsize({
umd,
forceUmd,
themes,
forceThemes,
stageChanges,
}).then(() => {
// .action only allows Promise<void> or void
})
);

createCommand("themes")
Expand Down
19 changes: 15 additions & 4 deletions packages/dev-utils/src/libsize.ts
Expand Up @@ -126,7 +126,11 @@ function getSizes(message: string): readonly Size[] {
});
}

function logPercentChanged(message: string, umd: boolean, css: boolean): void {
function getPercentChanged(
message: string,
umd: boolean,
css: boolean
): string {
const previous = getSizes(getPreviousLibsize(ROOT_README_PATH));
const current = getSizes(message);
const start = umd ? 0 : 3;
Expand Down Expand Up @@ -161,7 +165,7 @@ function logPercentChanged(message: string, umd: boolean, css: boolean): void {
})
.join("\n");

log.info(updated);
return updated;
}

function updateLibsize(filePath: string, message: string): void {
Expand Down Expand Up @@ -233,7 +237,7 @@ export async function libsize({
themes = true,
forceThemes = false,
stageChanges = false,
}: Options): Promise<void> {
}: Options): Promise<string> {
umd = umd || forceUmd;
themes = themes || forceThemes;
if (!umd && !themes) {
Expand Down Expand Up @@ -267,7 +271,12 @@ ${list(css)}
`;
}

logPercentChanged(message, !!umds.length, !!css.length);
const percentChanged = getPercentChanged(
message,
!!umds.length,
!!css.length
);
log.info(percentChanged);

if (stageChanges && umds.length && css.length) {
log.info("Updating documentation files with libsize...");
Expand All @@ -280,4 +289,6 @@ ${list(css)}
git("add -u");
}
}

return percentChanged;
}
49 changes: 45 additions & 4 deletions packages/dev-utils/src/release.ts
@@ -1,12 +1,16 @@
import { Octokit } from "@octokit/core";
import dotenv from "dotenv";
import log from "loglevel";
import { join } from "path";
import prompts from "prompts";

import { changelogData } from "./changelogData";
import { clean } from "./clean";
import { projectRoot } from "./constants";
import { libsize } from "./libsize";
import { changelogData } from "./changelogData";
import { getLernaVersion, git, replaceTag, run } from "./utils";
import { variables } from "./variables";
import { initBlog } from "./utils/initBlog";
import { variables } from "./variables";

export type ReleaseType =
| "major"
Expand Down Expand Up @@ -74,6 +78,21 @@ export async function release({
clean: enableClean,
type,
}: Options): Promise<void> {
const localDotEnv = join(projectRoot, ".env.local");
dotenv.config({ path: localDotEnv });
const { GITHUB_TOKEN } = process.env;
if (!GITHUB_TOKEN) {
log.error(
`Missing a \`GITHUB_TOKEN\` environment variable. This should be located at:
- ${localDotEnv}
A token can be created at:
- https://github.com/settings/tokens/new?scopes=repo
`
);

process.exit(1);
}
const yes = autoYes ? " --yes" : "";

if (enableClean) {
Expand All @@ -91,9 +110,9 @@ export async function release({

await changelogData();
run(`npx lerna version ${type} --no-push${yes}`);
await initBlog();
const changelog = await initBlog();

await libsize({
const percentChanged = await libsize({
umd: true,
themes: true,
// have to force the themes to be updated since they are always stored in
Expand Down Expand Up @@ -122,4 +141,26 @@ export async function release({

git("push origin main");
git("push --tags");

log.info("Creating github release");
const body = `${changelog}
### Library Size Changes
${percentChanged}
`;
const version = await getLernaVersion();
const octokit = new Octokit({ auth: GITHUB_TOKEN });
const response = await octokit.request(
"POST /repos/{owner}/{repo}/releases",
{
owner: "mlaursen",
repo: "react-md",
tag_name: `v${version}`,
body,
prerelease: type.startsWith("pre"),
}
);

log.info(`Created release: ${response.data.html_url}`);
}
16 changes: 11 additions & 5 deletions packages/dev-utils/src/utils/initBlog.ts
Expand Up @@ -7,7 +7,12 @@ import { format } from "./format";

const NEW_ENTRY = /^#{1,2}\s+\[\d/;

export async function initBlog(): Promise<void> {
/**
*
* @return the current release markdown that can be used to generate a github
* release for the current tag.
*/
export async function initBlog(): Promise<string> {
const blogPath = join(documentationRoot, src, "blogs", "index.md");
const version = (await getPackageJson("react-md")).version;
const changelog = await readFile(join(projectRoot, "CHANGELOG.md"), "utf8");
Expand All @@ -31,9 +36,8 @@ export async function initBlog(): Promise<void> {
process.exit(1);
}

const currentRelease = lines
.slice(lastEntryStart, nextEntryStart)
.join("\n")
const currentRelease = lines.slice(lastEntryStart, nextEntryStart).join("\n");
const blogMarkdown = currentRelease
// create smaller headings and remove margin
.replace(/^(###)\s+(.+)$/gm, "##### $2<!-- no-margin -->")
// replace issue/pr references with super-shorthand syntax. they can
Expand All @@ -52,12 +56,14 @@ Date: ${new Date().toLocaleDateString("en-US", {
Summary:
${currentRelease}
${blogMarkdown}
---
${blog}
`;

await writeFile(blogPath, format(contents, "markdown"));

return currentRelease;
}
2 changes: 1 addition & 1 deletion packages/dev-utils/src/utils/packages.ts
Expand Up @@ -83,7 +83,7 @@ export async function getAllVersions(): Promise<typeof VERSIONS> {
/**
* Gets the current root lerna version.
*/
export async function getLernaVersion(): Promise<string | null> {
export async function getLernaVersion(): Promise<string> {
const lernaJsonPath = join(projectRoot, "lerna.json");
if (VERSIONS.has(lernaJsonPath)) {
const value = VERSIONS.get(lernaJsonPath);
Expand Down
85 changes: 84 additions & 1 deletion yarn.lock
Expand Up @@ -1511,6 +1511,25 @@
dependencies:
"@octokit/types" "^2.0.0"

"@octokit/auth-token@^2.4.4":
version "2.4.5"
resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-2.4.5.tgz#568ccfb8cb46f36441fac094ce34f7a875b197f3"
integrity sha512-BpGYsPgJt05M7/L/5FoE1PiAbdxXFZkX/3kDYcsvd1v6UhlnE5e96dTDr0ezX/EFwciQxf3cNV0loipsURU+WA==
dependencies:
"@octokit/types" "^6.0.3"

"@octokit/core@^3.2.5":
version "3.2.5"
resolved "https://registry.yarnpkg.com/@octokit/core/-/core-3.2.5.tgz#57becbd5fd789b0592b915840855f3a5f233d554"
integrity sha512-+DCtPykGnvXKWWQI0E1XD+CCeWSBhB6kwItXqfFmNBlIlhczuDPbg+P6BtLnVBaRJDAjv+1mrUJuRsFSjktopg==
dependencies:
"@octokit/auth-token" "^2.4.4"
"@octokit/graphql" "^4.5.8"
"@octokit/request" "^5.4.12"
"@octokit/types" "^6.0.3"
before-after-hook "^2.1.0"
universal-user-agent "^6.0.0"

"@octokit/endpoint@^6.0.0":
version "6.0.0"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-6.0.0.tgz#4c7acd79ab72df78732a7d63b09be53ec5a2230b"
Expand All @@ -1520,6 +1539,29 @@
is-plain-object "^3.0.0"
universal-user-agent "^5.0.0"

"@octokit/endpoint@^6.0.1":
version "6.0.11"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-6.0.11.tgz#082adc2aebca6dcefa1fb383f5efb3ed081949d1"
integrity sha512-fUIPpx+pZyoLW4GCs3yMnlj2LfoXTWDUVPTC4V3MUEKZm48W+XYpeWSZCv+vYF1ZABUm2CqnDVf1sFtIYrj7KQ==
dependencies:
"@octokit/types" "^6.0.3"
is-plain-object "^5.0.0"
universal-user-agent "^6.0.0"

"@octokit/graphql@^4.5.8":
version "4.6.0"
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-4.6.0.tgz#f9abca55f82183964a33439d5264674c701c3327"
integrity sha512-CJ6n7izLFXLvPZaWzCQDjU/RP+vHiZmWdOunaCS87v+2jxMsW9FB5ktfIxybRBxZjxuJGRnxk7xJecWTVxFUYQ==
dependencies:
"@octokit/request" "^5.3.0"
"@octokit/types" "^6.0.3"
universal-user-agent "^6.0.0"

"@octokit/openapi-types@^5.0.0":
version "5.0.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-5.0.0.tgz#420853cc6d97a12521fa1d105ecc277112b32935"
integrity sha512-QXpwbGjidE+XhgCEeXpffQk/XGiexgne8czTebwU359Eoko8FJzAED4aizrQlL9t4n6tMx/1Ka1vwZbP6rayFA==

"@octokit/plugin-enterprise-rest@^6.0.1":
version "6.0.1"
resolved "https://registry.yarnpkg.com/@octokit/plugin-enterprise-rest/-/plugin-enterprise-rest-6.0.1.tgz#e07896739618dab8da7d4077c658003775f95437"
Expand Down Expand Up @@ -1577,6 +1619,20 @@
once "^1.4.0"
universal-user-agent "^5.0.0"

"@octokit/request@^5.3.0", "@octokit/request@^5.4.12":
version "5.4.14"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-5.4.14.tgz#ec5f96f78333bb2af390afa5ff66f114b063bc96"
integrity sha512-VkmtacOIQp9daSnBmDI92xNIeLuSRDOIuplp/CJomkvzt7M18NXgG044Cx/LFKLgjKt9T2tZR6AtJayba9GTSA==
dependencies:
"@octokit/endpoint" "^6.0.1"
"@octokit/request-error" "^2.0.0"
"@octokit/types" "^6.7.1"
deprecation "^2.0.0"
is-plain-object "^5.0.0"
node-fetch "^2.6.1"
once "^1.4.0"
universal-user-agent "^6.0.0"

"@octokit/rest@^16.28.4":
version "16.43.1"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.43.1.tgz#3b11e7d1b1ac2bbeeb23b08a17df0b20947eda6b"
Expand Down Expand Up @@ -1606,6 +1662,13 @@
dependencies:
"@types/node" ">= 8"

"@octokit/types@^6.0.3", "@octokit/types@^6.7.1":
version "6.9.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-6.9.0.tgz#08505bf87385772032e7d3b08487944f87ee6c52"
integrity sha512-j4ms2ukvWciu8hSuIWtWK/LyOWMZ0ZsRcvPIVLBYyAkTKBKrMJyiyv2wawJnyphKyEOhRgIyu5Nmf4yPxp0tcg==
dependencies:
"@octokit/openapi-types" "^5.0.0"

"@opentelemetry/api@0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-0.14.0.tgz#4e17d8d2f1da72b19374efa7b6526aa001267cae"
Expand Down Expand Up @@ -3473,6 +3536,11 @@ before-after-hook@^2.0.0:
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.1.0.tgz#b6c03487f44e24200dd30ca5e6a1979c5d2fb635"
integrity sha512-IWIbu7pMqyw3EAJHzzHbWa85b6oud/yfKYg5rqB5hNE8CeMi3nX+2C2sj0HswfblST86hpVEOAb9x34NZd6P7A==

before-after-hook@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.1.1.tgz#99ae36992b5cfab4a83f6bee74ab27835f28f405"
integrity sha512-5ekuQOvO04MDj7kYZJaMab2S8SPjGJbotVNyv7QYFCOAwrGZs/YnoDNlh1U+m5hl7H2D/+n0taaAV/tfyd3KMA==

big.js@^5.2.2:
version "5.2.2"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-5.2.2.tgz#65f0af382f578bcdc742bd9c281e9cb2d7768328"
Expand Down Expand Up @@ -5826,6 +5894,11 @@ dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

download@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/download/-/download-8.0.0.tgz#afc0b309730811731aae9f5371c9f46be73e51b1"
Expand Down Expand Up @@ -8756,6 +8829,11 @@ is-plain-object@^3.0.0:
dependencies:
isobject "^4.0.0"

is-plain-object@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/is-plain-object/-/is-plain-object-5.0.0.tgz#4427f50ab3429e9025ea7d52e9043a9ef4159344"
integrity sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==

is-posix-bracket@^0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/is-posix-bracket/-/is-posix-bracket-0.1.1.tgz#3334dc79774368e92f016e6fbc0a88f5cd6e6bc4"
Expand Down Expand Up @@ -10981,7 +11059,7 @@ node-fetch-npm@^2.0.2:
json-parse-better-errors "^1.0.0"
safe-buffer "^5.1.1"

node-fetch@2.6.1, node-fetch@^2.3.0, node-fetch@^2.5.0:
node-fetch@2.6.1, node-fetch@^2.3.0, node-fetch@^2.5.0, node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
Expand Down Expand Up @@ -15869,6 +15947,11 @@ universal-user-agent@^5.0.0:
dependencies:
os-name "^3.1.0"

universal-user-agent@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.0.tgz#3381f8503b251c0d9cd21bc1de939ec9df5480ee"
integrity sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==

universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
Expand Down

0 comments on commit 83c2b65

Please sign in to comment.