Skip to content

Commit b0e4641

Browse files
Preserve original readme and only replace given section
1 parent f838541 commit b0e4641

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

readme.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-env node */
22
// eslint-disable console
3-
import { promises } from "fs";
3+
import { promises, existsSync } from "fs";
44
import path from "path";
55

66
import ignored from "./ignore.js";
@@ -88,6 +88,34 @@ const main = async (cwd, level = 0) => {
8888

8989
const readmePath = path.join(cwd, "README.md");
9090

91+
if (existsSync(readmePath)) {
92+
const contents = await readFileAsync(readmePath, "utf-8");
93+
94+
const prefix = "<!-- DOCS:START - Do not remove or modify this section -->";
95+
const postfix = "<!-- DOCS:END -->";
96+
97+
const prefixIndex = contents.indexOf(prefix);
98+
const postfixIndex = contents.indexOf(postfix);
99+
100+
if (prefixIndex !== -1 && postfixIndex !== -1) {
101+
const before = contents.substr(0, prefixIndex);
102+
const after = contents.substr(postfixIndex + postfix.length);
103+
104+
const padding = "".padEnd(2, "#");
105+
106+
const withAdjustedLevels = composedReadme.replace(
107+
/(#+)/gi,
108+
`${padding}$1`
109+
);
110+
111+
const replaced = `${before}${prefix}\n${withAdjustedLevels.trim()}\n${postfix}${after}`;
112+
113+
await writeFileAsync(readmePath, replaced, "utf-8");
114+
115+
return;
116+
}
117+
}
118+
91119
await writeFileAsync(readmePath, composedReadme, "utf-8");
92120
};
93121

0 commit comments

Comments
 (0)