Skip to content

Commit

Permalink
feat: add --check flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jul 13, 2019
1 parent b739de3 commit 3bf6ddd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
17 changes: 8 additions & 9 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Gitdown adds [additional functionality](#features) (generating table of contents

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

a

## Features

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

### Automating Gitdown

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

```json
"husky": {
"hooks": {
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
"pre-commit": "npm run lint && npm run test && npm run build"
"pre-commit": "npm run lint && npm run test && npm run build",
"pre-push": "gitdown ./.README/README.md --output-file ./README.md --check",
}
}

```

Where `create-readme` is your script to generate `README.md`, e.g.
`--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:

```json
"scripts": {
"create-readme": "gitdown ./.README/README.md --output-file ./README.md",
}
> Gitdown destination file does not represent the current state of the template.
```
Do not automate generating and committing documentation: automating commits will result in a noisy commit log.
4 changes: 2 additions & 2 deletions .README/helpers/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Prints a string formatted according to the given [moment format](http://momentjs
Generates:

```markdown
{"gitdown": "date"}
{"gitdown": "date", "format": "YYYY"}
1563038327
2019

```

Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ foo: bar
```


a

This comment has been minimized.

Copy link
@brettz9

brettz9 Jul 15, 2019

Contributor

I presume these lines were inadvertent?

<a name="features"></a>
## Features

Expand Down Expand Up @@ -675,23 +677,20 @@ master
<a name="recipes-automating-gitdown"></a>
### Automating Gitdown

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

```json
"husky": {
"hooks": {
"post-commit": "npm run create-readme && git add README.md && git commit -m 'docs: generate docs' --no-verify",
"pre-commit": "npm run lint && npm run test && npm run build"
"pre-commit": "npm run lint && npm run test && npm run build",
"pre-push": "gitdown ./.README/README.md --output-file ./README.md --check",
}
}

```

Where `create-readme` is your script to generate `README.md`, e.g.
`--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:

```json
"scripts": {
"create-readme": "gitdown ./.README/README.md --output-file ./README.md",
}
> Gitdown destination file does not represent the current state of the template.
```
Do not automate generating and committing documentation: automating commits will result in a noisy commit log.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
},
"husky": {
"hooks": {
"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",
"pre-commit": "npm run lint && npm run test && npm run build"
"pre-commit": "npm run lint && npm run test && npm run build",
"pro-push": "babel-node ./src/bin/index.js ./.README/README.md --output-file ./README.md --check"

This comment has been minimized.

Copy link
@brettz9

brettz9 Jul 15, 2019

Contributor

Should this be pre-push?

}
},
"keywords": [
Expand Down
36 changes: 29 additions & 7 deletions src/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ const argv = yargs
.usage('Usage: $0 <README.md> [options]')
.demand(1, 1, 'Gitdown program must be executed with exactly one non-option argument.')
.options({
check: {
default: false,
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).',
type: 'boolean'
},
force: {
default: false,
demand: false,
describe: 'Write to file with different extension than ".md".',
type: 'boolean'
},
Expand Down Expand Up @@ -61,17 +65,35 @@ const argv = yargs
.strict()
.argv;

((inputFile, outputFile) => {
const main = async () => {
const inputFile = argv._[0];
const outputFile = argv.outputFile;

const resolvedInputFile = path.resolve(process.cwd(), inputFile);
const resolvedOutputFile = path.resolve(process.cwd(), outputFile);

// eslint-disable-next-line global-require
const Gitdown = require('./..');
const Gitdown = require('..');

const gitdown = Gitdown.readFile(resolvedInputFile);

if (argv.check) {
const generatedMarkdown = await gitdown.get();

if (fs.readFileSync(resolvedOutputFile, 'utf-8') === generatedMarkdown) {
return;
} else {
// eslint-disable-next-line no-console
console.error('Gitdown destination file does not represent the current state of the template.');

// eslint-disable-next-line no-process-exit
process.exit(1);
}

return;
}

gitdown.writeFile(resolvedOutputFile);
})(
argv._[0],
argv.outputFile
);
};

main();

0 comments on commit 3bf6ddd

Please sign in to comment.