Skip to content

Commit

Permalink
chore(dev-utils): new release script with pre-compiled theme support
Browse files Browse the repository at this point in the history
It looks like jsDelivr supports serving files through github which is a
nice workaround for the pre-compiled themes since including the themes
apparently breaks package managers due to the giant size. There will be
documentation in a following commit, but it'll be setup like:

https://cdn.jsdelivr.net/gh/mlaursen/react-md@VERSION/themes/react-md.{PRIMARY}-{SECONDARY}-{SECONDARY_WEIGHT}-{LIGHT|DARK}.min.css

This also fixes an error with the pre-compiled themes for the error
color due to my "amazing" regex replacement to decrease build time.
Since the error color ended up being the same as the first theme color,
the error colors were incorrect.
  • Loading branch information
mlaursen committed Sep 3, 2020
1 parent def2dfb commit 5886f9f
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ coverage
# Don't want to include the lock files for these examples
# since they can be downloaded and reused
examples/*/*.lock

# the pre-compiled themes should not be added other than for tags
/themes
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
"description": "The mono-repo for react-md",
"main": "index.js",
"scripts": {
"pre-publish": "dev-utils prepublish",
"lerna-version": "npx lerna version --no-push --yes",
"fix-changelogs": "dev-utils fix-changelogs --amend",
"lerna-publish": "npx lerna publish from-git --yes",
"publish-complete": "git push --follow-tags --no-verify origin master",
"release": "npm-run-all pre-publish lerna-version fix-changelogs lerna-publish publish-complete",
"release": "dev-utils release",
"dev-utils": "dev-utils",
"sandbox": "yarn workspace documentation sandbox",
"start": "yarn workspace documentation start-dev",
Expand Down
2 changes: 2 additions & 0 deletions packages/dev-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
"postcss-sorting": "^5.0.1",
"prettier": "^2.1.0",
"pretty-ms": "^7.0.0",
"prompts": "^2.3.2",
"rimraf": "^3.0.2",
"sassdoc": "^2.7.2",
"typescript": "^3.9.7"
},
"devDependencies": {
"@types/cssnano": "^4.0.0",
"@types/he": "^1.1.1",
"@types/prompts": "^2.0.8",
"chokidar-cli": "^2.1.0"
}
}
30 changes: 26 additions & 4 deletions packages/dev-utils/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import indexer from "./indexer";
import libsize from "./libsize";
import prepublish from "./prepublish";
import readmes from "./readmes";
import release, { toReleaseType, RELEASE_TYPES } from "./release";
import rmdReadme from "./rmdReadme";
import sandbox from "./sandbox";
import sassdoc from "./sassdoc";
Expand Down Expand Up @@ -81,7 +82,10 @@ createCommand("sassdoc")
.description(
"Creates the sassdoc for the documentation site in all scoped packages."
)
.option("--no-copy", "")
.option(
"--no-copy",
"Updates the command to no longer copy the scss files into all the dists."
)
.action(({ copy = true }) => sassdoc(copy));

createCommand("variables")
Expand Down Expand Up @@ -139,11 +143,15 @@ createCommand("libsize")
"--commit",
"Updates the command to commit any libsize changes to the base README.md and the about page in the documentation site"
)
.option(
"--stage",
"Updates the command to stage any libsize changes instead of committing."
)
.description(
"Prints the gzipped size for the entire library based on the UMD bundle and the min/max prebuilt CSS themes."
)
.action(({ umd = true, themes = true, commit = false }) =>
libsize(umd, themes, commit)
.action(({ umd = true, themes = true, commit = false, stage = false }) =>
libsize(umd, themes, commit, stage)
);

createCommand("themes")
Expand Down Expand Up @@ -194,6 +202,20 @@ createCommand("fix-changelogs")
"--amend",
"Amend the previous commit to include these changes. This should really only be used during the release process."
)
.action(({ amend = false }) => fixChangelogs(amend));
.option("--stage", "Boolean if the changes should only be staged")
.action(({ amend = false, stage = false }) => fixChangelogs(amend, stage));
createCommand("release")
.option(
"-t, --type <type>",
`The release type. This should be one of: [ ${RELEASE_TYPES.join(", ")} ]`,
toReleaseType
)
.option(
"-b, --blog",
"Updates the command to wait for the blog to be written before triggering the release. " +
"This will default to `true` when the release type is not one of the `pre*` types."
)
.description("Triggers a release for react-md")
.action(({ type = "", blog = undefined }) => release(type, blog));

commander.parse(process.argv);
1 change: 1 addition & 0 deletions packages/dev-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const projectRoot = execSync("git rev-parse --show-toplevel")
.trim();
export const packagesRoot = join(projectRoot, "packages");
export const documentationRoot = join(packagesRoot, "documentation");
export const themesDist = join(projectRoot, "themes");

export const isRoot = process.cwd() === projectRoot;

Expand Down
19 changes: 8 additions & 11 deletions packages/dev-utils/src/fixChangelogs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { readFile, writeFile } from "fs-extra";
import log from "loglevel";
import { join } from "path";

import { projectRoot } from "./constants";
import indexer from "./indexer";
import getPackages from "./utils/getPackages";
import git from "./utils/git";
import git, { uncommittedFiles, ammendCommit } from "./utils/git";
import glob from "./utils/glob";

type Transformer = (changelog: string, isRoot: boolean) => string;
Expand Down Expand Up @@ -84,7 +82,10 @@ const transform = (changelog: string, isRoot: boolean): string =>
const CHANGELOG_FILES =
"CHANGELOG.md packages/*/CHANGELOG.md packages/documentation/src/constants/meta";

