Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
93 additions
and
81 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" }, | ||
}), | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`, | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.