Skip to content

Commit 5cdb402

Browse files
committed
reuse output extensions
1 parent d274ac9 commit 5cdb402

File tree

4 files changed

+11
-22
lines changed

4 files changed

+11
-22
lines changed

packages/cli/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BundleOutputFormat, RuleSeverity } from '@redocly/openapi-core';
1+
import type { RuleSeverity } from '@redocly/openapi-core';
22
import type { RespectOptions, GenerateArazzoFileOptions } from '@redocly/respect-core';
33
import type { LintOptions } from './commands/lint.js';
44
import type { BundleOptions } from './commands/bundle.js';
@@ -23,8 +23,8 @@ export type Entrypoint = {
2323
alias?: string;
2424
output?: string;
2525
};
26-
export const outputExtensions = ['json', 'yaml', 'yml'] as ReadonlyArray<BundleOutputFormat>; // FIXME: use one source of truth (2.0)
27-
export type OutputExtensions = 'json' | 'yaml' | 'yml' | undefined; // FIXME: use one source of truth (2.0)
26+
export const outputExtensions = ['json', 'yaml', 'yml'] as const;
27+
export type OutputExtensions = typeof outputExtensions[number];
2828
export type CommandOptions =
2929
| StatsOptions
3030
| SplitOptions

packages/cli/src/utils/miscellaneous.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ import { outputExtensions } from '../types.js';
2828
import { exitWithError } from './error.js';
2929
import { handleLintConfig } from '../commands/lint.js';
3030

31-
import type {
32-
Config,
33-
BundleOutputFormat,
34-
Oas3Definition,
35-
Oas2Definition,
36-
Exact,
37-
} from '@redocly/openapi-core';
31+
import type { Config, Oas3Definition, Oas2Definition, Exact } from '@redocly/openapi-core';
3832
import type { Totals, Entrypoint, OutputExtensions, CommandOptions } from '../types.js';
3933

4034
const globPromise = promisify(glob.glob);
@@ -185,7 +179,7 @@ export class CircularJSONNotSupportedError extends Error {
185179
}
186180
}
187181

188-
export function dumpBundle(obj: any, format: BundleOutputFormat, dereference?: boolean): string {
182+
export function dumpBundle(obj: any, format: OutputExtensions, dereference?: boolean): string {
189183
if (format === 'json') {
190184
try {
191185
return JSON.stringify(obj, null, 2);
@@ -279,10 +273,8 @@ export function writeJson(data: unknown, filename: string) {
279273

280274
export function getAndValidateFileExtension(fileName: string): NonNullable<OutputExtensions> {
281275
const ext = fileName.split('.').pop();
282-
283-
if (['yaml', 'yml', 'json'].includes(ext!)) {
284-
// FIXME: ^ use one source of truth (2.0)
285-
return ext as NonNullable<OutputExtensions>;
276+
if (outputExtensions.includes(ext as OutputExtensions)) {
277+
return ext as OutputExtensions;
286278
}
287279
logger.warn(`Unsupported file extension: ${ext}. Using yaml.\n`);
288280
return 'yaml';
@@ -377,7 +369,7 @@ export function getOutputFileName({
377369
entrypoint: string;
378370
output?: string;
379371
argvOutput?: string;
380-
ext?: BundleOutputFormat;
372+
ext?: OutputExtensions;
381373
entries: number;
382374
}) {
383375
let outputFile = output || argvOutput;
@@ -386,16 +378,16 @@ export function getOutputFileName({
386378
}
387379

388380
if (entries > 1 && argvOutput) {
389-
ext = ext || (extname(entrypoint).substring(1) as BundleOutputFormat);
381+
ext = ext || (extname(entrypoint).substring(1) as OutputExtensions);
390382
if (!outputExtensions.includes(ext)) {
391383
throw new Error(`Invalid file extension: ${ext}.`);
392384
}
393385
outputFile = join(argvOutput, basename(entrypoint, extname(entrypoint))) + '.' + ext;
394386
} else {
395387
ext =
396388
ext ||
397-
(extname(outputFile).substring(1) as BundleOutputFormat) ||
398-
(extname(entrypoint).substring(1) as BundleOutputFormat);
389+
(extname(outputFile).substring(1) as OutputExtensions) ||
390+
(extname(entrypoint).substring(1) as OutputExtensions);
399391
if (!outputExtensions.includes(ext)) {
400392
throw new Error(`Invalid file extension: ${ext}.`);
401393
}

packages/core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export {
2-
type BundleOutputFormat,
32
type CollectFn,
43
type Exact,
54
keysOf,

packages/core/src/utils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export function popStack<T, P extends Stack<T>>(head: P) {
3232
return head?.prev ?? null;
3333
}
3434

35-
export type BundleOutputFormat = 'json' | 'yml' | 'yaml'; // FIXME: use one source of truth (2.0)
36-
3735
export async function loadYaml<T>(filename: string): Promise<T> {
3836
const contents = await fs.promises.readFile(filename, 'utf-8');
3937
return parseYaml(contents) as T;

0 commit comments

Comments
 (0)