From 3200b6b19492d9b6eedf111050f5e5638768660e Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 13 May 2024 11:43:45 +0200 Subject: [PATCH] Refactor TS to use the same paths for build&test for loading package.json Signed-off-by: Matteo Collina --- package.json | 8 ++++---- schema.json | 2 +- {ai-providers => src/ai-providers}/azure.ts | 0 {ai-providers => src/ai-providers}/event.ts | 0 {ai-providers => src/ai-providers}/llama2.ts | 0 {ai-providers => src/ai-providers}/mistral.ts | 0 {ai-providers => src/ai-providers}/ollama.ts | 0 {ai-providers => src/ai-providers}/open-ai.ts | 0 {ai-providers => src/ai-providers}/provider.ts | 0 {cli => src/cli}/create.ts | 0 {cli => src/cli}/start.ts | 0 config.d.ts => src/config.d.ts | 10 +++------- index.d.ts => src/index.d.ts | 0 index.ts => src/index.ts | 0 {lib => src/lib}/generator.ts | 11 ++++------- {lib => src/lib}/schema.ts | 5 +++++ {lib => src/lib}/templates/types.ts | 0 {plugins => src/plugins}/api.ts | 0 {plugins => src/plugins}/auth.ts | 0 {plugins => src/plugins}/rate-limiting.ts | 0 {plugins => src/plugins}/warp.ts | 0 {tests => src/tests}/e2e/api.test.ts | 0 {tests => src/tests}/e2e/auth.test.ts | 0 {tests => src/tests}/e2e/rate-limiting.test.ts | 0 {tests => src/tests}/types/README.md | 0 {tests => src/tests}/types/fastify.test-d.ts | 0 {tests => src/tests}/types/schema.test-d.ts | 0 {tests => src/tests}/unit/.gitignore | 0 {tests => src/tests}/unit/ai-providers.test.ts | 0 {tests => src/tests}/unit/generator.test.ts | 6 ------ {tests => src/tests}/utils/auth.ts | 0 {tests => src/tests}/utils/mocks/azure.ts | 0 {tests => src/tests}/utils/mocks/base.ts | 0 {tests => src/tests}/utils/mocks/index.ts | 0 {tests => src/tests}/utils/mocks/llama2.ts | 0 {tests => src/tests}/utils/mocks/mistral.ts | 0 {tests => src/tests}/utils/mocks/ollama.ts | 0 {tests => src/tests}/utils/mocks/open-ai.ts | 0 {tests => src/tests}/utils/stackable.ts | 0 39 files changed, 17 insertions(+), 25 deletions(-) rename {ai-providers => src/ai-providers}/azure.ts (100%) rename {ai-providers => src/ai-providers}/event.ts (100%) rename {ai-providers => src/ai-providers}/llama2.ts (100%) rename {ai-providers => src/ai-providers}/mistral.ts (100%) rename {ai-providers => src/ai-providers}/ollama.ts (100%) rename {ai-providers => src/ai-providers}/open-ai.ts (100%) rename {ai-providers => src/ai-providers}/provider.ts (100%) rename {cli => src/cli}/create.ts (100%) rename {cli => src/cli}/start.ts (100%) rename config.d.ts => src/config.d.ts (99%) rename index.d.ts => src/index.d.ts (100%) rename index.ts => src/index.ts (100%) rename {lib => src/lib}/generator.ts (93%) rename {lib => src/lib}/schema.ts (97%) rename {lib => src/lib}/templates/types.ts (100%) rename {plugins => src/plugins}/api.ts (100%) rename {plugins => src/plugins}/auth.ts (100%) rename {plugins => src/plugins}/rate-limiting.ts (100%) rename {plugins => src/plugins}/warp.ts (100%) rename {tests => src/tests}/e2e/api.test.ts (100%) rename {tests => src/tests}/e2e/auth.test.ts (100%) rename {tests => src/tests}/e2e/rate-limiting.test.ts (100%) rename {tests => src/tests}/types/README.md (100%) rename {tests => src/tests}/types/fastify.test-d.ts (100%) rename {tests => src/tests}/types/schema.test-d.ts (100%) rename {tests => src/tests}/unit/.gitignore (100%) rename {tests => src/tests}/unit/ai-providers.test.ts (100%) rename {tests => src/tests}/unit/generator.test.ts (90%) rename {tests => src/tests}/utils/auth.ts (100%) rename {tests => src/tests}/utils/mocks/azure.ts (100%) rename {tests => src/tests}/utils/mocks/base.ts (100%) rename {tests => src/tests}/utils/mocks/index.ts (100%) rename {tests => src/tests}/utils/mocks/llama2.ts (100%) rename {tests => src/tests}/utils/mocks/mistral.ts (100%) rename {tests => src/tests}/utils/mocks/ollama.ts (100%) rename {tests => src/tests}/utils/mocks/open-ai.ts (100%) rename {tests => src/tests}/utils/stackable.ts (100%) diff --git a/package.json b/package.json index 0dd5f8d..09ed3ba 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "scripts": { "create": "node ./dist/cli/create.js", "start": "node ./dist/cli/start.js -c ./ai-warp-app/platformatic.json", - "build": "tsc --build && cp -r ./static ./dist/ && cp config.d.ts dist/", - "build:config": "npm run build && node ./dist/lib/schema.js --dump-schema > schema.json && json2ts > config.d.ts < schema.json", + "build": "tsc --build && cp -r ./static ./dist/ && cp src/config.d.ts dist/", + "build:config": "npm run build && node ./dist/lib/schema.js --dump-schema > schema.json && json2ts > src/config.d.ts < schema.json", "prepare": "npm run build:config", "clean": "rm -fr ./dist", "lint": "ts-standard | snazzy", @@ -19,8 +19,8 @@ "lint-md": "markdownlint-cli2 .", "lint-md:fix": "markdownlint-cli2 --fix .", "test": "npm run test:unit && npm run test:e2e && npm run test:types", - "test:unit": "node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx --test-concurrency=1 ./tests/unit/*", - "test:e2e": "node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx --test-concurrency=1 ./tests/e2e/*", + "test:unit": "node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx --test-concurrency=1 ./src/tests/unit/*", + "test:e2e": "node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx --test-concurrency=1 ./src/tests/e2e/*", "test:types": "tsd" }, "engines": { diff --git a/schema.json b/schema.json index e43822d..9400ba9 100644 --- a/schema.json +++ b/schema.json @@ -1,6 +1,6 @@ { "$id": "ai-warp", - "version": "1.37.1", + "version": "0.3.1", "title": "Ai Warp Config", "type": "object", "properties": { diff --git a/ai-providers/azure.ts b/src/ai-providers/azure.ts similarity index 100% rename from ai-providers/azure.ts rename to src/ai-providers/azure.ts diff --git a/ai-providers/event.ts b/src/ai-providers/event.ts similarity index 100% rename from ai-providers/event.ts rename to src/ai-providers/event.ts diff --git a/ai-providers/llama2.ts b/src/ai-providers/llama2.ts similarity index 100% rename from ai-providers/llama2.ts rename to src/ai-providers/llama2.ts diff --git a/ai-providers/mistral.ts b/src/ai-providers/mistral.ts similarity index 100% rename from ai-providers/mistral.ts rename to src/ai-providers/mistral.ts diff --git a/ai-providers/ollama.ts b/src/ai-providers/ollama.ts similarity index 100% rename from ai-providers/ollama.ts rename to src/ai-providers/ollama.ts diff --git a/ai-providers/open-ai.ts b/src/ai-providers/open-ai.ts similarity index 100% rename from ai-providers/open-ai.ts rename to src/ai-providers/open-ai.ts diff --git a/ai-providers/provider.ts b/src/ai-providers/provider.ts similarity index 100% rename from ai-providers/provider.ts rename to src/ai-providers/provider.ts diff --git a/cli/create.ts b/src/cli/create.ts similarity index 100% rename from cli/create.ts rename to src/cli/create.ts diff --git a/cli/start.ts b/src/cli/start.ts similarity index 100% rename from cli/start.ts rename to src/cli/start.ts diff --git a/config.d.ts b/src/config.d.ts similarity index 99% rename from config.d.ts rename to src/config.d.ts index e9f653d..69ab576 100644 --- a/config.d.ts +++ b/src/config.d.ts @@ -217,13 +217,9 @@ export interface AiWarpConfig { prefix?: string; path?: string; }; - plugins?: - | { - [k: string]: unknown; - } - | { - [k: string]: unknown; - }; + plugins?: { + [k: string]: unknown; + }; }[]; }; module?: string; diff --git a/index.d.ts b/src/index.d.ts similarity index 100% rename from index.d.ts rename to src/index.d.ts diff --git a/index.ts b/src/index.ts similarity index 100% rename from index.ts rename to src/index.ts diff --git a/lib/generator.ts b/src/lib/generator.ts similarity index 93% rename from lib/generator.ts rename to src/lib/generator.ts index ebc1a29..cf6c0b0 100644 --- a/lib/generator.ts +++ b/src/lib/generator.ts @@ -13,21 +13,19 @@ interface PackageJson { devDependencies: Record } +const dir = import.meta.dirname || dirname(fileURLToPath(import.meta.url)) +const aiWarpPackageJsonPath = join(dir, '..', '..', 'package.json') + class AiWarpGenerator extends ServiceGenerator { private _packageJson: PackageJson | null = null getDefaultConfig (): { [x: string]: BaseGenerator.JSONValue } { const defaultBaseConfig = super.getDefaultConfig() // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions - const dir = import.meta.dirname || dirname(fileURLToPath(import.meta.url)) const defaultConfig = { aiProvider: 'openai', aiModel: 'gpt-3.5-turbo', localSchema: false, - // TODO: temporary fix, when running the typescript files directly - // (in tests) this goes a directory above the actual project. Exposing - // temporarily until I come up with something better - aiWarpPackageJsonPath: join(dir, '..', '..', 'package.json') } return Object.assign({}, defaultBaseConfig, defaultConfig) } @@ -175,8 +173,7 @@ class AiWarpGenerator extends ServiceGenerator { async getStackablePackageJson (): Promise { if (this._packageJson == null) { - const packageJsonPath = this.config.aiWarpPackageJsonPath - const packageJsonFile = await readFile(packageJsonPath, 'utf8') + const packageJsonFile = await readFile(aiWarpPackageJsonPath, 'utf8') const packageJson: Partial = JSON.parse(packageJsonFile) if (packageJson.name === undefined || packageJson.name === null) { diff --git a/lib/schema.ts b/src/lib/schema.ts similarity index 97% rename from lib/schema.ts rename to src/lib/schema.ts index 678552d..b3a14a8 100644 --- a/lib/schema.ts +++ b/src/lib/schema.ts @@ -1,9 +1,14 @@ +import { join } from 'node:path' +import { readFileSync } from 'node:fs' import { schema } from '@platformatic/service' +const { version } = JSON.parse(readFileSync(join(import.meta.dirname, '..', '..', 'package.json'), 'utf-8')) + const aiWarpSchema = { ...schema.schema, $id: 'ai-warp', title: 'Ai Warp Config', + version, properties: { ...schema.schema.properties, module: { type: 'string' }, diff --git a/lib/templates/types.ts b/src/lib/templates/types.ts similarity index 100% rename from lib/templates/types.ts rename to src/lib/templates/types.ts diff --git a/plugins/api.ts b/src/plugins/api.ts similarity index 100% rename from plugins/api.ts rename to src/plugins/api.ts diff --git a/plugins/auth.ts b/src/plugins/auth.ts similarity index 100% rename from plugins/auth.ts rename to src/plugins/auth.ts diff --git a/plugins/rate-limiting.ts b/src/plugins/rate-limiting.ts similarity index 100% rename from plugins/rate-limiting.ts rename to src/plugins/rate-limiting.ts diff --git a/plugins/warp.ts b/src/plugins/warp.ts similarity index 100% rename from plugins/warp.ts rename to src/plugins/warp.ts diff --git a/tests/e2e/api.test.ts b/src/tests/e2e/api.test.ts similarity index 100% rename from tests/e2e/api.test.ts rename to src/tests/e2e/api.test.ts diff --git a/tests/e2e/auth.test.ts b/src/tests/e2e/auth.test.ts similarity index 100% rename from tests/e2e/auth.test.ts rename to src/tests/e2e/auth.test.ts diff --git a/tests/e2e/rate-limiting.test.ts b/src/tests/e2e/rate-limiting.test.ts similarity index 100% rename from tests/e2e/rate-limiting.test.ts rename to src/tests/e2e/rate-limiting.test.ts diff --git a/tests/types/README.md b/src/tests/types/README.md similarity index 100% rename from tests/types/README.md rename to src/tests/types/README.md diff --git a/tests/types/fastify.test-d.ts b/src/tests/types/fastify.test-d.ts similarity index 100% rename from tests/types/fastify.test-d.ts rename to src/tests/types/fastify.test-d.ts diff --git a/tests/types/schema.test-d.ts b/src/tests/types/schema.test-d.ts similarity index 100% rename from tests/types/schema.test-d.ts rename to src/tests/types/schema.test-d.ts diff --git a/tests/unit/.gitignore b/src/tests/unit/.gitignore similarity index 100% rename from tests/unit/.gitignore rename to src/tests/unit/.gitignore diff --git a/tests/unit/ai-providers.test.ts b/src/tests/unit/ai-providers.test.ts similarity index 100% rename from tests/unit/ai-providers.test.ts rename to src/tests/unit/ai-providers.test.ts diff --git a/tests/unit/generator.test.ts b/src/tests/unit/generator.test.ts similarity index 90% rename from tests/unit/generator.test.ts rename to src/tests/unit/generator.test.ts index bbcc76a..bf81aea 100644 --- a/tests/unit/generator.test.ts +++ b/src/tests/unit/generator.test.ts @@ -41,7 +41,6 @@ describe('AiWarpGenerator', () => { const generator = new AiWarpGenerator() generator.setConfig({ targetDirectory: dir, - aiWarpPackageJsonPath: join(import.meta.dirname, '..', '..', 'package.json'), aiProvider: 'openai', aiModel: 'gpt-3.5-turbo' }) @@ -57,7 +56,6 @@ describe('AiWarpGenerator', () => { const generator = new AiWarpGenerator() generator.setConfig({ targetDirectory: dir, - aiWarpPackageJsonPath: join(import.meta.dirname, '..', '..', 'package.json'), aiProvider: 'openai', aiModel: 'gpt-3.5-turbo' }) @@ -77,7 +75,6 @@ describe('AiWarpGenerator', () => { const generator = new AiWarpGenerator() generator.setConfig({ targetDirectory: dir, - aiWarpPackageJsonPath: join(import.meta.dirname, '..', '..', 'package.json'), aiProvider: 'openai', aiModel: 'gpt-3.5-turbo' }) @@ -112,7 +109,6 @@ describe('AiWarpGenerator', () => { const generator = new AiWarpGenerator() generator.setConfig({ targetDirectory: dir, - aiWarpPackageJsonPath: join(import.meta.dirname, '..', '..', 'package.json'), aiProvider: 'openai', aiModel: 'gpt-3.5-turbo' }) @@ -128,7 +124,6 @@ describe('AiWarpGenerator', () => { const generator = new AiWarpGenerator() generator.setConfig({ targetDirectory: dir, - aiWarpPackageJsonPath: join(import.meta.dirname, '..', '..', 'package.json'), aiProvider: 'openai', aiModel: 'gpt-3.5-turbo', plugin: true @@ -148,7 +143,6 @@ describe('AiWarpGenerator', () => { const generator = new AiWarpGenerator() generator.setConfig({ targetDirectory: dir, - aiWarpPackageJsonPath: join(import.meta.dirname, '..', '..', 'package.json'), aiProvider: 'openai', aiModel: 'gpt-3.5-turbo', plugin: true, diff --git a/tests/utils/auth.ts b/src/tests/utils/auth.ts similarity index 100% rename from tests/utils/auth.ts rename to src/tests/utils/auth.ts diff --git a/tests/utils/mocks/azure.ts b/src/tests/utils/mocks/azure.ts similarity index 100% rename from tests/utils/mocks/azure.ts rename to src/tests/utils/mocks/azure.ts diff --git a/tests/utils/mocks/base.ts b/src/tests/utils/mocks/base.ts similarity index 100% rename from tests/utils/mocks/base.ts rename to src/tests/utils/mocks/base.ts diff --git a/tests/utils/mocks/index.ts b/src/tests/utils/mocks/index.ts similarity index 100% rename from tests/utils/mocks/index.ts rename to src/tests/utils/mocks/index.ts diff --git a/tests/utils/mocks/llama2.ts b/src/tests/utils/mocks/llama2.ts similarity index 100% rename from tests/utils/mocks/llama2.ts rename to src/tests/utils/mocks/llama2.ts diff --git a/tests/utils/mocks/mistral.ts b/src/tests/utils/mocks/mistral.ts similarity index 100% rename from tests/utils/mocks/mistral.ts rename to src/tests/utils/mocks/mistral.ts diff --git a/tests/utils/mocks/ollama.ts b/src/tests/utils/mocks/ollama.ts similarity index 100% rename from tests/utils/mocks/ollama.ts rename to src/tests/utils/mocks/ollama.ts diff --git a/tests/utils/mocks/open-ai.ts b/src/tests/utils/mocks/open-ai.ts similarity index 100% rename from tests/utils/mocks/open-ai.ts rename to src/tests/utils/mocks/open-ai.ts diff --git a/tests/utils/stackable.ts b/src/tests/utils/stackable.ts similarity index 100% rename from tests/utils/stackable.ts rename to src/tests/utils/stackable.ts