|
| 1 | +/** |
| 2 | + * This script generates markdown files for each endpoint in the |
| 3 | + * configured OpenAPI specs. |
| 4 | + */ |
| 5 | +const { execSync } = require('child_process'); |
| 6 | +const path = require('path'); |
| 7 | +const fs = require('fs'); |
| 8 | +const yaml = require('js-yaml'); |
| 9 | + |
| 10 | +// Calculate the relative paths |
| 11 | +const DOCS_ROOT = process.env.DOCS_ROOT || '.'; |
| 12 | + |
| 13 | +// Function to execute shell commands |
| 14 | +const execCommand = (command) => { |
| 15 | + try { |
| 16 | + console.log(`Executing: ${command}`); |
| 17 | + execSync(command, { stdio: 'inherit' }); |
| 18 | + } catch (error) { |
| 19 | + console.error(`Error executing command: ${command}`); |
| 20 | + process.exit(1); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +const openapiUtils = { |
| 25 | + isPlaceholderFragment: function(str) { |
| 26 | + const placeholderRegex = new RegExp('^\{.*\}$'); |
| 27 | + return placeholderRegex.test(str); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function getPathGroups(openapi) { |
| 32 | + const pathGroups = {}; |
| 33 | + Object.keys(openapi.paths).sort() |
| 34 | + .forEach((p) => { |
| 35 | + const delimiter = '/'; |
| 36 | + let key = p.split(delimiter); |
| 37 | + let isItemPath = openapiUtils.isPlaceholderFragment(key[key.length - 1]); |
| 38 | + if(isItemPath) { |
| 39 | + key = key.slice(0, -1); |
| 40 | + } |
| 41 | + key = (key.slice(0, 4)) |
| 42 | + isItemPath = openapiUtils.isPlaceholderFragment(key[key.length - 1]); |
| 43 | + if(isItemPath) { |
| 44 | + key = key.slice(0, -1); |
| 45 | + } |
| 46 | + const groupKey = key.join('/'); |
| 47 | + pathGroups[groupKey] = pathGroups[groupKey] || {}; |
| 48 | + pathGroups[groupKey][p] = openapi.paths[p]; |
| 49 | + }); |
| 50 | + return pathGroups; |
| 51 | +} |
| 52 | + |
| 53 | +function main() { |
| 54 | + /** |
| 55 | + * Configure the product specs to generate markdown files for. |
| 56 | + */ |
| 57 | + const config = { |
| 58 | + dataDir: path.join(DOCS_ROOT, '/data/api/influxdb'), |
| 59 | + apis: [ |
| 60 | + { |
| 61 | + name: 'cloud-v2', |
| 62 | + menu: 'influxdb_cloud', |
| 63 | + // Source OpenAPI spec file |
| 64 | + spec_file: path.join(DOCS_ROOT, '/api-docs/cloud/v2/ref.yml'), |
| 65 | + // Target content directory for generated endpoint spec pages |
| 66 | + // endpoints_dir: path.join(DOCS_ROOT, '/content/influxdb/cloud/api/v2/yaml'), |
| 67 | + // Target content directory for generated .md pages |
| 68 | + pages_dir: path.join(DOCS_ROOT, '/content/influxdb/cloud/api/v2'), |
| 69 | + }, |
| 70 | + { |
| 71 | + name: 'oss-v2', |
| 72 | + menu: 'influxdb_v2', |
| 73 | + spec_file: path.join(DOCS_ROOT, '/api-docs/v2/ref.yml'), |
| 74 | + // Target content directory for generated endpoint spec pages |
| 75 | + // endpoints_dir: path.join(DOCS_ROOT, '/content/influxdb/v2/api/v2/yaml'), |
| 76 | + // Target content directory for generated .md pages |
| 77 | + pages_dir: path.join(DOCS_ROOT, '/content/influxdb/v2/api/v2'), |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | + |
| 82 | + config.apis.forEach(api => { |
| 83 | + // Execute the getswagger.sh script to fetch and bundle the configured spec. |
| 84 | + execCommand(`${path.join(DOCS_ROOT, '/api-docs/getswagger.sh')} ${api.name} -B`); |
| 85 | + |
| 86 | + // Remove old Hugo pages |
| 87 | + fs.rmSync(api.pages_dir, {recursive: true, force: true}); |
| 88 | + |
| 89 | + spec = yaml.load(fs.readFileSync(api.spec_file)); |
| 90 | + const pathGroups = getPathGroups(spec); |
| 91 | + |
| 92 | + if (!fs.existsSync(api.pages_dir)) { |
| 93 | + fs.mkdirSync(api.pages_dir, { recursive: true }); |
| 94 | + }; |
| 95 | + |
| 96 | + Object.keys(pathGroups).forEach( pathGroup => { |
| 97 | + // Deep copy the spec object |
| 98 | + let pathSpec = JSON.parse(JSON.stringify(spec)); |
| 99 | + pathSpec.paths = pathGroups[pathGroup]; |
| 100 | + pathSpec['x-pathGroupTitle'] = `${pathGroup}\n${spec.info.title}`; |
| 101 | + pathSpec['x-pathGroup'] = pathGroup; |
| 102 | + |
| 103 | + const pageParams = { |
| 104 | + type: 'api', |
| 105 | + title: pathSpec['x-pathGroupTitle'], |
| 106 | + description: pathSpec.info.description, |
| 107 | + api: { |
| 108 | + part_of: api.spec_file, |
| 109 | + spec: JSON.stringify(pathSpec), |
| 110 | + }, |
| 111 | + } |
| 112 | + pageParams.menu = {}; |
| 113 | + pageParams.menu[api.menu] = { |
| 114 | + parent: 'INFLUXDB HTTP API', |
| 115 | + weight: 1, |
| 116 | + name: pathGroup |
| 117 | + }; |
| 118 | + |
| 119 | + let frontMatter = JSON.stringify(pageParams); |
| 120 | + |
| 121 | + const pageName = `${pathGroup.replaceAll('/', '-').replace(/^-/, '')}`; |
| 122 | + const pagePath = path.join(api.pages_dir, `${pageName}.md`); |
| 123 | + fs.writeFileSync(pagePath, frontMatter); |
| 124 | + console.log(`Created: ${pagePath}`); |
| 125 | + }); |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +main(); |
0 commit comments