Skip to content

Commit

Permalink
fix(md-aggregate): use esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
snomiao committed May 26, 2022
1 parent 04a3ce9 commit 92b8891
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 81 deletions.
79 changes: 0 additions & 79 deletions packages/md-aggregate/cli.mjs

This file was deleted.

20 changes: 20 additions & 0 deletions packages/md-aggregate/esbuild.mjs
@@ -0,0 +1,20 @@
import esbuild from "esbuild";
await Promise.all([
esbuild.build({
entryPoints: ["src/index.ts", "src/cli.ts"],
platform: "node",
minify: true,
sourcemap: true,
outdir: "lib/esm",
format: "esm",
}),
esbuild.build({
entryPoints: ["src/index.ts", "src/cli.ts"],
platform: "node",
minify: true,
sourcemap: true,
outdir: "lib/cjs",
format: "cjs",
outExtension: { ".js": ".cjs" },
}),
]);
6 changes: 4 additions & 2 deletions packages/md-aggregate/package.json
Expand Up @@ -19,11 +19,13 @@
"author": "snomiao <snomiao@gmail.com>",
"bin": "cli.mjs",
"scripts": {
"postversion": "prettier -w CHANGELOG.md && md-aggregate CHANGELOG.md README.md -w"
"postversion": "prettier -w CHANGELOG.md && md-aggregate CHANGELOG.md README.md -w",
"build": "tsc && node esbuild.mjs"
},
"dependencies": {
"arg": "^5.0.1",
"escape-string-regexp": "^5.0.0"
"escape-string-regexp": "^5.0.0",
"yargs": "^17.5.1"
},
"publishConfig": {
"access": "public"
Expand Down
21 changes: 21 additions & 0 deletions packages/md-aggregate/src/cli.ts
@@ -0,0 +1,21 @@
#!/usr/bin/env node

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import mdAggregate from ".";

const argv = await yargs(hideBin(process.argv))
.usage("md-aggregate [...src.md] dst.md [-w]")
.example("one", "md-aggregate CHANGELOG.md README.md -w")
.example("multi", "md-aggregate LICENSE.md CHANGELOG.md README.md -w")
.boolean("write")
.alias("w", "write")
.describe("w", "write mode (when missing you will get preview in console)")
.demand(2)
.help()
.alias("h", "help")
.version()
.alias("v", "version").argv;

const [dstPath, ...srcs] = argv._.reverse();
await mdAggregate(String(dstPath), srcs.map(String), argv);
46 changes: 46 additions & 0 deletions packages/md-aggregate/src/index.ts
@@ -0,0 +1,46 @@
import escapeStringRegexp from "escape-string-regexp";
import { readFile, writeFile } from "fs/promises";
import { resolve } from "path";

export default async function mdAggregate(
dstPath: string,
srcs: string[],
argv: { write: boolean },
) {
let dstContent = await readFile(resolve(dstPath), "utf8");
await Promise.all(srcs.map(srcProcess));
if (argv.write) {
await writeFile(resolve(dstPath), dstContent);
console.log(resolve(dstPath));
} else {
console.log(dstContent);
}
return;
async function srcProcess(src) {
const srcContent = await readFile(resolve(src), "utf8");
if (!srcContent.match(/^# /)) throw new Error("srcContent must start with '# '");
const lines = srcContent
.trim()
.split("\n")
.map((e) => e.replace(/^#/gim, "##"));

// TODO: handle \r\n
const matchStartPattern = `\n${escapeStringRegexp(lines[0])}$`;
const eofPattern = "$(?![\\r\\n])";

const sectionStartPattern = `\n##? `;

const startPattern = `${matchStartPattern}|${eofPattern}`;
const anyPattern = `[\\s\\S]*?`;
const endPattern = `${eofPattern}|${sectionStartPattern}`;

const srcContentWithoutSectionStart = lines
.join("\n")
.replace(new RegExp(sectionStartPattern, "img"), "\n### ");

dstContent = dstContent.replace(
new RegExp(`(${startPattern})${anyPattern}(?=${endPattern})`, "m"),
`\n${srcContentWithoutSectionStart}\n`,
);
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92b8891

Please sign in to comment.