Skip to content

Commit 3bf6ddd

Browse files
committed
feat: add --check flag
1 parent b739de3 commit 3bf6ddd

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

.README/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ Gitdown adds [additional functionality](#features) (generating table of contents
4444

4545
{"gitdown": "include", "file": "./usage.md"}
4646

47+
a
48+
4749
## Features
4850

4951
{"gitdown": "include", "file": "./helpers/contents.md"}
@@ -61,23 +63,20 @@ Gitdown adds [additional functionality](#features) (generating table of contents
6163

6264
### Automating Gitdown
6365

64-
Use [Husky](https://www.npmjs.com/package/husky) to automate generation of README.md and committing it to the version control.
66+
Use [Husky](https://www.npmjs.com/package/husky) to check if user generated README.md before committing his changes.
6567

6668
```json
6769
"husky": {
6870
"hooks": {
69-
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
70-
"pre-commit": "npm run lint && npm run test && npm run build"
71+
"pre-commit": "npm run lint && npm run test && npm run build",
72+
"pre-push": "gitdown ./.README/README.md --output-file ./README.md --check",
7173
}
7274
}
7375

7476
```
7577

76-
Where `create-readme` is your script to generate `README.md`, e.g.
78+
`--check` attributes makes Gitdown check if the target file differes from the source template. If the file differs then the program exits with an error code and message:
7779

78-
```json
79-
"scripts": {
80-
"create-readme": "gitdown ./.README/README.md --output-file ./README.md",
81-
}
80+
> Gitdown destination file does not represent the current state of the template.
8281
83-
```
82+
Do not automate generating and committing documentation: automating commits will result in a noisy commit log.

.README/helpers/date.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Prints a string formatted according to the given [moment format](http://momentjs
2222
Generates:
2323

2424
```markdown
25-
{"gitdown": "date"}
26-
{"gitdown": "date", "format": "YYYY"}
25+
1563038327
26+
2019
2727

2828
```
2929

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ foo: bar
207207
```
208208

209209

210+
a
211+
210212
<a name="features"></a>
211213
## Features
212214

@@ -675,23 +677,20 @@ master
675677
<a name="recipes-automating-gitdown"></a>
676678
### Automating Gitdown
677679

678-
Use [Husky](https://www.npmjs.com/package/husky) to automate generation of README.md and committing it to the version control.
680+
Use [Husky](https://www.npmjs.com/package/husky) to check if user generated README.md before committing his changes.
679681

680682
```json
681683
"husky": {
682684
"hooks": {
683-
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
684-
"pre-commit": "npm run lint && npm run test && npm run build"
685+
"pre-commit": "npm run lint && npm run test && npm run build",
686+
"pre-push": "gitdown ./.README/README.md --output-file ./README.md --check",
685687
}
686688
}
687689

688690
```
689691

690-
Where `create-readme` is your script to generate `README.md`, e.g.
692+
`--check` attributes makes Gitdown check if the target file differes from the source template. If the file differs then the program exits with an error code and message:
691693

692-
```json
693-
"scripts": {
694-
"create-readme": "gitdown ./.README/README.md --output-file ./README.md",
695-
}
694+
> Gitdown destination file does not represent the current state of the template.
696695
697-
```
696+
Do not automate generating and committing documentation: automating commits will result in a noisy commit log.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
},
4646
"husky": {
4747
"hooks": {
48-
"post-commit": "if [[ -z \"$SKIP_CREATE_README\" ]]; then export SKIP_CREATE_README=1 && npm run create-readme && git commit -m 'docs: generate docs' --no-verify -- README.md; else unset SKIP_CREATE_README; fi",
49-
"pre-commit": "npm run lint && npm run test && npm run build"
48+
"pre-commit": "npm run lint && npm run test && npm run build",
49+
"pro-push": "babel-node ./src/bin/index.js ./.README/README.md --output-file ./README.md --check"
5050
}
5151
},
5252
"keywords": [

src/bin/index.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ const argv = yargs
99
.usage('Usage: $0 <README.md> [options]')
1010
.demand(1, 1, 'Gitdown program must be executed with exactly one non-option argument.')
1111
.options({
12+
check: {
13+
default: false,
14+
description: 'Checks if the destination file represents the current state of the template. Terminates program with exit status 1 if generating a new document would result in changes of the target document. Terminates program with exit status 0 otherwise (without writng to the destination).',
15+
type: 'boolean'
16+
},
1217
force: {
1318
default: false,
14-
demand: false,
1519
describe: 'Write to file with different extension than ".md".',
1620
type: 'boolean'
1721
},
@@ -61,17 +65,35 @@ const argv = yargs
6165
.strict()
6266
.argv;
6367

64-
((inputFile, outputFile) => {
68+
const main = async () => {
69+
const inputFile = argv._[0];
70+
const outputFile = argv.outputFile;
71+
6572
const resolvedInputFile = path.resolve(process.cwd(), inputFile);
6673
const resolvedOutputFile = path.resolve(process.cwd(), outputFile);
6774

6875
// eslint-disable-next-line global-require
69-
const Gitdown = require('./..');
76+
const Gitdown = require('..');
7077

7178
const gitdown = Gitdown.readFile(resolvedInputFile);
7279

80+
if (argv.check) {
81+
const generatedMarkdown = await gitdown.get();
82+
83+
if (fs.readFileSync(resolvedOutputFile, 'utf-8') === generatedMarkdown) {
84+
return;
85+
} else {
86+
// eslint-disable-next-line no-console
87+
console.error('Gitdown destination file does not represent the current state of the template.');
88+
89+
// eslint-disable-next-line no-process-exit
90+
process.exit(1);
91+
}
92+
93+
return;
94+
}
95+
7396
gitdown.writeFile(resolvedOutputFile);
74-
})(
75-
argv._[0],
76-
argv.outputFile
77-
);
97+
};
98+
99+
main();

0 commit comments

Comments
 (0)