Skip to content

Commit

Permalink
feat(md-aggregate): multi input
Browse files Browse the repository at this point in the history
  • Loading branch information
snomiao committed Jun 17, 2022
1 parent 0144e2a commit fcf0b6e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 212 deletions.
196 changes: 14 additions & 182 deletions packages/md-aggregate/README.md
@@ -1,29 +1,30 @@
# MD-Aggregate

Aggregate README.md with CHANGELOG.md or whatever.
Aggregate README.md with CHANGELOG.md or whatever.md.

## Usage
## Install

```shell
npx md-aggregate CHANGELOG.md README.md --write

# or

npm i -g md-aggregate
md-aggregate CHANGELOG.md README.md --w
```

### help
## Usage

```plaintext
md-aggregate [...src] dst [options]
md-aggregate <target> [input..]
Example: md-aggregate CHANGELOG.md README.md -w
aggregate markdown files
Options:
-v, --version : show version
-h, --help : show help
-w, --write : write mode (when missing you will get preview in console)
-w, --write write mode (when missing you will get preview in console)
[boolean] -i, --input source markdown files [array] -t, --target destination markdown file [string] -h, --help Show help [boolean] -v, --version Show version number [boolean]
Examples:
preview: md-aggregate README.md CHANGELOG.md
one: md-aggregate README.md CHANGELOG.md -w
multi: md-aggregate README.md ABOUT.md LICENSE.md CHANGELOG.md -w
multi: md-aggregate README.md -i ABOUT.md -i LICENSE.md -i CHANGELOG.md
-w
```

## Description
Expand Down Expand Up @@ -66,173 +67,4 @@ It will be append or replaced into README.md like this:
...CHANGELOG

...

## License

