Skip to content

Commit 8aadc8c

Browse files
chiawendtbrettz9
authored andcommitted
chore: change the process for generatng readme
1. Remove post-commit hook for gernerating readme. 2. Add pre-push hook that checks readme is up to date. 3. Move other pre-commit hooks to pre-push hook. 4. Check readme is up to date in ci.
1 parent ef11562 commit 8aadc8c

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ sudo: false
1515
script:
1616
- npm run test
1717
- 'if [ -n "${LINT-}" ]; then npm run lint; fi'
18+
- 'if [ -n "${CHECK_README-}" ]; then npm run check-readme; fi'
1819
- npm run build
1920
env:
2021
matrix:
@@ -25,6 +26,8 @@ matrix:
2526
include:
2627
- node_js: 'lts/*'
2728
env: LINT=true
29+
- node_js: 'lts/*'
30+
env: CHECK_README=true
2831
exclude:
2932
- node_js: 6
3033
env: ESLINT=6

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
},
4141
"husky": {
4242
"hooks": {
43-
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
44-
"pre-commit": "npm run lint && npm run test && npm run build"
43+
"pre-push": "npm run lint && npm run test && npm run build && npm run check-readme"
4544
}
4645
},
4746
"keywords": [
@@ -60,9 +59,9 @@
6059
"url": "https://github.com/gajus/eslint-plugin-jsdoc"
6160
},
6261
"scripts": {
63-
"add-assertions": "babel-node ./src/bin/readme-assertions",
6462
"build": "rm -fr ./dist && NODE_ENV=production babel ./src --out-dir ./dist --copy-files --source-maps",
65-
"create-readme": "gitdown ./.README/README.md --output-file ./README.md && npm run add-assertions",
63+
"check-readme": "babel-node ./src/bin/generateReadme.js --check",
64+
"create-readme": "babel-node ./src/bin/generateReadme.js",
6665
"lint-fix": "eslint --fix ./src ./test",
6766
"lint": "eslint ./src ./test",
6867
"test-cov": "BABEL_ENV=test nyc mocha --recursive --require @babel/register --reporter progress --timeout 9000",

src/bin/readme-assertions.js renamed to src/bin/generateReadme.js

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'path';
55
import fs from 'fs';
66
import _ from 'lodash';
77
import glob from 'glob';
8+
import Gitdown from 'gitdown';
89

910
const trimCode = (code) => {
1011
let lines = code.replace(/^\n/, '').trimEnd().split('\n');
@@ -66,10 +67,24 @@ const getAssertions = () => {
6667
return _.zipObject(assertionNames, assertionCodes);
6768
};
6869

69-
const updateDocuments = (assertions) => {
70-
const readmeDocumentPath = path.join(__dirname, '../../README.md');
70+
const getSomeBranch = () => {
71+
const gitConfig = fs.readFileSync(path.join(__dirname, '../../.git/config')).toString();
72+
const [, branch] = /\[branch "([^"]+)"\]/.exec(gitConfig) || [];
7173

72-
let documentBody = fs.readFileSync(readmeDocumentPath, 'utf8');
74+
return branch;
75+
};
76+
77+
const generateReadme = async () => {
78+
const assertions = getAssertions();
79+
const gitdown = Gitdown.readFile(path.join(__dirname, '../../.README/README.md'));
80+
81+
gitdown.setConfig({
82+
gitinfo: {
83+
defaultBranchName: getSomeBranch() || 'master',
84+
gitPath: path.join(__dirname, '../../.git')
85+
}
86+
});
87+
let documentBody = await gitdown.get();
7388

7489
documentBody = documentBody.replace(/<!-- assertions ([a-z]+?) -->/ig, (assertionsBlock) => {
7590
const ruleName = assertionsBlock.match(/assertions ([a-z]+)/i)[1];
@@ -83,7 +98,45 @@ const updateDocuments = (assertions) => {
8398
'\n````\n\nThe following patterns are not considered problems:\n\n````js\n' + ruleAssertions.valid.join('\n\n') + '\n````\n';
8499
});
85100

86-
fs.writeFileSync(readmeDocumentPath, documentBody);
101+
return documentBody;
102+
};
103+
104+
const generateReadmeAndWriteToDisk = async () => {
105+
const readme = await generateReadme();
106+
const dist = path.join(__dirname, '..', '..', 'README.md');
107+
fs.writeFileSync(dist, readme);
108+
};
109+
110+
const assertReadmeIsUpToDate = async () => {
111+
const readme = await generateReadme();
112+
const readmePath = path.join(__dirname, '..', '..', 'README.md');
113+
114+
const isUpToDate = fs.readFileSync(readmePath).toString() === readme;
115+
116+
if (!isUpToDate) {
117+
throw new Error('Readme is not up to date, please run `npm run create-readme` to update it.');
118+
}
87119
};
88120

89-
updateDocuments(getAssertions());
121+
const main = async () => {
122+
try {
123+
const hasCheckFlag = process.argv.some((arg) => {
124+
return arg === '--check';
125+
});
126+
127+
if (hasCheckFlag) {
128+
await assertReadmeIsUpToDate();
129+
} else {
130+
await generateReadmeAndWriteToDisk();
131+
}
132+
} catch (error) {
133+
/* eslint-disable-next-line no-console */
134+
console.error(error);
135+
/* eslint-disable-next-line no-process-exit */
136+
process.exit(1);
137+
}
138+
};
139+
140+
main();
141+
142+
export default generateReadme;

0 commit comments

Comments
 (0)