From 880dae2aabc51db62b0f8fd630fc0b39fadde031 Mon Sep 17 00:00:00 2001 From: Philipp Melab Date: Wed, 3 Jan 2024 19:23:13 +0000 Subject: [PATCH] refactor(SLB-203): use shared executors package --- packages/executors/.eslintrc.mjs | 4 -- packages/executors/.gitignore | 1 - packages/executors/.prettierrc | 1 - packages/executors/package.json | 31 -------- packages/executors/rollup.config.js | 8 --- packages/executors/src/index.ts | 1 - packages/executors/src/lib.test.ts | 77 -------------------- packages/executors/src/lib.ts | 107 ---------------------------- packages/executors/tsconfig.json | 19 ----- packages/executors/turbo.json | 9 --- packages/schema/package.json | 4 +- pnpm-lock.yaml | 35 +++------ 12 files changed, 13 insertions(+), 284 deletions(-) delete mode 100644 packages/executors/.eslintrc.mjs delete mode 100644 packages/executors/.gitignore delete mode 100644 packages/executors/.prettierrc delete mode 100644 packages/executors/package.json delete mode 100644 packages/executors/rollup.config.js delete mode 100644 packages/executors/src/index.ts delete mode 100644 packages/executors/src/lib.test.ts delete mode 100644 packages/executors/src/lib.ts delete mode 100644 packages/executors/tsconfig.json delete mode 100644 packages/executors/turbo.json diff --git a/packages/executors/.eslintrc.mjs b/packages/executors/.eslintrc.mjs deleted file mode 100644 index 2702f9693..000000000 --- a/packages/executors/.eslintrc.mjs +++ /dev/null @@ -1,4 +0,0 @@ -export default { - extends: ['@amazeelabs/eslint-config'], - root: true, -}; diff --git a/packages/executors/.gitignore b/packages/executors/.gitignore deleted file mode 100644 index 378eac25d..000000000 --- a/packages/executors/.gitignore +++ /dev/null @@ -1 +0,0 @@ -build diff --git a/packages/executors/.prettierrc b/packages/executors/.prettierrc deleted file mode 100644 index ae00ae6bf..000000000 --- a/packages/executors/.prettierrc +++ /dev/null @@ -1 +0,0 @@ -"@amazeelabs/prettier-config" diff --git a/packages/executors/package.json b/packages/executors/package.json deleted file mode 100644 index 78f700dca..000000000 --- a/packages/executors/package.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "name": "@amazeelabs/executors", - "version": "1.0.0", - "description": "", - "main": "build/index.js", - "types": "build/index.d.ts", - "type": "module", - "scripts": { - "prep": "tsc && rollup -c", - "build": "pnpm prep", - "test:unit": "vitest run", - "test:static": "tsc --noEmit && eslint \"**/*.{ts,tsx,js,jsx}\" --ignore-path=\"./.gitignore\" --fix" - }, - "keywords": [], - "author": "Amazee Labs ", - "license": "ISC", - "dependencies": { - "lodash-es": "^4.17.21" - }, - "devDependencies": { - "@amazeelabs/eslint-config": "^1.4.43", - "@amazeelabs/prettier-config": "^1.1.3", - "@types/lodash-es": "^4.17.12", - "eslint": "^8.43.0", - "prettier": "^2.8.8", - "rollup": "^4.9.2", - "rollup-plugin-dts": "^6.1.0", - "typescript": "^5.3.3", - "vitest": "^1.1.0" - } -} diff --git a/packages/executors/rollup.config.js b/packages/executors/rollup.config.js deleted file mode 100644 index f4e235e3b..000000000 --- a/packages/executors/rollup.config.js +++ /dev/null @@ -1,8 +0,0 @@ -import { defineConfig } from 'rollup'; -import dts from 'rollup-plugin-dts'; - -export default defineConfig({ - input: 'build/dts/index.d.ts', - output: [{ file: 'build/index.d.ts', format: 'es' }], - plugins: [dts()], -}); diff --git a/packages/executors/src/index.ts b/packages/executors/src/index.ts deleted file mode 100644 index 6158ab70c..000000000 --- a/packages/executors/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { createExecutor, registerExecutor, clearRegistry } from './lib.js'; diff --git a/packages/executors/src/lib.test.ts b/packages/executors/src/lib.test.ts deleted file mode 100644 index b410d06e0..000000000 --- a/packages/executors/src/lib.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { beforeEach, expect, test, vi } from 'vitest'; - -import { clearRegistry, createExecutor, registerExecutor } from './lib'; - -beforeEach(clearRegistry); - -test('no operator', () => { - expect(() => createExecutor('unknown')).toThrow(); -}); - -test('global default operator', () => { - const op = vi.fn(); - registerExecutor(op); - expect(() => createExecutor('unknown')()).not.toThrow(); - expect(op).toHaveBeenCalledOnce(); - expect(op).toHaveBeenCalledWith('unknown', undefined); -}); - -test('global default operator with arguments', () => { - const op = vi.fn(); - registerExecutor(op); - expect(() => createExecutor('unknown', { foo: 'bar' })()).not.toThrow(); - expect(op).toHaveBeenCalledOnce(); - expect(op).toHaveBeenCalledWith('unknown', { foo: 'bar' }); -}); - -test('operation default operator', () => { - const a = vi.fn(); - registerExecutor('a', a); - expect(() => createExecutor('b')()).toThrow(); - expect(() => createExecutor('a')()).not.toThrow(); - expect(a).toHaveBeenCalledOnce(); - expect(a).toHaveBeenCalledWith('a', undefined); -}); - -test('operation default operator with arguments', () => { - const a = vi.fn(); - registerExecutor('a', a); - expect(() => createExecutor('b', { foo: 'bar' })()).toThrow(); - expect(() => createExecutor('a', { foo: 'bar' })()).not.toThrow(); - expect(a).toHaveBeenCalledOnce(); - expect(a).toHaveBeenCalledWith('a', { foo: 'bar' }); -}); - -test('structural argument matching', () => { - const id = 'x'; - const a = vi.fn(); - const b = vi.fn(); - const c = vi.fn(); - registerExecutor(id, { y: 1 }, a); - registerExecutor(id, { y: 2 }, b); - registerExecutor(id, { y: 1, z: 1 }, c); - expect(() => createExecutor(id, { y: 3 })()).toThrow(); - expect(() => createExecutor(id, { y: 1, z: 1 })()).not.toThrow(); - expect(() => createExecutor(id, { y: 1 })()).not.toThrow(); - expect(() => createExecutor(id, { y: 2 })()).not.toThrow(); - - expect(a).toHaveBeenCalledOnce(); - expect(a).toHaveBeenCalledWith(id, { y: 1 }); - expect(b).toHaveBeenCalledOnce(); - expect(b).toHaveBeenCalledWith(id, { y: 2 }); - expect(c).toHaveBeenCalledOnce(); - expect(c).toHaveBeenCalledWith(id, { y: 1, z: 1 }); -}); - -test('static data resolution', () => { - const id = 'x'; - registerExecutor(id, { foo: 'bar' }); - expect(createExecutor(id)).toEqual({ foo: 'bar' }); -}); - -test('static data resolution with arguments', () => { - const id = 'x'; - registerExecutor(id, { y: 1 }, { foo: 'bar' }); - expect(() => createExecutor(id)).toThrow(); - expect(createExecutor(id, { y: 1 })).toEqual({ foo: 'bar' }); -}); diff --git a/packages/executors/src/lib.ts b/packages/executors/src/lib.ts deleted file mode 100644 index ed22ee0ed..000000000 --- a/packages/executors/src/lib.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { isMatch, isString } from 'lodash-es'; - -type Executor = - | any - | ((id: string, variables: Record) => any | Promise); - -type VariablesMatcher = - | Record - | ((vars: Record) => boolean); - -type RegistryEntry = { - executor: Executor; - id?: string; - variables?: VariablesMatcher; -}; - -let registry: Array = []; - -export function clearRegistry() { - registry = []; -} - -export function registerExecutor(executor: Executor): void; - -export function registerExecutor(id: string, executor: Executor): void; - -export function registerExecutor( - id: string, - variables: VariablesMatcher, - executor: Executor, -): void; - -export function registerExecutor(...args: unknown[]): void { - registry.push({ - id: isString(args[0]) ? args[0] : undefined, - executor: args[args.length - 1], - variables: args.length === 3 ? args[1] : undefined, - }); -} - -function matchVariables(matcher: VariablesMatcher | undefined, variables: any) { - if (typeof matcher === 'undefined') { - return true; - } - if (typeof matcher === 'function') { - return matcher(variables); - } - return isMatch(variables, matcher); -} - -function getCandidates(id: string) { - return (registry as Array).filter( - (entry) => id === entry.id || entry.id === undefined, - ); -} - -function formatEntry(id: string | undefined, variables?: Record) { - return ` ${id ? id : '*'}:${variables ? JSON.stringify(variables) : '*'}`; -} - -class ExecutorRegistryError extends Error { - constructor(id: string, variables?: Record) { - const candidates = getCandidates(id); - const candidatesMessage = - candidates.length > 0 - ? [ - 'Candidates:', - ...candidates.map(({ id, variables }) => - formatEntry(id, variables), - ), - ] - : []; - super( - [ - 'No executor found for:', - formatEntry(id, variables), - ...candidatesMessage, - ].join('\n'), - ); - this.name = 'ExecutorRegistryError'; - } -} - -/** - * Create an executor from the registry. - * - * @param id - * The operation ID, from the schema package. - * @param variables - * A dictionary of variables to be passed to the operation. - */ -export function createExecutor( - id: string, - variables?: Record, -): () => any | any { - const op = getCandidates(id) - .filter((entry) => matchVariables(entry.variables, variables)) - .pop(); - - if (op) { - if (typeof op.executor === 'function') { - return () => op.executor(id, variables); - } - return op.executor; - } - throw new ExecutorRegistryError(id, variables); -} diff --git a/packages/executors/tsconfig.json b/packages/executors/tsconfig.json deleted file mode 100644 index 82c36dcd1..000000000 --- a/packages/executors/tsconfig.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "allowJs": false, - "skipLibCheck": true, - "esModuleInterop": false, - "allowSyntheticDefaultImports": true, - "strict": true, - "forceConsistentCasingInFileNames": true, - "module": "ESNext", - "moduleResolution": "Node", - "resolveJsonModule": true, - "isolatedModules": true, - "declaration": true, - "declarationDir": "build/dts", - "outDir": "build" - }, - "include": ["src"] -} diff --git a/packages/executors/turbo.json b/packages/executors/turbo.json deleted file mode 100644 index 66be584b4..000000000 --- a/packages/executors/turbo.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "$schema": "https://turborepo.org/schema.json", - "extends": ["//"], - "pipeline": { - "prep": { - "outputs": ["build"] - } - } -} diff --git a/packages/schema/package.json b/packages/schema/package.json index 9665c9454..e274d496b 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -35,8 +35,8 @@ "prep": "graphql-codegen --verbose && swc ./src -d ./build && tsc --emitDeclarationOnly" }, "devDependencies": { - "@amazeelabs/codegen-gatsby-fragments": "^1.1.8", "@amazeelabs/codegen-autoloader": "^1.1.1", + "@amazeelabs/codegen-gatsby-fragments": "^1.1.8", "@amazeelabs/codegen-operation-ids": "^0.1.21", "@graphql-codegen/cli": "^4.0.1", "@graphql-codegen/schema-ast": "^4.0.0", @@ -49,7 +49,7 @@ "typescript": "^5.1.6" }, "dependencies": { - "@amazeelabs/executors": "workspace:*", + "@amazeelabs/executors": "^1.1.0", "@amazeelabs/scalars": "^1.6.2", "@swc/cli": "^0.1.62", "@swc/core": "^1.3.80" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e90dca079..bcd106e6a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -362,8 +362,8 @@ importers: packages/schema: dependencies: '@amazeelabs/executors': - specifier: workspace:* - version: link:../executors + specifier: ^1.1.0 + version: 1.1.0 '@amazeelabs/scalars': specifier: ^1.6.2 version: 1.6.2(react@18.2.0)(tailwindcss@3.3.2) @@ -800,7 +800,7 @@ packages: eslint: ^8.36.0 dependencies: '@typescript-eslint/eslint-plugin': 5.60.1(@typescript-eslint/parser@5.60.1)(eslint@7.32.0)(typescript@5.3.3) - '@typescript-eslint/parser': 5.60.1(eslint@7.32.0)(typescript@5.3.3) + '@typescript-eslint/parser': 5.60.1(eslint@7.32.0)(typescript@5.1.6) eslint: 7.32.0 eslint-config-prettier: 8.8.0(eslint@7.32.0) eslint-plugin-import: 2.27.5(@typescript-eslint/parser@5.60.1)(eslint@7.32.0) @@ -817,6 +817,12 @@ packages: - tailwindcss - typescript + /@amazeelabs/executors@1.1.0: + resolution: {integrity: sha512-W7Q1JV0zyraAcHdv3cUDFNNonflq/WuTtqtS0zT1VrZAyInNDPT5nSgTiJ4V9RqD+3lVhBk6jMlSsQkCPa3B8A==} + dependencies: + lodash-es: 4.17.21 + dev: false + /@amazeelabs/gatsby-silverback-cloudinary@1.2.5: resolution: {integrity: sha512-EwaakDLR0TAcYiYcf3ntu5MqbNrCZVjh9Qq215jofmvoFK91qFzD8+rnUiGvTMfyWsCsI+tIXRpK81R0Lt4wjA==} dev: false @@ -10628,7 +10634,7 @@ packages: optional: true dependencies: '@eslint-community/regexpp': 4.5.1 - '@typescript-eslint/parser': 5.60.1(eslint@7.32.0)(typescript@5.3.3) + '@typescript-eslint/parser': 5.60.1(eslint@7.32.0)(typescript@5.1.6) '@typescript-eslint/scope-manager': 5.60.1 '@typescript-eslint/type-utils': 5.60.1(eslint@7.32.0)(typescript@5.3.3) '@typescript-eslint/utils': 5.60.1(eslint@7.32.0)(typescript@5.3.3) @@ -10715,26 +10721,6 @@ packages: transitivePeerDependencies: - supports-color - /@typescript-eslint/parser@5.60.1(eslint@7.32.0)(typescript@5.3.3): - resolution: {integrity: sha512-pHWlc3alg2oSMGwsU/Is8hbm3XFbcrb6P5wIxcQW9NsYBfnrubl/GhVVD/Jm/t8HXhA2WncoIRfBtnCgRGV96Q==} - engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} - requiresBuild: true - peerDependencies: - eslint: ^6.0.0 || ^7.0.0 || ^8.0.0 - typescript: '*' - peerDependenciesMeta: - typescript: - optional: true - dependencies: - '@typescript-eslint/scope-manager': 5.60.1 - '@typescript-eslint/types': 5.60.1 - '@typescript-eslint/typescript-estree': 5.60.1(typescript@5.3.3) - debug: 4.3.4 - eslint: 7.32.0 - typescript: 5.3.3 - transitivePeerDependencies: - - supports-color - /@typescript-eslint/parser@6.0.0(eslint@7.32.0)(typescript@5.1.6): resolution: {integrity: sha512-TNaufYSPrr1U8n+3xN+Yp9g31vQDJqhXzzPSHfQDLcaO4tU+mCfODPxCwf4H530zo7aUBE3QIdxCXamEnG04Tg==} engines: {node: ^16.0.0 || >=18.0.0} @@ -31902,6 +31888,7 @@ packages: resolution: {integrity: sha512-hU8GNNuQfwuQmqTLfiKcqEhZY72Zxb7nnN07koCUNmntNxbKQnVbeIS6sqUgR3eXSlbOpit8+/gr1KpqoMgWCQ==} engines: {node: '>=v14.18.0'} hasBin: true + requiresBuild: true peerDependencies: '@edge-runtime/vm': '*' '@vitest/browser': '*'