Skip to content
Snippets Groups Projects

Switch to currently supported redocly cli

Merged Martin Lowe requested to merge malowe/main/redocly-update into main
Files
5
+ 38
35
@@ -10,13 +10,15 @@
SPDX-License-Identifier: EPL-2.0
******************************************************************/
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const path = require('path');
const fs = require('fs');
const process = require('process');
const yaml = require('yaml');
import util from 'util';
import path from 'path';
import { exec as execProcess } from 'child_process';
import fs from 'fs';
import process from 'process';
import yaml from 'yaml';
import pRetry from 'p-retry';
const exec = util.promisify(execProcess);
// matches web URLs
const URL_REGEX = new RegExp('^https?://(?:[^./]+.){1,3}.[a-z]{2,3}(/[^/]+)*.[a-z]{3,4}?$');
// location where to save temp files for the run
@@ -63,7 +65,7 @@ async function processSpec(specDisplayName, specMachineName) {
console.log(`Processing openapi spec for ${specDisplayName}...`);
// Go to the location where the spec is, and continue processing
await exec(
`npx redoc-cli build -o "..${path.sep}content${path.sep}${specMachineName}${path.sep}index.html" "${specMachineName}${path.sep}openapi.yaml"`
`npx @redocly/cli build-docs --output="..${path.sep}content${path.sep}${specMachineName}${path.sep}index.html" "${specMachineName}${path.sep}openapi.yaml"`
);
console.log(`Done processing ${specDisplayName}`);
}
@@ -85,36 +87,37 @@ async function run() {
// Create the data holder for the generated repo data definitions
const reposDefinition = yaml.parse(fs.readFileSync('../repos.yaml', 'utf8'));
await Promise.all(
reposDefinition['specs'].map(async currentSpecDefinition => {
// get the variables from the yml definition of the spec
const specMachineName = currentSpecDefinition['name'];
const specDisplayName = currentSpecDefinition['displayName'] || currentSpecDefinition['name'];
const specLocation = currentSpecDefinition['location'];
const potentialLocalSpecLocation = `${initialProjectDirectory}${path.sep}${specLocation}`;
const outputSpecLocation = `${specMachineName}${path.sep}openapi.yaml`;
for (const currentSpecDefinition of reposDefinition['specs']) {
// get the variables from the yml definition of the spec
const specMachineName = currentSpecDefinition['name'];
const specDisplayName = currentSpecDefinition['displayName'] || currentSpecDefinition['name'];
const specLocation = currentSpecDefinition['location'];
const potentialLocalSpecLocation = `${initialProjectDirectory}${path.sep}${specLocation}`;
const outputSpecLocation = `${specMachineName}${path.sep}openapi.yaml`;
// Do prep for spec by creating folder in tmp dir to hold processed files
fs.mkdirSync(specMachineName, { recursive: true });
// Do prep for spec by creating folder in tmp dir to hold processed files
fs.mkdirSync(specMachineName, { recursive: true });
console.log(`Retrieving openapi spec for ${specDisplayName}...`);
// prepare the spec, fetching remote specs or copying the spec for processing
if (isExternalSpec(specLocation)) {
await pullExternalSpec(specLocation, outputSpecLocation);
} else {
fs.copyFileSync(potentialLocalSpecLocation, outputSpecLocation);
console.log(`Copied local spec for ${specDisplayName} to output dir`);
}
// process the spec, setting the information about the spec into the output data var
await processSpec(specDisplayName, specMachineName).then(() => {
console.log(`Retrieving openapi spec for ${specDisplayName}...`);
// prepare the spec, fetching remote specs or copying the spec for processing
if (isExternalSpec(specLocation)) {
await pullExternalSpec(specLocation, outputSpecLocation);
} else {
fs.copyFileSync(potentialLocalSpecLocation, outputSpecLocation);
console.log(`Copied local spec for ${specDisplayName} to output dir`);
}
// process the spec, setting the information about the spec into the output data var
await pRetry(async () => processSpec(specDisplayName, specMachineName).then(() => {
artificialSpecData['items'].push({ name: specMachineName, displayName: specDisplayName });
}).then(() => {
console.log('Outputing generated spec data for site render');
// Create the data dir if it doesn't exist
fs.mkdirSync('../data', { recursive: true });
// Dump the generated spec list to the data file for hugo build
fs.writeFileSync('../data/specs.yaml', yaml.stringify(artificialSpecData));
}).catch(result => result.reject('Failed to fetch the resource')), {
retries: 3,
onFailedAttempt: error => console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`)
});
})
).then(() => {
console.log('Outputing generated spec data for site render');
// Create the data dir if it doesn't exist
fs.mkdirSync('../data', { recursive: true });
// Dump the generated spec list to the data file for hugo build
fs.writeFileSync('../data/specs.yaml', yaml.stringify(artificialSpecData));
});
};
}
Loading