Skip to content

Commit

Permalink
feat(writers): header handling directly return string or eslint-disable
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Apr 2, 2022
1 parent 9856d94 commit df688ea
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/core/writers/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { log } from 'console';
import execa from 'execa';
import { appendFile, outputFile, pathExists, readFile } from 'fs-extra';
import uniq from 'lodash.uniq';
import { InfoObject } from 'openapi3-ts';
import { join } from 'upath';
import { NormalizedOptions, OutputMode } from '../../types';
import { WriteSpecsProps } from '../../types/writers';
Expand All @@ -16,6 +17,19 @@ import { writeSplitMode } from './splitMode';
import { writeSplitTagsMode } from './splitTagsMode';
import { writeTagsMode } from './tagsMode';

const getHeader = (
option: false | ((info: InfoObject) => string | string[]),
info: InfoObject,
): string => {
if (!option) {
return '';
}

const header = option(info);

return Array.isArray(header) ? jsDoc({ description: header }) : header;
};

export const writeSpecs = async (
{ operations, schemas, target, info }: WriteSpecsProps,
workspace: string,
Expand All @@ -34,11 +48,7 @@ export const writeSpecs = async (
return acc;
}, {} as Record<keyof typeof schemas, string>);

const header = output.override.header
? jsDoc({
description: output.override.header(info),
})
: '';
const header = getHeader(output.override.header, info);

if (output.schemas) {
const rootSchemaPath = output.schemas;
Expand Down
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type NormalizedOverrideOutput = {
required?: boolean;
baseUrl?: string;
};
header: false | ((info: InfoObject) => string[]);
header: false | ((info: InfoObject) => string[] | string);
formData: boolean | NormalizedMutator;
formUrlEncoded: boolean | NormalizedMutator;
components: {
Expand Down Expand Up @@ -210,7 +210,7 @@ export type OverrideOutput = {
required?: boolean;
baseUrl?: string;
};
header?: boolean | ((info: InfoObject) => string[]);
header?: boolean | ((info: InfoObject) => string[] | string);
formData?: boolean | Mutator;
formUrlEncoded?: boolean | Mutator;
components?: {
Expand Down
11 changes: 9 additions & 2 deletions src/utils/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export function jsDoc(
): string {
// Ensure there aren't any comment terminations in doc
const lines = (
Array.isArray(description) ? description : [description || '']
Array.isArray(description)
? description.filter((d) => !d.includes('eslint-disable'))
: [description || '']
).map((line) => line.replace(regex, replacement));

const count = [description, deprecated, summary].reduce(
Expand All @@ -30,7 +32,12 @@ export function jsDoc(
}

const oneLine = count === 1 && tryOneLine;
let doc = '/**';
const eslintDisable = Array.isArray(description)
? description
.find((d) => d.includes('eslint-disable'))
?.replace(regex, replacement)
: undefined;
let doc = `${eslintDisable ? `/* ${eslintDisable} */\n` : ''}/**`;

if (description) {
if (!oneLine) {
Expand Down

1 comment on commit df688ea

@vercel
Copy link

@vercel vercel bot commented on df688ea Apr 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.