From 5da84335338d536e7dfd3781f1db89fd59656d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Sun, 12 May 2024 16:04:03 +0200 Subject: [PATCH] Further fixes fixes idrinth-api-bench/issues#1027 --- .dictionary.txt | 110 +++++++++++++++++++++++++++ .github/workflows/fta.yml | 2 +- .knip.json | 2 +- bin/check-route-definitions.js | 4 - bin/run-loadtest.js | 4 - package-lock.json | 16 ++-- package.json | 10 +-- test/cli/bench.ts | 21 ------ test/cli/check-routes.ts | 11 --- tools/constants.js | 7 ++ tools/fta.js | 132 +++++++++++++++++++++++++++++++++ 11 files changed, 266 insertions(+), 53 deletions(-) create mode 100644 .dictionary.txt delete mode 100644 bin/check-route-definitions.js delete mode 100644 bin/run-loadtest.js delete mode 100644 test/cli/bench.ts delete mode 100644 test/cli/check-routes.ts create mode 100644 tools/constants.js create mode 100644 tools/fta.js diff --git a/.dictionary.txt b/.dictionary.txt new file mode 100644 index 0000000..c5bb529 --- /dev/null +++ b/.dictionary.txt @@ -0,0 +1,110 @@ +2xx +APIs +Autowiring +Benchmarking +Björn +Büttner +CLI +Codacy +Destructuring +DockerHub +Dockerfiles +ENV +Fastify +vue +Gitea +Gitlab +HAR +Idrinth's +airtimeux.com +JSON +Javascript +Jira +LinkedIn +MSSQL +Matomo +Microservice +Middleware +Middlewares +Mindmap +NPMJS +Node.js +OpenApi +OpenSauced +PascalCase +Postgres +README +Reddit +Roadmap +SHA +Web-UI +XML +XRAY_CLIENT_ID +XRAY_CLIENT_SECRET +XRAY_ENDPOINT +XRAY_PASSWORD +XRAY_TEST_PLAN_KEY +XRAY_USER_NAME +XRay +aBc +api-bench +api-bench-cli +api-bench-gitea-action +api-bench-gitlab-runner +api-bench-history-microservice +api-bench-history-website +api-bench-runner +api-benchmark +autowire +autowired +benchmarking +bugfixes +bvanderlaan +camelCase +ci-images +coai +codecheck +csrf-header +data.yml +deprecations +dockerfiles +eslint +faq +gitea +gitlab +gitlab-runner +history-microservice +iab +idrinth +idrinth-api-bench +jeffbski +json +jungwild +macOS +markmap +matteofigus +microservice +microservices +middleware +middlewares +mindmap +mono-repo +npm +ns +package.json +pino +pre +programmatically +readme +rest-APIs +sexualised +src +subprojects +tracking.bjoern-buettner.me +ui +UI +webmaster@idrinth-api-ben.ch +winston +www.contributor-covenant.org +www.youtube-nocookie.com +xml diff --git a/.github/workflows/fta.yml b/.github/workflows/fta.yml index 273c685..e97139a 100644 --- a/.github/workflows/fta.yml +++ b/.github/workflows/fta.yml @@ -32,4 +32,4 @@ jobs: package-lock.json - run: npm ci - run: npm run language - - run: npm run ftas + - run: npm run fta diff --git a/.knip.json b/.knip.json index 6fec84a..734f665 100644 --- a/.knip.json +++ b/.knip.json @@ -2,5 +2,5 @@ "$schema": "https://unpkg.com/knip@5/schema.json", "entry": ["index.ts", "src/middlewares/*.ts", "src/locales/*.ts", "worker/*.js", "bin/*.js", "tools/*.js", "test/**/*.ts", "integration/**/*.ts", "property/**/*.ts"], "project": ["index.ts", "src/**/*.ts", "test/**/*.ts", "integration/**/*.ts", "property/**/*.ts", "tools/*.js"], - "ignoreDependencies": ["@idrinth/api-bench-cli", "ts-node"] + "ignoreDependencies": ["ts-node"] } diff --git a/bin/check-route-definitions.js b/bin/check-route-definitions.js deleted file mode 100644 index a712992..0000000 --- a/bin/check-route-definitions.js +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env node -import checkRoutes from '../src/cli/check-routes.ts'; - -checkRoutes(process.argv, process.cwd(),); diff --git a/bin/run-loadtest.js b/bin/run-loadtest.js deleted file mode 100644 index 84665fb..0000000 --- a/bin/run-loadtest.js +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env node -import load from '../src/cli/loader.ts'; - -load(process.argv,); diff --git a/package-lock.json b/package-lock.json index 611b3b4..f713804 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,11 +28,7 @@ "yaml": "^2.4.1" }, "bin": { - "check-route-definitions": "bin/check-route-definitions.js", - "iab": "bin/iab.js", - "iabcrd": "bin/check-route-definitions.js", - "iabl": "bin/run-loadtest.js", - "run-loadtest": "bin/run-loadtest.js" + "iab": "bin/iab.js" }, "devDependencies": { "@commitlint/cli": "^19.2.1", @@ -50,6 +46,7 @@ "consolemock": "^1.1.0", "eslint": "^8.55.0", "fast-check": "^3.17.1", + "fta-cli": "^2.0.0", "knip": "^5.13.0", "markdownlint-cli": "^0.40.0", "mocha": "^10.2.0", @@ -3219,6 +3216,15 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/fta-cli": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fta-cli/-/fta-cli-2.0.0.tgz", + "integrity": "sha512-MdpB2ChAfzNhPck/+VHAlUHZFiSP7LSt91Vi4jJymg+oRVRWOKpKbd5fuk9zJZR1YQ4uDkYZEAop04Qkcueqaw==", + "dev": true, + "bin": { + "fta": "index.js" + } + }, "node_modules/function-bind": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", diff --git a/package.json b/package.json index 3ef6170..0901203 100644 --- a/package.json +++ b/package.json @@ -78,13 +78,10 @@ "winston": "^3.11.0", "markdownlint-cli": "^0.40.0", "spellchecker-cli": "^6.2.0", - "knip": "^5.13.0" + "knip": "^5.13.0", + "fta-cli": "^2.0.0" }, "bin": { - "check-route-definitions": "bin/check-route-definitions.js", - "run-loadtest": "bin/run-loadtest.js", - "iabl": "bin/run-loadtest.js", - "iabcrd": "bin/check-route-definitions.js", "iab": "bin/iab.js" }, "scripts": { @@ -103,7 +100,8 @@ "spellcheck": "itlfy dump && spellchecker", "knip": "knip --exclude duplicates --exclude exports --exclude types", "commitlint": "commitlint --edit", - "commitlint-ci": "commitlint --latest" + "commitlint-ci": "commitlint --from latest", + "fta": "node tools/fta.js" }, "engines": { "node": ">=20" diff --git a/test/cli/bench.ts b/test/cli/bench.ts deleted file mode 100644 index 91234a9..0000000 --- a/test/cli/bench.ts +++ /dev/null @@ -1,21 +0,0 @@ -import cli from '../../src/cli/bench'; -import { - expect, -} from 'chai'; -import 'mocha'; - -describe('bench', () => { - it('should be a function', () => { - expect(cli,).to.be.a('function',); - },); - it('cli() throw when given 0 total tasks', async() => { - try { - await cli([ - '0', - '0', - ],); - } catch (e) { - expect(e,).to.be.an('Error',); - } - },); -},); diff --git a/test/cli/check-routes.ts b/test/cli/check-routes.ts deleted file mode 100644 index 1a9e9b0..0000000 --- a/test/cli/check-routes.ts +++ /dev/null @@ -1,11 +0,0 @@ -import cli from '../../src/cli/check-routes'; -import { - expect, -} from 'chai'; -import 'mocha'; - -describe('check-routes', () => { - it('should be a function', () => { - expect(cli,).to.be.a('function',); - },); -},); diff --git a/tools/constants.js b/tools/constants.js new file mode 100644 index 0000000..2876d84 --- /dev/null +++ b/tools/constants.js @@ -0,0 +1,7 @@ +export const FIRST_ARGUMENT = 2; +export const EXIT_SUCCESS = 0; +export const EXIT_FAILURE = 1; +export const EMPTY = 0; +export const ERROR_FTA_SCORE = 60; +export const WARNING_FTA_SCORE = 60; +export const PADDING_DEFAULT = 1; diff --git a/tools/fta.js b/tools/fta.js new file mode 100644 index 0000000..7e6ab33 --- /dev/null +++ b/tools/fta.js @@ -0,0 +1,132 @@ +import { + EXIT_FAILURE, + EXIT_SUCCESS, + FIRST_ARGUMENT, + ERROR_FTA_SCORE, + WARNING_FTA_SCORE, + PADDING_DEFAULT, +} from './constants.js'; +import path from 'path'; +import fs from 'fs'; +import { + execSync, +} from 'child_process'; + +const platform = process.platform; +const architecture = process.arch; + +// eslint-disable-next-line complexity +const getBinaryPath = () => { + const targetDirectory = path.join( + process.cwd(), + 'node_modules', + 'fta-cli', + 'binaries', + ); + + switch (platform) { + case 'win32': + if (architecture === 'x64') { + return path.join(targetDirectory, 'x86_64-pc-windows-msvc', 'fta.exe',); + } + if (architecture === 'arm64') { + return path.join( + targetDirectory, + 'aarch64-pc-windows-msvc', + 'fta.exe', + ); + } + break; + case 'darwin': + if (architecture === 'x64') { + return path.join(targetDirectory, 'x86_64-apple-darwin', 'fta',); + } else if (architecture === 'arm64') { + return path.join(targetDirectory, 'aarch64-apple-darwin', 'fta',); + } + break; + case 'linux': + if (architecture === 'x64') { + return path.join(targetDirectory, 'x86_64-unknown-linux-musl', 'fta',); + } else if (architecture === 'arm64') { + return path.join(targetDirectory, 'aarch64-unknown-linux-musl', 'fta',); + } else if (architecture === 'arm') { + return path.join(targetDirectory, 'arm-unknown-linux-musleabi', 'fta',); + } + break; + default: + throw new Error('Unsupported platform: ' + platform,); + } + + throw new Error('Binary not found for the current platform',); +}; + +const setUnixPerms = (binaryPath,) => { + if (platform === 'darwin' || platform === 'linux') { + try { + fs.chmodSync(binaryPath, '755',); + } catch (e) { + console.warn('Could not chmod fta binary: ', e,); + } + } +}; + +const binary = getBinaryPath(); +setUnixPerms(binary,); +const output = JSON.parse(execSync( + `${ binary } ${ process.cwd() } --json`, + { + encoding: 'utf8', + stdio: 'pipe', + }, +).toString(),); + +let hardToMaintain = false; +let maxLength = 0; +let maxScore = 0; +let maxAfter = 0; + +for (const file of output) { + file.name = `${ process.argv[FIRST_ARGUMENT] }/${ file.file_name }`; + maxLength = maxLength > file.name.length ? maxLength : file.name.length; + maxScore = maxScore > `${ file.fta_score }`.split('.',).shift().length + ? maxScore + : `${ file.fta_score }`.split('.',).shift().length; + maxAfter = maxAfter > `${ file.fta_score }`.split('.',).pop().length + ? maxScore + : `${ file.fta_score }`.split('.',).pop().length; +} + +for (const file of output) { + const padding = new Array(maxLength - file.name.length + PADDING_DEFAULT,) + .fill(' ',) + .join('',); + const before = new Array( + maxScore + - `${ file.fta_score }`.split('.',).shift().length + + PADDING_DEFAULT, + ) + .fill(' ',) + .join('',); + const after = new Array( + maxAfter + - `${ file.fta_score }`.split('.',).pop().length + + PADDING_DEFAULT, + ) + .fill('0',) + .join('',); + const message = [ + `${ file.name }${ padding }`, + `${ before }${ file.fta_score }${ after.replace(/0$/u, ' ',) }`, + ` ${ file.assessment }`, + ].join('|',); + if (file.fta_score > ERROR_FTA_SCORE) { + hardToMaintain = true; + console.error(message,); + } else if (file.fta_score > WARNING_FTA_SCORE) { + console.warn(message,); + } else { + console.log(message,); + } +} + +process.exit(hardToMaintain ? EXIT_FAILURE : EXIT_SUCCESS,);