|
| 1 | +import path from 'path'; |
1 | 2 | import jsYaml from 'js-yaml'; |
| 3 | +import callerCallsite from 'caller-callsite'; |
| 4 | +import chalk from 'chalk'; |
| 5 | + |
| 6 | +// @ts-ignore |
| 7 | +import { Linter } from 'eslint'; |
| 8 | +// @ts-ignore |
| 9 | +import openapiEslintPlugin from 'eslint-plugin-openapi-jsdoc'; |
2 | 10 |
|
3 | 11 | import parseFile from './util/parseFile'; |
4 | 12 | import globList from './util/globList'; |
5 | 13 | import SpecBuilder from './SpecBuilder'; |
6 | | -import { ParserOptions, OpenApiObject } from './exported'; |
| 14 | +import { ParserOptions, OpenApiObject, BaseDefinition } from './exported'; |
7 | 15 | import yamlLOC from './util/yamlLOC'; |
| 16 | +import loadDefinition from './util/loadDefinition'; |
| 17 | +import formatter from './util/formatter'; |
8 | 18 |
|
9 | | -function parseComments({ |
10 | | - definition, |
11 | | - paths, |
12 | | - verbose = true, |
13 | | -}: ParserOptions): OpenApiObject { |
14 | | - if (!definition && !paths) { |
15 | | - throw new Error('Provided options are incorrect.'); |
| 19 | +function getCallerPath() { |
| 20 | + const filename = callerCallsite()?.getFileName(); |
| 21 | + if (filename) { |
| 22 | + return path.dirname(filename); |
16 | 23 | } |
| 24 | + return ''; |
| 25 | +} |
| 26 | + |
| 27 | +function parseComments( |
| 28 | + definition: BaseDefinition | string, |
| 29 | + { |
| 30 | + root = getCallerPath(), |
| 31 | + extension = ['.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.yaml', '.yml'], |
| 32 | + include = ['**'], |
| 33 | + exclude = [ |
| 34 | + 'coverage/**', |
| 35 | + 'packages/*/test{,s}/**', |
| 36 | + '**/*.d.ts', |
| 37 | + 'test{,s}/**', |
| 38 | + `test{,-*}.{js,cjs,mjs,ts,tsx,jsx,yaml,yml}`, |
| 39 | + `**/*{.,-}test.{js,cjs,mjs,ts,tsx,jsx,yaml,yml}`, |
| 40 | + '**/__tests__/**', |
17 | 41 |
|
18 | | - const spec = new SpecBuilder(definition); |
| 42 | + /* Exclude common development tool configuration files */ |
| 43 | + '**/{ava,babel,nyc}.config.{js,cjs,mjs}', |
| 44 | + '**/jest.config.{js,cjs,mjs,ts}', |
| 45 | + '**/{karma,rollup,webpack}.config.js', |
| 46 | + '**/.{eslint,mocha}rc.{js,cjs}', |
19 | 47 |
|
20 | | - const files = globList(paths); |
| 48 | + // always ignore '**/node_modules/**' |
| 49 | + ], |
| 50 | + excludeNodeModules = true, |
| 51 | + verbose = true, |
| 52 | + }: ParserOptions = {} |
| 53 | +): OpenApiObject { |
| 54 | + if (!definition) { |
| 55 | + throw new Error('A base OpenAPI definition is required.'); |
| 56 | + } |
| 57 | + |
| 58 | + let definitionObject: BaseDefinition; |
| 59 | + if (typeof definition === 'string') { |
| 60 | + definitionObject = loadDefinition(definition); |
| 61 | + } else { |
| 62 | + definitionObject = definition; |
| 63 | + } |
| 64 | + |
| 65 | + const spec = new SpecBuilder(definitionObject); |
| 66 | + |
| 67 | + const files = globList(root, extension, include, exclude, excludeNodeModules); |
| 68 | + |
| 69 | + const linter = new Linter(); |
| 70 | + linter.defineRules({ |
| 71 | + ...openapiEslintPlugin.rules, |
| 72 | + }); |
21 | 73 |
|
22 | 74 | let totalLOC = 0; |
| 75 | + let allMessages: any[] = []; |
23 | 76 | files.forEach((file) => { |
24 | | - const parsedFile = parseFile(file, verbose); |
| 77 | + const { parsedFile, messages } = parseFile(file, linter, verbose); |
| 78 | + allMessages.push({ filePath: file, messages: messages }); |
25 | 79 | const specs = parsedFile.map((item) => item.spec); |
26 | 80 | const loc = parsedFile.reduce((acc, cur) => (acc += cur.loc), 0); |
27 | 81 | totalLOC += loc; |
28 | 82 | spec.addData(specs); |
29 | 83 | }); |
30 | 84 |
|
31 | 85 | if (verbose) { |
32 | | - const specAsYaml = jsYaml.safeDump(JSON.parse(JSON.stringify(spec))); |
33 | | - const originalLOC = yamlLOC(specAsYaml); |
| 86 | + const errorTable = formatter(allMessages); |
| 87 | + if (errorTable) { |
| 88 | + console.log(errorTable); |
| 89 | + } |
| 90 | + |
| 91 | + // Only measure paths and components. |
| 92 | + let pathsAsYaml = ''; |
| 93 | + if (spec.paths) { |
| 94 | + pathsAsYaml = jsYaml.safeDump(JSON.parse(JSON.stringify(spec.paths))); |
| 95 | + } |
| 96 | + let componentAsYaml = ''; |
| 97 | + if (spec.components) { |
| 98 | + componentAsYaml = jsYaml.safeDump( |
| 99 | + JSON.parse(JSON.stringify(spec.components)) |
| 100 | + ); |
| 101 | + } |
| 102 | + const originalLOC = yamlLOC(pathsAsYaml) + yamlLOC(componentAsYaml); |
34 | 103 | const locDiff = originalLOC - totalLOC; |
35 | 104 | const savings = (locDiff / originalLOC) * 100; |
36 | | - console.log( |
37 | | - `✨ You've saved ${locDiff} lines of extra YAML (${savings.toFixed(1)}%)` |
38 | | - ); |
| 105 | + if (locDiff > 0) { |
| 106 | + console.log(); |
| 107 | + console.log( |
| 108 | + `✨ You've saved ${chalk.bold( |
| 109 | + locDiff |
| 110 | + )} lines of extra YAML (${chalk.green.bold( |
| 111 | + `\u25BC ${savings.toFixed(1)}%` |
| 112 | + )})` |
| 113 | + ); |
| 114 | + console.log(); |
| 115 | + } |
39 | 116 | } |
40 | 117 |
|
41 | 118 | return spec; |
|
0 commit comments