-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: dblock <[email protected]>
- Loading branch information
Showing
10 changed files
with
3,482 additions
and
534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ jobs: | |
node-version: 20.10.0 | ||
- run: npm install | ||
- run: npm run test | ||
- run: npm run lint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
# OpenSearch OpenAPI Tools | ||
|
||
This folder contains tools for the repo: | ||
- Merger: merges multiple OpenAPI files into one | ||
- Linter: validates files in the spec folder | ||
|
||
- [Merger](./merger/): merges multiple OpenAPI files into one | ||
- [Linter](./linter/): validates files in the spec folder | ||
|
||
## Setup | ||
|
||
1. Install [Node.js](https://nodejs.org/en/learn/getting-started/how-to-install-nodejs) | ||
2. Run `npm install` in the `tools` folder | ||
|
||
## Merger | ||
|
||
The merger tool merges the multi-file OpenSearch spec into a single file for programmatic use. It takes 2 parameters: | ||
- The path to the root folder of the multi-file spec | ||
- The path to the output file | ||
|
||
- the path to the root folder of the multi-file spec | ||
- the path to the output file | ||
|
||
Example: | ||
|
||
```bash | ||
npm run merge -- ../spec ../build/opensearch-openapi.latest.yaml | ||
``` | ||
|
||
## Linter | ||
|
||
The linter tool validates the OpenSearch spec files in the `spec` folder: | ||
|
||
```bash | ||
npm run lint | ||
npm run lint:spec | ||
``` | ||
|
||
It will print out all the errors and warnings in the spec files. This tool in still in development, and it will be integrated into the CI/CD pipeline and run automatically with every PR. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
import { FlatCompat } from '@eslint/eslintrc' | ||
import pluginJs from '@eslint/js' | ||
|
||
// mimic CommonJS variables -- not needed if using CommonJS | ||
const _filename = fileURLToPath(import.meta.url) | ||
const _dirname = path.dirname(_filename) | ||
const compat = new FlatCompat({ baseDirectory: _dirname, recommendedConfig: pluginJs.configs.recommended }) | ||
|
||
export default [ | ||
pluginJs.configs.recommended, | ||
...compat.extends('standard-with-typescript'), | ||
{ | ||
files: ['**/*.{js,ts}'], | ||
ignores: [ | ||
'**/eslint.config.mjs' | ||
], | ||
rules: { | ||
'@typescript-eslint/array-type': 'warn', | ||
'@typescript-eslint/block-spacing': 'warn', | ||
'@typescript-eslint/comma-dangle': 'warn', | ||
'@typescript-eslint/comma-spacing': 'warn', | ||
'@typescript-eslint/consistent-indexed-object-style': 'warn', | ||
'@typescript-eslint/consistent-type-assertions': 'warn', | ||
'@typescript-eslint/consistent-type-imports': 'warn', | ||
'@typescript-eslint/dot-notation': 'warn', | ||
'@typescript-eslint/explicit-function-return-type': 'warn', | ||
'@typescript-eslint/indent': 'warn', | ||
'@typescript-eslint/keyword-spacing': 'warn', | ||
'@typescript-eslint/lines-between-class-members': 'warn', | ||
'@typescript-eslint/member-delimiter-style': 'warn', | ||
'@typescript-eslint/naming-convention': 'warn', | ||
'@typescript-eslint/no-confusing-void-expression': 'warn', | ||
'@typescript-eslint/no-dynamic-delete': 'warn', | ||
'@typescript-eslint/no-invalid-void-type': 'warn', | ||
'@typescript-eslint/no-non-null-assertion': 'warn', | ||
'@typescript-eslint/no-unnecessary-type-assertion': 'warn', | ||
'@typescript-eslint/no-unsafe-argument': 'warn', | ||
'@typescript-eslint/no-unused-vars': 'warn', | ||
'@typescript-eslint/object-curly-spacing': 'warn', | ||
'@typescript-eslint/prefer-nullish-coalescing': 'warn', | ||
'@typescript-eslint/quotes': 'warn', | ||
'@typescript-eslint/require-array-sort-compare': 'warn', | ||
'@typescript-eslint/semi': 'warn', | ||
'@typescript-eslint/space-before-blocks': 'warn', | ||
'@typescript-eslint/space-before-function-paren': 'warn', | ||
'@typescript-eslint/strict-boolean-expressions': 'warn', | ||
'@typescript-eslint/type-annotation-spacing': 'warn', | ||
'array-bracket-spacing': 'warn', | ||
'array-callback-return': 'warn', | ||
curly: 'warn', | ||
'eol-last': 'warn', | ||
eqeqeq: 'warn', | ||
'new-cap': 'warn', | ||
'no-multi-spaces': 'warn', | ||
'no-multiple-empty-lines': 'warn', | ||
'no-return-assign': 'warn', | ||
'no-useless-return': 'warn', | ||
'object-curly-newline': 'warn', | ||
'object-property-newline': 'warn', | ||
'object-shorthand': 'warn', | ||
'quote-props': 'warn', | ||
'space-in-parens': 'warn' | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
import fs from "fs"; | ||
import YAML from "yaml"; | ||
import _ from "lodash"; | ||
import fs from 'fs' | ||
import YAML from 'yaml' | ||
import _ from 'lodash' | ||
|
||
export function resolve(ref: string, root: Record<string, any>) { | ||
const paths = ref.replace('#/', '').split('/'); | ||
for(const p of paths) { | ||
root = root[p]; | ||
if(root === undefined) break; | ||
} | ||
return root; | ||
export function resolve (ref: string, root: Record<string, any>) { | ||
const paths = ref.replace('#/', '').split('/') | ||
for (const p of paths) { | ||
root = root[p] | ||
if (root === undefined) break | ||
} | ||
return root | ||
} | ||
|
||
export function sortByKey(obj: Record<string, any>, priorities: string[] = []) { | ||
const orders = _.fromPairs(priorities.map((k, i) => [k, i+1])); | ||
const sorted = _.entries(obj).sort((a,b) => { | ||
const order_a = orders[a[0]]; | ||
const order_b = orders[b[0]]; | ||
if(order_a && order_b) return order_a - order_b; | ||
if(order_a) return 1; | ||
if(order_b) return -1; | ||
return a[0].localeCompare(b[0]); | ||
}); | ||
sorted.forEach(([k, v]) => { | ||
delete obj[k]; | ||
obj[k] = v; | ||
}); | ||
export function sortByKey (obj: Record<string, any>, priorities: string[] = []) { | ||
const orders = _.fromPairs(priorities.map((k, i) => [k, i + 1])) | ||
const sorted = _.entries(obj).sort((a, b) => { | ||
const order_a = orders[a[0]] | ||
const order_b = orders[b[0]] | ||
if (order_a && order_b) return order_a - order_b | ||
Check warning on line 19 in tools/helpers.ts GitHub Actions / tools-tests
|
||
if (order_a) return 1 | ||
if (order_b) return -1 | ||
return a[0].localeCompare(b[0]) | ||
}) | ||
sorted.forEach(([k, v]) => { | ||
delete obj[k] | ||
obj[k] = v | ||
}) | ||
} | ||
|
||
export function write2file(file_path: string, content: Record<string, any>): void { | ||
fs.writeFileSync(file_path, quoteRefs(YAML.stringify(removeAnchors(content), {lineWidth: 0, singleQuote: true}))); | ||
export function write2file (file_path: string, content: Record<string, any>): void { | ||
fs.writeFileSync(file_path, quoteRefs(YAML.stringify(removeAnchors(content), { lineWidth: 0, singleQuote: true }))) | ||
} | ||
|
||
function quoteRefs(str: string): string { | ||
return str.split('\n').map((line) => { | ||
if(line.includes('$ref')) { | ||
const [key, value] = line.split(': '); | ||
if(!value.startsWith("'")) line = `${key}: '${value}'`; | ||
} | ||
return line | ||
}).join('\n'); | ||
function quoteRefs (str: string): string { | ||
return str.split('\n').map((line) => { | ||
if (line.includes('$ref')) { | ||
const [key, value] = line.split(': ') | ||
if (!value.startsWith("'")) line = `${key}: '${value}'` | ||
} | ||
return line | ||
}).join('\n') | ||
} | ||
|
||
function removeAnchors(content: Record<string, any>): Record<string, any> { | ||
const replacer = (key: string, value: any) => key === '$anchor' ? undefined : value; | ||
return JSON.parse(JSON.stringify(content, replacer)); | ||
} | ||
function removeAnchors (content: Record<string, any>): Record<string, any> { | ||
const replacer = (key: string, value: any) => key === '$anchor' ? undefined : value | ||
return JSON.parse(JSON.stringify(content, replacer)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; | ||
testEnvironment: 'node' | ||
} |
Oops, something went wrong.