Skip to content

Commit

Permalink
feat: regenerate readme options on release
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden authored and kellyselden committed Jun 20, 2020
1 parent 929134c commit a8d236c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ Here are a couple examples of using the FalTest CLI.

## Default Options

(regen using `yarn start --help`)

<!-- CODEGEN_CLI_HELP_START -->
```
--help Show help [boolean]
--version Show version number [boolean]
Expand Down Expand Up @@ -75,6 +74,7 @@ Here are a couple examples of using the FalTest CLI.
--reporter Change the Mocha reporter [string]
--reporter-options Supply Mocha reporter options [string]
```
<!-- CODEGEN_CLI_HELP_END -->

## Filtering

Expand Down
16 changes: 16 additions & 0 deletions helpers/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const fs = require('fs');
const { promisify } = require('util');
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

async function replaceFile(path, callback) {
let contents = await readFile(path, 'utf8');
contents = await callback(contents);
await writeFile(path, contents);
}

module.exports = {
replaceFile,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint:git": "commitlint",
"lint:js": "eslint . --ext js,json",
"lint:md": "remark -f doc CONTRIBUTING.md README.md packages/*/README.md examples/*/README.md packages/lint/doc",
"release": "next release --scripts.postbump \"node scripts/postbump\" --no-bump-in-range-dependencies",
"release": "next release --scripts.precommit \"node scripts/precommit && git add -A\" --scripts.postbump \"node scripts/postbump\" --no-bump-in-range-dependencies",
"start": "./packages/cli/bin/index.js",
"test": "./scripts/run.js packages test",
"test:acceptance": "mocha --recursive"
Expand Down
51 changes: 51 additions & 0 deletions scripts/precommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env node
'use strict';

const execa = require('execa');
const path = require('path');
const { replaceFile } = require('../helpers/fs');

const rootDirectory = path.resolve(__dirname, '..');

async function replace({ replacementRegex, replacementText, filePath }) {
replacementText = `$1
\`\`\`
${replacementText}
\`\`\`
$2`;

await replaceFile(filePath, (contents) => {
if (!replacementRegex.test(contents)) {
throw new Error(`${filePath} is malformed`);
}

return contents.replace(replacementRegex, replacementText);
});
}

async function syncCliOptions() {
let helpText = (
await execa('yarn', ['--silent', 'start', '--help'], {
cwd: rootDirectory,
env: {
FALTEST_PRINT_VERSION: 'false',
},
})
).stdout;

let replacementRegex = /(<!-- CODEGEN_CLI_HELP_START -->).+(<!-- CODEGEN_CLI_HELP_END -->)/s;

let readmePath = path.join(rootDirectory, 'README.md');

await replace({
replacementRegex,
replacementText: helpText,
filePath: readmePath,
});
}

(async () => {
await syncCliOptions();
})();

require('../packages/cli/src/utils/throw-up');

0 comments on commit a8d236c

Please sign in to comment.