bump-cli
Version:
The Bump CLI is used to interact with your API documentation hosted on Bump.sh by using the API of developers.bump.sh
54 lines (52 loc) • 2.05 kB
JavaScript
import { ux } from '@oclif/core';
import chalk from 'chalk';
import { existsSync } from 'node:fs';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { fileArg, overlayFileArg } from '../args.js';
import { BaseCommand } from '../base-command.js';
import { confirm as promptConfirm } from '../core/utils/prompts.js';
import { API } from '../definition.js';
import * as flagsBuilder from '../flags.js';
export default class Overlay extends BaseCommand {
static args = {
file: fileArg,
overlay: overlayFileArg,
};
static description = 'Apply an OpenAPI specified overlay to your API definition.';
static examples = [
`Apply the OVERLAY_FILE to the existing DEFINITION_FILE. The resulting
definition is output on stdout meaning you can redirect it to a new
file.
${chalk.dim('$ bump overlay DEFINITION_FILE OVERLAY_FILE > destination/file.json')}
* Let's apply the overlay to the main definition... done
`,
];
static flags = {
out: flagsBuilder.out(),
};
async run() {
const { args, flags } = await this.parse(Overlay);
const outputPath = flags.out;
const { file, overlay } = args;
ux.action.start("* Let's apply the overlay to the main definition");
ux.action.status = '...loading definition file';
const api = await API.load(file);
ux.action.status = '...applying overlay';
const [overlayedDefinition] = await api.extractDefinition(outputPath, [overlay]);
ux.action.stop();
if (outputPath) {
await mkdir(dirname(outputPath), { recursive: true });
let confirm = true;
if (existsSync(outputPath)) {
confirm = await promptConfirm(`Do you want to override the existing destination file? (${outputPath})`);
}
if (confirm) {
await writeFile(outputPath, overlayedDefinition);
}
}
else {
ux.stdout(overlayedDefinition);
}
}
}