License: GPLv3
Author: snomiao <snomiao@gmail.com>
```

## About

### License

GPLv3 - [The GNU General Public License v3.0 - GNU Project - Free Software Foundation](https://www.gnu.org/licenses/gpl-3.0.en.html)


### Author

Author: snomiao <snomiao@gmail.com>
Website: [snomiao.com](https://snomiao.com)

### Sponsors

- None yet.

Claim your sponsorship by donating snomiao <[Email: snomiao@gmail.com](mailto:snomiao@gmail.com)>

### Contribute

The main repo is in [here](https://github.com/snomiao/js#readme), any issue and PR's welcome.

## Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

Note: This section is orginally written in CHANGELOG.md file and is merged into README.md to show how this project works.

### [1.1.12](https://github.com/snomiao/js/compare/md-aggregate@1.1.11...md-aggregate@1.1.12) (2022-05-12)

**Note:** Version bump only for package md-aggregate

### [1.1.11](https://github.com/snomiao/js/compare/md-aggregate@0.1.10...md-aggregate@1.1.11) (2022-05-03)

**Note:** Version bump only for package md-aggregate

### [0.1.10](https://github.com/snomiao/js/compare/md-aggregate@0.1.9...md-aggregate@0.1.10) (2022-04-23)

**Note:** Version bump only for package md-aggregate

### [0.1.9](https://github.com/snomiao/js/compare/md-aggregate@0.1.8...md-aggregate@0.1.9) (2022-04-23)

**Note:** Version bump only for package md-aggregate

### [0.1.8](https://github.com/snomiao/js/compare/md-aggregate@0.1.7...md-aggregate@0.1.8) (2022-04-23)

#### Bug Fixes

- srcContentWithoutSectionStart ([ec8f862](https://github.com/snomiao/js/commit/ec8f862a50da91470bb670789849d58fef069ae3))

### [0.1.7](https://github.com/snomiao/js/compare/md-aggregate@0.1.6...md-aggregate@0.1.7) (2022-04-23)

#### Bug Fixes

- replace # => ## ([bfc6e79](https://github.com/snomiao/js/commit/bfc6e798e455714dea2caf181614003ba243e3f2))
- srcContentWithoutSectionStart ([3837654](https://github.com/snomiao/js/commit/383765497b9964381eaf5f65483e94a043f4bdf0))

### [0.1.6](https://github.com/snomiao/js/compare/md-aggregate@0.1.5...md-aggregate@0.1.6) (2022-04-21)

**Note:** Version bump only for package md-aggregate

### [0.1.5](https://github.com/snomiao/js/compare/md-aggregate@0.1.4...md-aggregate@0.1.5) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.1.4](https://github.com/snomiao/js/compare/md-aggregate@0.1.3...md-aggregate@0.1.4) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.1.3](https://github.com/snomiao/js/compare/md-aggregate@0.1.2...md-aggregate@0.1.3) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.1.2](https://github.com/snomiao/js/compare/md-aggregate@0.1.1...md-aggregate@0.1.2) (2022-04-20)

#### Bug Fixes

- writeFile in monopkgs ([d18e90a](https://github.com/snomiao/js/commit/d18e90a90dc8fc053092938148247dd3717d51c9))

### [0.1.1](https://github.com/snomiao/js/compare/md-aggregate@0.1.0...md-aggregate@0.1.1) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.1.0](https://github.com/snomiao/js/compare/md-aggregate@0.0.8...md-aggregate@0.1.0) (2022-04-20)

#### Features

- escapeStringRegexp ([a1263b3](https://github.com/snomiao/js/commit/a1263b33a1d15502bcb4b0b26f804cd1fa0a17c3))

### [0.0.8](https://github.com/snomiao/js/compare/md-aggregate@0.0.7...md-aggregate@0.0.8) (2022-04-20)

#### Bug Fixes

- add prettier and selflink to CHANGELOG.md ([df03f61](https://github.com/snomiao/js/commit/df03f61e2d3a5b46e87cecec592b67ceaad1381d))

### [0.0.7](https://github.com/snomiao/js/compare/md-aggregate@0.0.6...md-aggregate@0.0.7) (2022-04-20)

#### Bug Fixes

- apply write md-aggregate ([2aa4291](https://github.com/snomiao/js/commit/2aa4291997dea136e7769d7b6986e6e818da37ef))

### [0.0.6](https://github.com/snomiao/js/compare/md-aggregate@0.0.5...md-aggregate@0.0.6) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.0.5](https://github.com/snomiao/js/compare/md-aggregate@0.0.4...md-aggregate@0.0.5) (2022-04-20)

#### Bug Fixes

- apply md-aggregate ([2343a8d](https://github.com/snomiao/js/commit/2343a8dc6567da2093198e466a05aed60639484d))

### [0.0.4](https://github.com/snomiao/js/compare/md-aggregate@0.0.3...md-aggregate@0.0.4) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.0.3](https://github.com/snomiao/js/compare/md-aggregate@0.0.2...md-aggregate@0.0.3) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### 0.0.2 (2022-04-20)

**Note:** Version bump only for package md-aggregate

## [0.1.0](https://github.com/snomiao/js/compare/md-aggregate@0.0.8...md-aggregate@0.1.0) (2022-04-20)

#### Features

- escapeStringRegexp ([a1263b3](https://github.com/snomiao/js/commit/a1263b33a1d15502bcb4b0b26f804cd1fa0a17c3))

### [0.0.8](https://github.com/snomiao/js/compare/md-aggregate@0.0.7...md-aggregate@0.0.8) (2022-04-20)

#### Bug Fixes

- add prettier and selflink to CHANGELOG.md ([df03f61](https://github.com/snomiao/js/commit/df03f61e2d3a5b46e87cecec592b67ceaad1381d))

### [0.0.7](https://github.com/snomiao/js/compare/md-aggregate@0.0.6...md-aggregate@0.0.7) (2022-04-20)

#### Bug Fixes

- apply write md-aggregate ([2aa4291](https://github.com/snomiao/js/commit/2aa4291997dea136e7769d7b6986e6e818da37ef))

### [0.0.6](https://github.com/snomiao/js/compare/md-aggregate@0.0.5...md-aggregate@0.0.6) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.0.5](https://github.com/snomiao/js/compare/md-aggregate@0.0.4...md-aggregate@0.0.5) (2022-04-20)

#### Bug Fixes

- apply md-aggregate ([2343a8d](https://github.com/snomiao/js/commit/2343a8dc6567da2093198e466a05aed60639484d))

### [0.0.4](https://github.com/snomiao/js/compare/md-aggregate@0.0.3...md-aggregate@0.0.4) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### [0.0.3](https://github.com/snomiao/js/compare/md-aggregate@0.0.2...md-aggregate@0.0.3) (2022-04-20)

**Note:** Version bump only for package md-aggregate

### 0.0.2 (2022-04-20)

**Note:** Version bump only for package md-aggregate
23 changes: 15 additions & 8 deletions packages/md-aggregate/src/cli.ts
Expand Up @@ -5,15 +5,22 @@ 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")
.scriptName("md-aggregate")
.command("$0 <target> [input..]", "aggregate markdown files")
.example("preview: ", "md-aggregate README.md CHANGELOG.md ")
.example("one: ", "md-aggregate README.md CHANGELOG.md -w")
.example("multi: ", "md-aggregate README.md ABOUT.md LICENSE.md CHANGELOG.md -w")
.example("multi: ", "md-aggregate README.md -i ABOUT.md -i LICENSE.md -i CHANGELOG.md -w")
.option("target", { describe: "destination markdown file", type: "string" })
.option("input", { describe: "source markdown files", array: true, type: "string" })
.boolean("write")
.alias("w", "write")
.describe("w", "write mode (when missing you will get preview in console)")
.demandCommand(2)
.alias("i", "input")
.alias("t", "target")
.alias("w", "write")
.alias("h", "help")
.alias("v", "version").argv;
.alias("v", "version")
.demandCommand(2).argv;
console.log(argv);

const [dstPath, ...srcs] = argv._.reverse();
await mdAggregate(String(dstPath), srcs.map(String), argv);
await mdAggregate( argv);
37 changes: 15 additions & 22 deletions packages/md-aggregate/src/index.ts
Expand Up @@ -2,45 +2,38 @@ 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));
export default async function mdAggregate({
input = [] as string[],
target = "",
write = false,
} = {}) {
let dstContent = await readFile(resolve(target), "utf8");
await Promise.all(input.map(srcProcess));
if (write) {
await writeFile(resolve(target), dstContent);
console.log(resolve(target));
} else {
console.log(dstContent);
}
return;
async function srcProcess(src) {
async function srcProcess(src: string) {
const srcContent = await readFile(resolve(src), "utf8");
if (!srcContent.match(/^# /)) throw new Error("srcContent must start with '# '");
const lines = srcContent
.trim()
.split("\n")
.split(/\r?\n/)
.map((e) => e.replace(/^#/gim, "##"));

// TODO: handle \r\n
// TODO: test with \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`,
);
const pattern = new RegExp(`(${startPattern})${anyPattern}(?=${endPattern})`, "m");
dstContent = dstContent.replace(pattern, `\n${srcContentWithoutSectionStart}\n`);
}
}

0 comments on commit fcf0b6e

Please sign in to comment.