Skip to content

Commit

Permalink
trying to get generate-doc-examples working
Browse files Browse the repository at this point in the history
  • Loading branch information
anniegale9538 committed Jun 8, 2024
1 parent c202a6b commit c5c0491
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@ test/bundlers/parcel-test/.parcel-cache

lib
junit-output

parsed-alternative-report.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@types/tap": "^15.0.7",
"chai": "^4.3.7",
"cross-zip": "^4.0.0",
"dedent": "^1.5.3",
"desm": "^1.2.0",
"into-stream": "^7.0.0",
"js-yaml": "^4.1.0",
Expand All @@ -73,6 +74,7 @@
"rimraf": "^3.0.2",
"semver": "^7.3.7",
"split2": "^4.1.0",
"standard": "^17.1.0",
"stoppable": "^1.1.0",
"tap": "^16.1.0",
"ts-node": "^10.7.0",
Expand Down
22 changes: 10 additions & 12 deletions scripts/generate-docs-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,22 @@

/**
* To run this generator you must have the
* `alternatives_report.spec.json` placed in the root of this project.
* To get the `alternatives_report.spec.json` you must run the script
* to parse the original `alternatives_report.json`, which is not yet public
* and lives in github.com/elastic/clients-team/tree/master/scripts/docs-json-generator
*
* `parsed-alternative-report.json` (not public) placed in the root of this project.
* It lives in https://github.com/elastic/clients-flight-recorder/blob/main/recordings/docs/parsed-alternative-report.json
* This script will remove the content of the `docs/doc_examples` folder and generate
* all the files present in the `enabledFiles` list below.
* You can run it with the following command:
*
* ```bash
* $ node scripts/generate-docs-examples.js
* $ npx tsx scripts/generate-docs-examples.js
* ```
*/

const { join } = require('path')
const { writeFileSync } = require('fs')
const rimraf = require('rimraf')
const standard = require('standard')
const standard = require('standard').default
const dedent = require('dedent')

const docsExamplesDir = join('docs', 'doc_examples')
Expand Down Expand Up @@ -109,14 +107,14 @@ const enabledFiles = [
'search/suggesters.asciidoc'
]

function generate () {
async function generate () {
rimraf.sync(join(docsExamplesDir, '*'))
const examples = require(join(__dirname, '..', 'alternatives_report.spec.json'))
const examples = require(join(__dirname, '..', 'parsed-alternative-report.json'))
for (const example of examples) {
if (example.lang !== 'console') continue
if (!enabledFiles.includes(example.source_location.file)) continue

const asciidoc = generateAsciidoc(example.parsed_source)
const asciidoc = await generateAsciidoc(example.parsed_source)
writeFileSync(
join(docsExamplesDir, `${example.digest}.asciidoc`),
asciidoc,
Expand All @@ -125,7 +123,7 @@ function generate () {
}
}

function generateAsciidoc (source) {
async function generateAsciidoc (source) {
let asciidoc = '// This file is autogenerated, DO NOT EDIT\n'
asciidoc += '// Use `node scripts/generate-docs-examples.js` to generate the docs examples\n\n'
let code = 'async function run (client) {\n// START\n'
Expand All @@ -142,7 +140,7 @@ console.log(response${getResponsePostfix(i)})
}

code += '// END\n}'
const { results } = standard.lintTextSync(code, { fix: true })
const results = await standard.lintText(code, { fix: true })
code = results[0].output
code = code.slice(code.indexOf('// START\n') + 9, code.indexOf('\n\n// END'))

Expand Down

0 comments on commit c5c0491

Please sign in to comment.