|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +const { join } = require('path') |
| 21 | +const { writeFile } = require('fs/promises') |
| 22 | +const fetch = require('node-fetch') |
| 23 | +const rimraf = require('rimraf') |
| 24 | +const ora = require('ora') |
| 25 | +const { convertRequests } = require('@elastic/request-converter') |
| 26 | +const minimist = require('minimist') |
| 27 | + |
| 28 | +const docsExamplesDir = join(__dirname, '../../docs', 'examples') |
| 29 | + |
| 30 | +const log = ora('Generating example snippets') |
| 31 | + |
| 32 | +const failures = {} |
| 33 | + |
| 34 | +async function getAlternativesReport (version = 'master') { |
| 35 | + const reportUrl = `https://raw.githubusercontent.com/elastic/built-docs/master/raw/en/elasticsearch/reference/${version}/alternatives_report.json` |
| 36 | + const response = await fetch(reportUrl) |
| 37 | + if (!response.ok) { |
| 38 | + log.fail(`unexpected response ${response.statusText}`) |
| 39 | + process.exit(1) |
| 40 | + } |
| 41 | + return await response.json() |
| 42 | +} |
| 43 | + |
| 44 | +async function makeSnippet (example) { |
| 45 | + const { source, digest } = example |
| 46 | + const fileName = `${digest}.asciidoc` |
| 47 | + const filePath = join(docsExamplesDir, fileName) |
| 48 | + |
| 49 | + try { |
| 50 | + const code = await convertRequests(source, 'python', { |
| 51 | + complete: false, |
| 52 | + printResponse: true |
| 53 | + }) |
| 54 | + await writeFile(filePath, asciidocWrapper(code, example), 'utf8') |
| 55 | + } catch (err) { |
| 56 | + failures[digest] = err.message |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +async function generate (version) { |
| 61 | + log.start() |
| 62 | + |
| 63 | + rimraf.sync(join(docsExamplesDir, '*')) |
| 64 | + |
| 65 | + log.text = `Downloading alternatives report for version ${version}` |
| 66 | + const examples = await getAlternativesReport(version) |
| 67 | + |
| 68 | + let counter = 1 |
| 69 | + for (const example of examples) { |
| 70 | + log.text = `${counter++}/${examples.length}: ${example.digest}` |
| 71 | + |
| 72 | + // skip over bad request definitions |
| 73 | + if (example.source.startsWith('{') || example.source.endsWith('...')) { |
| 74 | + failures[example.digest] = 'Incomplete request syntax' |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + await makeSnippet(example) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function asciidocWrapper (source, example) { |
| 83 | + return `// This file is autogenerated, DO NOT EDIT |
| 84 | +// ${example.source_location.file}:${example.source_location.line} |
| 85 | +
|
| 86 | +[source, python] |
| 87 | +---- |
| 88 | +${source.trim()} |
| 89 | +---- |
| 90 | +` |
| 91 | +} |
| 92 | + |
| 93 | +const options = minimist(process.argv.slice(2), { |
| 94 | + string: ['version'], |
| 95 | + default: { |
| 96 | + version: 'master' |
| 97 | + } |
| 98 | +}) |
| 99 | + |
| 100 | +generate(options.version) |
| 101 | + .then(() => log.succeed('done!')) |
| 102 | + .catch(err => log.fail(err.message)) |
| 103 | + .finally(() => { |
| 104 | + const keys = Object.keys(failures) |
| 105 | + if (keys.length > 0) { |
| 106 | + let message = 'Some examples failed to generate:\n\n' |
| 107 | + for (const key of keys) { |
| 108 | + message += `${key}: ${failures[key]}\n` |
| 109 | + } |
| 110 | + console.error(message) |
| 111 | + } |
| 112 | + }) |
0 commit comments