export default async function fixChangelogs(amend: boolean): Promise<void> {
export default async function fixChangelogs(
amend: boolean,
stage: boolean = false
): Promise<void> {
log.info("Finding and formatting changelogs...");
const packagesChangelogs = await glob("packages/*/CHANGELOG.md");
const changelogPaths = ["CHANGELOG.md", ...packagesChangelogs];
Expand All @@ -100,15 +101,11 @@ export default async function fixChangelogs(amend: boolean): Promise<void> {

await indexer();

if (amend && git(`diff ${CHANGELOG_FILES}`)) {
if (uncommittedFiles() && (amend || stage)) {
git(`add ${CHANGELOG_FILES}`);
git("commit --amend --no-edit");

const { version } = await import(join(projectRoot, "lerna.json"));
const isTagged = !!git(`tag --list 'v${version}'`);
if (isTagged) {
git(`tag -d v${version}`);
git(`tag v${version} -a "v${version}"`);
if (amend) {
ammendCommit();
}
}
}
19 changes: 12 additions & 7 deletions packages/dev-utils/src/libsize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import format from "./utils/format";
import glob from "./utils/glob";
import list from "./utils/list";
import writeFile from "./utils/writeFile";
import git from "./utils/git";
import git, { uncommittedFiles } from "./utils/git";

const cwd = join(packagesRoot, "react-md");

Expand Down Expand Up @@ -112,18 +112,20 @@ async function umdSize(): Promise<string[]> {
}

async function cssSize(): Promise<string[]> {
let css = await glob("dist/css/*.min.css", { cwd });
let css = await glob("themes/*.min.css", { cwd: projectRoot });
if (!css.length) {
log.info("No compiled css files found...");
log.info("Creating...");
log.info();
await createLoggedThemes();
css = await glob("dist/css/*.min.css", { cwd });
css = await glob("themes/*.min.css", { cwd: projectRoot });
}

const { min, max } = css.reduce(
(result, cssPath) => {
const size = gzipSize.sync(readFileSync(join(cwd, cssPath), "utf8"));
const size = gzipSize.sync(
readFileSync(join(projectRoot, cssPath), "utf8")
);
const update = { name: cssPath, size };
if (size > result.max.size) {
result.max = update;
Expand Down Expand Up @@ -220,7 +222,8 @@ export default function OtherPros(): ReactElement {
export default async function libsize(
umd: boolean = true,
themes: boolean = true,
commit: boolean = false
commit: boolean = false,
stageChanges: boolean = false
): Promise<void> {
if (umd) {
await createUmd();
Expand All @@ -246,12 +249,14 @@ ${list(css)}
);
updateOtherPros(umds, css);

if (!commit || !git("diff README.md")) {
if ((!stageChanges && !commit) || !uncommittedFiles()) {
return;
}

git(
"add README.md packages/documentation/src/components/About/README.md packages/documentation/src/components/Home/LibraryInfo/OtherPros.tsx"
);
git('commit -m "chore(libsize): Updated library size"');
if (commit) {
git('commit -m "chore(libsize): Updated library size"');
}
}
9 changes: 9 additions & 0 deletions packages/dev-utils/src/prepublish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import log from "loglevel";

import clean from "./clean";
import libsize from "./libsize";
import createScssVariables from "./scssVariables";
import git, { uncommittedFiles } from "./utils/git";

export default async function prepublish(init: boolean): Promise<void> {
if (!init) {
log.info("Cleaning all the old dists and .tsbuildinfo...");
await clean(true);
}

if (!init) {
await createScssVariables();
if (uncommittedFiles()) {
git("add -u");
}
}

execSync("yarn build", { stdio: "inherit" });
if (init) {
log.info("Initial setup complete!");
Expand Down
93 changes: 93 additions & 0 deletions packages/dev-utils/src/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { execSync } from "child_process";
import log from "loglevel";
import prompts from "prompts";

import { projectRoot } from "./constants";
import fixChangelogs from "./fixChangelogs";
import prepublish from "./prepublish";
import git, { replaceTag, uncommittedFiles, ammendCommit } from "./utils/git";

export type ReleaseType =
| "major"
| "minor"
| "patch"
| "premajor"
| "preminor"
| "prepatch"
| "prerelease"
| "";

export const RELEASE_TYPES: ReadonlyArray<ReleaseType> = [
"major",
"minor",
"patch",
"premajor",
"preminor",
"prepatch",
"prerelease",
];

export function toReleaseType(value: string): ReleaseType {
if (RELEASE_TYPES.includes(value as ReleaseType)) {
return value as ReleaseType;
}

return "";
}

const run = (command: string): void => {
log.debug(command);
execSync(command, {
cwd: projectRoot,
stdio: "inherit",
});
};

export default async function release(
type: ReleaseType = "",
blog: boolean = !type.startsWith("pre")
): Promise<void> {
// first, update the version since I'll be ammending this commit and tag with
// libsize changes, prettier changelogs, and adding the themes specifically
// for the tag only
run(`npx lerna version ${type} --no-push --yes`);
await fixChangelogs(false, true);

// run a clean build to create all the dists
await prepublish(false);

// add the pre-compiled themes to git so they can be included in the tag, but
// then remove them and ammend the commit with them removed so they aren't
// added to the main branch.
git("add -f themes");
await replaceTag();

git("rm -rf themes");
ammendCommit();

if (blog) {
const { blogged } = await prompts({
type: "confirm",
name: "blogged",
message: "Has the blog been written?",
initial: false,
});

if (!blogged) {
process.exit(1);
}

if (uncommittedFiles()) {
git("add -u");
await replaceTag();
}
}

let distTag = "";
if (type.startsWith("pre")) {
distTag = " --dist-tag=next";
}
run(`npx lerna publish from-git${distTag} --yes`);

git("push --follow-tags --no-verify origin master");
}
Loading

0 comments on commit 5886f9f

Please sign in to comment.