Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trying to get generate-doc-examples.js working #2284

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 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 @@ -76,8 +77,8 @@
"stoppable": "^1.1.0",
"tap": "^16.1.0",
"ts-node": "^10.7.0",
"ts-standard": "^11.0.0",
"typescript": "^4.6.4",
"ts-standard": "11.0.0",
"typescript": "4.6.4",
Comment on lines +80 to +81
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ts-standard": "11.0.0",
"typescript": "4.6.4",
"ts-standard": "^11.0.0",
"typescript": "^4.6.4",

I'd switch back to what was already there just to keep things consistent. Pinning to an exact version is not ideal unless absolutely necessary.

"workq": "^3.0.0",
"xmlbuilder2": "^3.0.2",
"zx": "^7.2.2"
Expand Down
28 changes: 17 additions & 11 deletions scripts/generate-docs-examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@

/**
* 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:
Expand All @@ -38,7 +36,7 @@
const { join } = require('path')
const { writeFileSync } = require('fs')
const rimraf = require('rimraf')
const standard = require('standard')
const tsStandard = require('ts-standard')
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,15 @@ console.log(response${getResponsePostfix(i)})
}

code += '// END\n}'
const { results } = standard.lintTextSync(code, { fix: true })
const { results } = await new Promise((resolve, reject) => {
tsStandard.lintText(code, { fix: true }, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
Comment on lines +143 to +151
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to wrap this explicitly in a Promise. Since you made generateAsciidoc an async function, any errors thrown by lintText will be thrown just like any other exception would be.

Suggested change
const { results } = await new Promise((resolve, reject) => {
tsStandard.lintText(code, { fix: true }, (err, result) => {
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
const { results } = await tsStandard.lintText(code, { fix: true })

code = results[0].output
code = code.slice(code.indexOf('// START\n') + 9, code.indexOf('\n\n// END'))

Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export default class Helpers {
rest_total_hits_as_int: params.rest_total_hits_as_int,
scroll_id
}, options as TransportRequestOptionsWithMeta)
response = r as TransportResult<T.ScrollResponse<TDocument, TAggregations>, unknown>
response = r as unknown as TransportResult<T.ScrollResponse<TDocument, TAggregations>, unknown>
assert(response !== undefined, 'The response is undefined, please file a bug report')
if (response.statusCode !== 429) break
await sleep(wait)
Expand Down
Loading