Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 4474d15

Browse files
committed
Fixed postcommit hook
1 parent 1f4d25b commit 4474d15

File tree

6 files changed

+108
-67
lines changed

6 files changed

+108
-67
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ _Table Of Contents_
3737
- [Hooks](#hooks)
3838
- [useHashScroll](#usehashscroll)
3939
- [Summary](#summary-3)
40+
- [Demo](#demo-3)
4041
- [Params](#params)
4142
- [Example](#example-3)
4243
- [Reused Props](#reused-props)

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@types/react-dom": "^17.0.0",
5959
"@types/react-router-dom": "^5.1.6",
6060
"@types/react-test-renderer": "^17.0.0",
61+
"@types/rimraf": "^3.0.0",
6162
"@typescript-eslint/eslint-plugin": "^4.8.2",
6263
"@typescript-eslint/parser": "^4.8.2",
6364
"axios": "^0.21.0",
@@ -80,6 +81,7 @@
8081
"react": "^17.0.1",
8182
"react-dom": "^17.0.1",
8283
"react-router-dom": "^5.2.0",
84+
"rimraf": "^3.0.2",
8385
"rollup": "^2.33.3",
8486
"rollup-plugin-terser": "^7.0.2",
8587
"simple-git": "^2.24.0",

scripts/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { join, parse } from "path";
44
import { exec } from "child_process";
55

66
import simpleGit from "simple-git";
7+
import * as rimraf from "rimraf";
78
import * as moment from "moment";
89

910
export const { readFile, writeFile, readdir: readDir } = promises;
@@ -46,3 +47,4 @@ export const PACKAGE_REPO_NAME = "react-hash-scroll";
4647
export const PACKAGE_REPO_OWNER = "YashTotale";
4748
export const ROOT_DIR = join(__dirname, "..");
4849
export const git = simpleGit(ROOT_DIR);
50+
export const remove = promisify(rimraf);

scripts/postcommit.ts

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,82 @@ import {
88
parse,
99
writeFile,
1010
getBranches,
11-
execute,
11+
remove,
1212
} from "./helpers";
1313

1414
const postcommit = async () => {
1515
try {
1616
if ((await getBranches()).current.includes("main")) {
17-
const { stdout } = await getFilesChangedInCommit("HEAD");
17+
const { stdout: changed } = await getFilesChangedInCommit("HEAD");
18+
19+
console.log(changed);
1820

1921
const docsDir = join(ROOT_DIR, "docs");
2022

21-
const componentsPath = "docs/Components/";
23+
const docsPath = "docs/";
2224

23-
if (stdout.includes(componentsPath)) {
24-
const rootGit = simpleGit(ROOT_DIR);
25+
const replacements = [
26+
"behavior",
27+
"position",
28+
"requiredpathname",
29+
"scrollfunc",
30+
];
2531

32+
if (changed.includes(docsPath)) {
2633
const wikiPath = join(ROOT_DIR, "wiki");
2734

35+
const rootGit = simpleGit(ROOT_DIR);
36+
2837
await rootGit.clone(
2938
"https://github.com/YashTotale/react-hash-scroll.wiki.git",
3039
wikiPath
3140
);
3241

3342
const git = simpleGit(wikiPath);
3443

35-
const componentsDir = join(docsDir, "Components");
36-
37-
const files = await readDir(join(componentsDir));
38-
39-
const replacements = [
40-
"behavior",
41-
"position",
42-
"requiredpathname",
43-
"scrollfunc",
44-
];
45-
46-
for (const file of files) {
47-
if (stdout.includes(`${componentsPath}${file}`)) {
48-
let fileContents = await readFile(
49-
join(componentsDir, file),
50-
"utf-8"
51-
);
52-
if (parse(file).name !== "ReusedProps") {
53-
replacements.forEach((r) => {
54-
fileContents = fileContents.replace(
55-
new RegExp(`(#${r})`, "g"),
56-
`https://github.com/YashTotale/react-hash-scroll/wiki/ReusedProps#${r}`
57-
);
58-
});
44+
const buildFile = async (file: string, dir: string) => {
45+
let fileContents = await readFile(join(dir, file), "utf-8");
46+
if (parse(file).name !== "ReusedProps") {
47+
replacements.forEach((r) => {
48+
fileContents = fileContents.replace(
49+
new RegExp(`(#${r})`, "g"),
50+
`https://github.com/YashTotale/react-hash-scroll/wiki/ReusedProps#${r}`
51+
);
52+
});
53+
}
54+
await writeFile(join(wikiPath, file), fileContents);
55+
};
56+
57+
const buildWiki = async (name: string, isFile?: boolean) => {
58+
const path = docsPath + name;
59+
60+
if (changed.includes(path)) {
61+
if (isFile) await buildFile(name, docsDir);
62+
else {
63+
const dir = join(docsDir, name);
64+
65+
const files = await readDir(dir);
66+
67+
for (const file of files) {
68+
if (changed.includes(`${path}/${file}`)) {
69+
await buildFile(file, dir);
70+
}
71+
}
5972
}
60-
await writeFile(join(wikiPath, file), fileContents);
6173
}
62-
}
74+
};
75+
76+
await buildWiki("Components");
77+
78+
await buildWiki("Hooks");
79+
80+
await buildWiki("ReusedProps.md", true);
6381

64-
git.add(wikiPath);
65-
git.commit("Changes");
66-
git.push();
82+
await git.add(wikiPath);
83+
await git.commit("Changes");
84+
await git.push();
6785

68-
await execute(`rm -rf ${wikiPath}`);
86+
await remove(wikiPath);
6987
}
7088
}
7189
} catch (e) {

scripts/precommit.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -159,46 +159,38 @@ const checkReadme = async () => {
159159
const docsPath = "docs/";
160160

161161
if (staged.includes(docsPath)) {
162-
const buildReadme = async (name: string, isFile?: boolean) => {
163-
const path = docsPath + name;
162+
const buildFile = async (file: string, dir: string, hashCount = 3) => {
163+
const readme = await getReadme();
164164

165-
if (staged.includes(path)) {
166-
const buildFile = async (
167-
file: string,
168-
dir: string,
169-
hashCount = 3
170-
) => {
171-
const readme = await getReadme();
165+
const hashes = "#".repeat(hashCount);
172166

173-
const hashes = "#".repeat(hashCount);
167+
let titleIndex = readme.indexOf(`${hashes} ${parse(file).name}`);
174168

175-
let titleIndex = readme.indexOf(`${hashes} ${parse(file).name}`);
169+
if (titleIndex < 0)
170+
titleIndex = readme.indexOf(
171+
`${hashes} ${parse(file)
172+
.name.match(/[A-Z][a-z]+|[0-9]+/g)
173+
?.join(" ")}`
174+
);
176175

177-
if (titleIndex < 0)
178-
titleIndex = readme.indexOf(
179-
`${hashes} ${parse(file)
180-
.name.match(/[A-Z][a-z]+|[0-9]+/g)
181-
?.join(" ")}`
182-
);
176+
const endIndex = readme.indexOf("---", titleIndex);
183177

184-
const endIndex = readme.indexOf("---", titleIndex);
178+
const fileContents = await readFile(join(dir, file), "utf-8");
185179

186-
const fileContents = await readFile(join(dir, file), "utf-8");
180+
const newReadme =
181+
readme.substring(0, titleIndex) +
182+
fileContents +
183+
"\n" +
184+
readme.substring(endIndex);
187185

188-
const newReadme =
189-
readme.substring(0, titleIndex) +
190-
fileContents +
191-
"\n" +
192-
readme.substring(endIndex);
193-
194-
await writeFile(readmeDest, newReadme);
195-
};
186+
await writeFile(readmeDest, newReadme);
187+
};
188+
const buildReadme = async (name: string, isFile?: boolean) => {
189+
const path = docsPath + name;
196190

197-
if (isFile) {
198-
if (staged.includes(path)) {
199-
await buildFile(name, docsDir, 2);
200-
}
201-
} else {
191+
if (staged.includes(path)) {
192+
if (isFile) await buildFile(name, docsDir, 2);
193+
else {
202194
const dir = join(docsDir, name);
203195

204196
const files = await readDir(dir);

0 commit comments

Comments
 (0)