UNPKG

@redocly/cli

Version:

[@Redocly](https://redocly.com) CLI is your all-in-one API documentation utility. It builds, manages, improves, and quality-checks your API descriptions, all of which comes in handy for various phases of the API Lifecycle. Create your own rulesets to make

108 lines 5.09 kB
import { HandledError, logger } from '@redocly/openapi-core'; import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, basename } from 'node:path'; import { blue, green } from 'colorette'; import { composeJsonLogsFiles } from './json-logs.js'; import { displayFilesSummaryTable } from './display-files-summary-table.js'; import { readEnvVariables } from '../../utils/read-env-variables.js'; import { resolveMtlsCertificates } from './mtls/resolve-mtls-certificates.js'; import { withConnectionClient } from './connection-client.js'; import { withHar } from './har-logs/index.js'; import { createHarLog } from './har-logs/har-logs.js'; import { jsonStringifyWithArrayBuffer } from '../../utils/json-stringify-with-array-buffer.js'; export async function handleRespect({ argv, config, version, collectSpecData, }) { let mtlsCerts; let harLogs; try { const { run, conditionallyMaskSecrets } = await import('@redocly/respect-core'); const workingDir = config.configPath ? dirname(config.configPath) : process.cwd(); if (argv.mtls) { mtlsCerts = resolveMtlsCertificates(argv.mtls, workingDir); } let customFetch = withConnectionClient(mtlsCerts); if (argv['har-output']) { harLogs = createHarLog({ version }); customFetch = withHar(customFetch, { har: harLogs }); } const options = { files: argv.files, input: argv.input, server: argv.server, workflow: argv.workflow, skip: argv.skip, verbose: argv.verbose, config, version, collectSpecData, severity: argv.severity, maxSteps: argv['max-steps'], maxFetchTimeout: argv['max-fetch-timeout'], executionTimeout: argv['execution-timeout'], requestFileLoader: { getFileBody: async (filePath) => { if (!existsSync(filePath)) { throw new Error(`File ${filePath} doesn't exist or isn't readable.`); } const buffer = readFileSync(filePath); return new File([buffer], basename(filePath)); }, }, envVariables: readEnvVariables(workingDir) || {}, logger, fetch: customFetch, noSecretsMasking: argv['no-secrets-masking'], }; if (options.skip && options.workflow) { throw new Error(`Cannot use both --skip and --workflow flags at the same time.`); } if (argv['har-output'] && !argv['har-output'].endsWith('.har')) { throw new Error('File for HAR logs should be in .har format'); } if (argv['json-output'] && !argv['json-output'].endsWith('.json')) { throw new Error('File for JSON logs should be in .json format'); } if (options.files.length > 1 && argv['har-output']) { // TODO: implement multiple run files HAR output throw new Error('Currently only a single file can be run with --har-output. Please run a single file at a time.'); } const startedAt = performance.now(); const runAllFilesResult = await run(options); logger.printNewLine(); displayFilesSummaryTable(runAllFilesResult, logger); logger.printNewLine(); const hasProblems = runAllFilesResult.some((result) => result.hasProblems); const hasWarnings = runAllFilesResult.some((result) => result.hasWarnings); if (argv['json-output']) { const jsonOutputData = { files: composeJsonLogsFiles(runAllFilesResult), status: hasProblems ? 'error' : hasWarnings ? 'warn' : 'success', totalTime: performance.now() - startedAt, }; writeFileSync(argv['json-output'], jsonStringifyWithArrayBuffer(jsonOutputData, 2), 'utf-8'); logger.output(blue(logger.indent(`JSON logs saved in ${green(argv['json-output'])}`, 2))); logger.printNewLine(); logger.printNewLine(); } if (argv['har-output']) { // TODO: implement multiple run files HAR output for (const result of runAllFilesResult) { const parsedHarLogs = conditionallyMaskSecrets({ value: harLogs, noSecretsMasking: result.ctx.noSecretsMasking, secretsSet: result.ctx.secretsSet || new Set(), }); writeFileSync(argv['har-output'], jsonStringifyWithArrayBuffer(parsedHarLogs, 2), 'utf-8'); logger.output(blue(`Har logs saved in ${green(argv['har-output'])}`)); logger.printNewLine(); logger.printNewLine(); } } if (hasProblems) { throw new Error(' Tests exited with error '); } } catch (error) { throw new HandledError(error?.message ?? error); } } //# sourceMappingURL=index.js.map