Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,434 changes: 3,345 additions & 1,089 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/openapi-lint/bin/fresha-openapi-lint.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/usr/bin/env node
require('@fresha/openapi-lint/build/index');
require('@fresha/openapi-lint/cli');
10 changes: 9 additions & 1 deletion packages/openapi-lint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
"name": "@fresha/openapi-lint",
"version": "0.3.1",
"description": "Linting tool for OpenAPI schemas",
"main": "build/index.js",
"bin": {
"fresha-openapi-lint": "./bin/fresha-openapi-lint.js"
},
"main": "build/api.js",
"types": "build/api.d.ts",
"exports": {
"./package.json": "./package.json",
".": "./build/api.js",
"./cli": "./build/cli.js"
},
"publishConfig": {
"access": "public"
},
Expand Down Expand Up @@ -63,9 +69,11 @@
"typescript": "^5.0.2"
},
"dependencies": {
"@fresha/api-tools-core": "^0.6.1",
"@fresha/openapi-model": "^0.8.1",
"chalk": "^4.1.0",
"glob": "^8.0.3",
"yaml": "^2.3.1",
"yargs": "^17.6.2"
},
"gitHead": "5830273395fc060b19d06814b9396ce07eea778d"
Expand Down
40 changes: 0 additions & 40 deletions packages/openapi-lint/src/Linter.ts

This file was deleted.

56 changes: 0 additions & 56 deletions packages/openapi-lint/src/LinterResult.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/openapi-lint/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './types';
export { loadConfigFromFile, createLinter } from './linter';
export { createFormatter } from './formatters';
70 changes: 70 additions & 0 deletions packages/openapi-lint/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import console from 'console';

import { OpenAPIReader, OpenAPIWriter } from '@fresha/openapi-model/build/3.0.3';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import { createFormatter } from './formatters';
import { loadConfigFromFile, createLinter } from './linter';

try {
const argv = yargs(hideBin(process.argv))
.epilog(
'For more information, see https://github.com/fresha/api-tools/tree/main/packages/openapi-lint',
)
.usage('Usage: $0 [OPTIONS] FILES')
.string('config')
.alias('config', 'c')
.describe('config', 'Path to configuration file')
.boolean('print-config')
.describe('print-config', 'Print configuration and exit')
.number('max-warnings')
.describe('max-warnings', 'Number of warnings to trigger nonzero exit code')
.boolean('fix')
.describe('fix', 'Automatically fix problems')
.choices('formatter', ['simple', 'json'])
.default('formatter', 'simple')
.alias('formatter', 'f')
.boolean('verbose')
.alias('verbose', 'v')
.describe('verbose', 'Print more information on console')
.parseSync();

const config = loadConfigFromFile(argv.config ?? '.openapi-lint.yaml');
if (argv.printConfig) {
config.print();
}

const linter = createLinter({
config,
maxWarnings: argv.maxWarnings ?? -1,
autoFix: !!argv.fix,
verbose: !!argv.verbose,
});

for (const elem of argv._) {
const fpath = String(elem);
try {
const openapi = new OpenAPIReader().parseFromFile(fpath);
openapi.setExtension('__filename', fpath);

if (linter.run(openapi)) {
const writer = new OpenAPIWriter();
openapi.deleteExtension('__filename');
writer.writeToFile(openapi, fpath);
}
} catch (err) {
if (err instanceof Error) {
console.log(err instanceof Error ? err.message : err);
}
}
}

const formatter = createFormatter(argv.formatter);
formatter.format(linter.result);

process.exit(linter.result.isFailure ? 1 : 0);
} catch (err) {
console.log(err instanceof Error ? err.message : err);
process.exit(1);
}
11 changes: 11 additions & 0 deletions packages/openapi-lint/src/formatters/JSONFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import console from 'console';

import type { Formatter, Result } from '../types';

export class JSONFormatter implements Formatter {
// eslint-disable-next-line class-methods-use-this
format(result: Result): void {
const json = JSON.stringify(result, null, 2);
console.log(json);
}
}
45 changes: 45 additions & 0 deletions packages/openapi-lint/src/formatters/SimpleFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import console from 'console';

import chalk from 'chalk';

import type { Severity, Formatter, Issue, Result } from '../types';

export class SimpleFormatter implements Formatter {
// eslint-disable-next-line class-methods-use-this
format(result: Result): void {
const issuesPerFile = new Map<string, Issue[]>();
const severityCount = new Map<Severity, number>();

for (const issue of result.issues()) {
issuesPerFile.set(issue.file, [...(issuesPerFile.get(issue.file) ?? []), issue]);
severityCount.set(issue.severity, (severityCount.get(issue.severity) ?? 0) + 1);
}

const fileNames = Array.from(issuesPerFile.keys()).sort();
for (const fileName of fileNames) {
console.log(fileName);
for (const item of issuesPerFile.get(fileName)!) {
let str = ' ';
if (item.severity === 'error') {
str += chalk.red('[error] ');
} else if (item.severity === 'warning') {
str += chalk.yellow('[warn] ');
}
str += item.message;
console.log(str);
}
}
if (result.issueCount > 0) {
let str = 'Summary: ';
const errorCount = severityCount.get('error') ?? 0;
if (errorCount > 0) {
str += chalk.red(`${errorCount} errors`);
}
const warningCount = severityCount.get('warning') ?? 0;
if (warningCount > 0) {
str += chalk.red(`${warningCount} warnings`);
}
console.log(str);
}
}
}
15 changes: 15 additions & 0 deletions packages/openapi-lint/src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { JSONFormatter } from './JSONFormatter';
import { SimpleFormatter } from './SimpleFormatter';

import type { Formatter } from '../types';

export const createFormatter = (format: string): Formatter => {
switch (format) {
case 'json':
return new JSONFormatter();
case 'simple':
return new SimpleFormatter();
default:
throw new Error(`Unknown format: ${format}`);
}
};
3 changes: 0 additions & 3 deletions packages/openapi-lint/src/index.test.ts

This file was deleted.

49 changes: 0 additions & 49 deletions packages/openapi-lint/src/index.ts

This file was deleted.

Loading