diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 5a2a12f6f..ec7c3c152 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,9 +31,3 @@ jobs: - name: Lint run: npm run lint - - - name: Check Style - uses: errata-ai/vale-action@v2.1.1 - with: - version: 3.7.1 - files: '["spec","tests"]' diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml new file mode 100644 index 000000000..916be0b25 --- /dev/null +++ b/.github/workflows/style.yml @@ -0,0 +1,28 @@ +name: Lint + +on: [pull_request,push] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: '20' + + - name: Install Dependencies + run: npm ci + + - name: Lint + run: npm run vale -- --source spec + run: npm run vale -- --source tests + + - name: Check Style + uses: errata-ai/vale-action@v2.1.1 + with: + version: 3.7.1 + files: '["spec","tests"]' diff --git a/package.json b/package.json index 5244756f3..8087827d4 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "lint": "eslint . --report-unused-disable-directives", "lint--fix": "eslint . --fix --report-unused-disable-directives", "merge": "ts-node tools/src/merger/merge.ts", + "vale": "ts-node tools/src/vale/vale.ts", "test": "npm run test:unit && npm run test:integ", "jest": "jest", "test:unit": "jest --testMatch='**/*.test.ts' --testPathIgnorePatterns=/integ/", diff --git a/tools/src/vale/OpenApiValer.ts b/tools/src/vale/OpenApiValer.ts new file mode 100644 index 000000000..a3658f1d7 --- /dev/null +++ b/tools/src/vale/OpenApiValer.ts @@ -0,0 +1,49 @@ +/* +* Copyright OpenSearch Contributors +* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +import fs from 'fs' +import globby from 'globby' +import _ from 'lodash' +import { Logger } from '../Logger' + +export default class OpenApiValer { + root_folder: string + logger: Logger + + constructor (root_folder: string, logger: Logger = new Logger()) { + this.logger = logger + this.root_folder = fs.realpathSync(root_folder) + } + + async vale(): Promise { + const files = await globby([this.root_folder]) + files.forEach((path) => { + this.process(path) + }) + } + + process(filename: string): void { + const contents = fs.readFileSync(filename, 'utf-8') + var writer = fs.createWriteStream(filename, { flags: 'w+' }) + + var inside_description = false + contents.split(/\r?\n/).forEach((line) => { + if (line.includes('description: |')) { + inside_description = true + } else if (line.includes('description: ')) { + writer.write(line.replace("description: ", " ")) + } else if (inside_description && line.includes(": ")) { + inside_description = false + } else if (inside_description) { + writer.write(line) + } + writer.write("\n") + }) + } +} diff --git a/tools/src/vale/vale.ts b/tools/src/vale/vale.ts new file mode 100644 index 000000000..5a6080171 --- /dev/null +++ b/tools/src/vale/vale.ts @@ -0,0 +1,27 @@ +/* +* Copyright OpenSearch Contributors +* SPDX-License-Identifier: Apache-2.0 +* +* The OpenSearch Contributors require contributions made to +* this file be licensed under the Apache-2.0 license or a +* compatible open source license. +*/ + +import { Command, Option } from '@commander-js/extra-typings' +import { Logger, LogLevel } from '../Logger' +import { resolve } from 'path' +import OpenApiValer from './OpenApiValer' + +const command = new Command() + .description('Prepares YAML files for Vale.') + .addOption(new Option('-s, --source ', 'path to the root folder of the multi-file spec').default(resolve(__dirname, '../../../spec'))) + .addOption(new Option('--verbose', 'show merge details').default(false)) + .allowExcessArguments(false) + .parse() + +const opts = command.opts() +const logger = new Logger(opts.verbose ? LogLevel.info : LogLevel.warn) +const valer = new OpenApiValer(opts.source, logger) +logger.log(`Pruning ${opts.source} ...`) +valer.vale() +logger.log('Done.')