From aaa489f3ecf45d0142ddc1937b342b984aee2a57 Mon Sep 17 00:00:00 2001 From: Godwin Alexander Date: Thu, 14 Sep 2023 12:09:54 +0100 Subject: [PATCH] fix: fix config resolution (#517) --- .../client/{functions => func}/index.ts | 0 .../trendingAnimeListRecieverController.ts | 0 .../server/auth/trendingAnimeServer.ts | 2 +- examples/kafka-test/package-lock.json | 43 ++---- src/lib/configs.ts | 24 ++- test/lib/config.spec.ts | 145 +++++++++++++++--- 6 files changed, 156 insertions(+), 58 deletions(-) rename examples/anime-http/client/{functions => func}/index.ts (100%) rename examples/anime-http/client/{functions => func}/trendingAnimeListRecieverController.ts (100%) diff --git a/examples/anime-http/client/functions/index.ts b/examples/anime-http/client/func/index.ts similarity index 100% rename from examples/anime-http/client/functions/index.ts rename to examples/anime-http/client/func/index.ts diff --git a/examples/anime-http/client/functions/trendingAnimeListRecieverController.ts b/examples/anime-http/client/func/trendingAnimeListRecieverController.ts similarity index 100% rename from examples/anime-http/client/functions/trendingAnimeListRecieverController.ts rename to examples/anime-http/client/func/trendingAnimeListRecieverController.ts diff --git a/examples/anime-http/server/auth/trendingAnimeServer.ts b/examples/anime-http/server/auth/trendingAnimeServer.ts index 7773225a3..d0690aa62 100644 --- a/examples/anime-http/server/auth/trendingAnimeServer.ts +++ b/examples/anime-http/server/auth/trendingAnimeServer.ts @@ -13,7 +13,7 @@ export async function serverAuth({ authProps, done }) { console.log("token", authProps.getToken()) console.log("userpassword", authProps.getUserPass()) - done(false) + done(true) } export async function clientAuth({ serverName }) { diff --git a/examples/kafka-test/package-lock.json b/examples/kafka-test/package-lock.json index 3556fffd0..d3405f8e4 100644 --- a/examples/kafka-test/package-lock.json +++ b/examples/kafka-test/package-lock.json @@ -15,12 +15,12 @@ }, "../..": { "name": "@asyncapi/glee", - "version": "0.10.17", + "version": "0.26.2", "license": "Apache-2.0", "dependencies": { - "@asyncapi/generator": "^1.9.14", - "@asyncapi/html-template": "^0.24.10", - "@asyncapi/markdown-template": "^1.1.1", + "@asyncapi/generator": "^1.10.14", + "@asyncapi/html-template": "^0.28.4", + "@asyncapi/markdown-template": "^1.3.3", "@asyncapi/parser": "^1.13.1", "@types/jest": "^27.4.0", "@types/qs": "^6.9.7", @@ -36,8 +36,9 @@ "emojis": "^1.0.10", "eslint-plugin-github": "^4.3.5", "eslint-plugin-security": "^1.4.0", + "got": "^12.5.3", "kafkajs": "^2.2.3", - "mqtt": "^4.2.6", + "mqtt": "^4.3.7", "path-to-regexp": "^6.2.0", "qs": "^6.11.0", "redis": "^4.0.2", @@ -55,10 +56,6 @@ "glee": "dist/cli/index.js" }, "devDependencies": { - "@semantic-release/commit-analyzer": "^8.0.1", - "@semantic-release/github": "^7.0.4", - "@semantic-release/npm": "^7.0.6", - "@semantic-release/release-notes-generator": "^9.0.1", "@tsconfig/node14": "^1.0.1", "@types/async": "^3.2.11", "@types/debug": "^4.1.7", @@ -68,20 +65,18 @@ "@typescript-eslint/eslint-plugin": "^5.9.0", "@typescript-eslint/parser": "^5.9.0", "all-contributors-cli": "^6.14.2", - "conventional-changelog-conventionalcommits": "^4.4.0", "eslint": "^8.6.0", "eslint-plugin-jest": "^23.8.2", - "eslint-plugin-sonarjs": "^0.5.0", + "eslint-plugin-sonarjs": "^0.19.0", "fs-extra": "^10.1.0", "jest": "^27.4.7", "jest-extended": "^1.2.0", "jsdoc-to-markdown": "^5.0.3", "markdown-toc": "^1.2.0", "rimraf": "^3.0.2", - "semantic-release": "^17.2.2", "ts-jest": "^27.1.2", "tsc-watch": "^4.5.0", - "typedoc": "^0.22.10", + "typedoc": "^0.23.28", "typedoc-plugin-markdown": "^3.11.8", "unixify": "^1.0.0" }, @@ -7599,14 +7594,10 @@ "@asyncapi/glee": { "version": "file:../..", "requires": { - "@asyncapi/generator": "^1.9.14", - "@asyncapi/html-template": "^0.24.10", - "@asyncapi/markdown-template": "^1.1.1", + "@asyncapi/generator": "^1.10.14", + "@asyncapi/html-template": "^0.28.4", + "@asyncapi/markdown-template": "^1.3.3", "@asyncapi/parser": "^1.13.1", - "@semantic-release/commit-analyzer": "^8.0.1", - "@semantic-release/github": "^7.0.4", - "@semantic-release/npm": "^7.0.6", - "@semantic-release/release-notes-generator": "^9.0.1", "@tsconfig/node14": "^1.0.1", "@types/async": "^3.2.11", "@types/debug": "^4.1.7", @@ -7623,7 +7614,6 @@ "better-ajv-errors": "^0.7.0", "bufferutil": "^4.0.3", "chalk": "^4.1.1", - "conventional-changelog-conventionalcommits": "^4.4.0", "cross-spawn": "^7.0.3", "debug": "^4.3.1", "dotenv": "^10.0.0", @@ -7633,24 +7623,24 @@ "eslint-plugin-github": "^4.3.5", "eslint-plugin-jest": "^23.8.2", "eslint-plugin-security": "^1.4.0", - "eslint-plugin-sonarjs": "^0.5.0", + "eslint-plugin-sonarjs": "^0.19.0", "fs-extra": "^10.1.0", + "got": "^12.5.3", "jest": "^27.4.7", "jest-extended": "^1.2.0", "jsdoc-to-markdown": "^5.0.3", "kafkajs": "^2.2.3", "markdown-toc": "^1.2.0", - "mqtt": "^4.2.6", + "mqtt": "^4.3.7", "path-to-regexp": "^6.2.0", "qs": "^6.11.0", "redis": "^4.0.2", "rimraf": "^3.0.2", - "semantic-release": "^17.2.2", "socket.io": "^4.1.2", "terminal-image": "^2.0.0", "ts-jest": "^27.1.2", "tsc-watch": "^4.5.0", - "typedoc": "^0.22.10", + "typedoc": "^0.23.28", "typedoc-plugin-markdown": "^3.11.8", "typescript": "^4.5.4", "unixify": "^1.0.0", @@ -11351,8 +11341,7 @@ } }, "mqtt": { - "version": "4.3.4", - "resolved": "https://registry.npmjs.org/mqtt/-/mqtt-4.3.4.tgz", + "version": "https://registry.npmjs.org/mqtt/-/mqtt-4.3.4.tgz", "integrity": "sha512-yAVDfVHz3Cjn6K68z54mf7fTni/AWsPhiEsRwZSvet2wO47R6NFUn2psWxYIph2JxWtL3ZKa/da8pjJKSaXPdQ==", "requires": { "commist": "^1.0.0", diff --git a/src/lib/configs.ts b/src/lib/configs.ts index 0e594245c..9ea4cf027 100644 --- a/src/lib/configs.ts +++ b/src/lib/configs.ts @@ -5,6 +5,8 @@ import { logErrorLine, logWarningMessage } from './logger.js' interface Config { functionsDir?: string + lifecycleDir?: string + authDir?: string } let GLEE_DIR: string @@ -29,9 +31,9 @@ export async function initializeConfigs( ) GLEE_FUNCTIONS_DIR = path.resolve( GLEE_DIR, - config.functionsDir || 'functions' + config.lifecycleDir || 'functions' ) - GLEE_AUTH_DIR = path.resolve(GLEE_DIR, config.functionsDir || 'auth') + GLEE_AUTH_DIR = path.resolve(GLEE_DIR, config.authDir || 'auth') GLEE_CONFIG_FILE_PATH_TS = path.resolve(GLEE_DIR, 'glee.config.ts') GLEE_CONFIG_FILE_PATH_JS = path.resolve(GLEE_DIR, 'glee.config.js') @@ -76,7 +78,7 @@ function isFileReadable(filePath: string) { /** * Loads the configuration from glee project. */ -async function loadConfigsFromFile() { +export async function loadConfigsFromFile() { if (!isFileReadable(GLEE_CONFIG_FILE_PATH)) return try { let { default: projectConfigs } = await import( @@ -87,10 +89,18 @@ async function loadConfigsFromFile() { } if (!projectConfigs) return - GLEE_DIR = projectConfigs.glee?.gleeDir || GLEE_DIR - GLEE_LIFECYCLE_DIR = projectConfigs.glee?.lifecycleDir ?? GLEE_LIFECYCLE_DIR - GLEE_FUNCTIONS_DIR = projectConfigs.glee?.functionsDir ?? GLEE_FUNCTIONS_DIR - GLEE_AUTH_DIR = projectConfigs.glee?.authDir ?? GLEE_AUTH_DIR + GLEE_DIR = projectConfigs.glee?.gleeDir + ? path.resolve(GLEE_PROJECT_DIR, projectConfigs.glee?.gleeDir) + : GLEE_DIR + GLEE_FUNCTIONS_DIR = projectConfigs.glee?.functionsDir + ? path.resolve(GLEE_DIR, projectConfigs.glee?.functionsDir) + : GLEE_FUNCTIONS_DIR + GLEE_LIFECYCLE_DIR = projectConfigs.glee?.lifecycleDir + ? path.resolve(GLEE_DIR, projectConfigs.glee?.lifecycleDir) + : GLEE_LIFECYCLE_DIR + GLEE_AUTH_DIR = projectConfigs.glee?.authDir + ? path.resolve(GLEE_DIR, projectConfigs.glee?.authDir) + : GLEE_AUTH_DIR ASYNCAPI_FILE_PATH = projectConfigs.glee?.asyncapiFilePath ?? ASYNCAPI_FILE_PATH return projectConfigs diff --git a/test/lib/config.spec.ts b/test/lib/config.spec.ts index 7ff72fdec..1e3e7854a 100644 --- a/test/lib/config.spec.ts +++ b/test/lib/config.spec.ts @@ -1,34 +1,133 @@ import fs from 'fs' import path from 'path' -import { findSpecFile } from '../../src/lib/configs.js' +import { + findSpecFile, + loadConfigsFromFile, + initializeConfigs, + getConfigs, +} from '../../src/lib/configs.js' const yamlPath = path.resolve('./asyncapi.yaml') const jsonPath = path.resolve('./asyncapi.json') + +const gleePath = './.glee' +const configFile = '/glee.config.js' +const configsPath = path.join(gleePath, configFile) +const [lifecycleDir, functionDir, authDir] = ['./lifecy', './func', './authSec'] +// const cwd = process.cwd() + describe('Tests resolving the AsyncAPI file path.', () => { - afterEach(async () => { - const promises = [jsonPath, yamlPath].map(async (file) => fs.unlinkSync(file)) - await Promise.allSettled(promises) - }) + afterEach(async () => { + const promises = [jsonPath, yamlPath].map(async (file) => + fs.unlinkSync(file) + ) + await Promise.allSettled(promises) + }) - test('Should return undefined if AsyncAPI file doesn\'t exists.', async () => { - expect(findSpecFile("")).toBe(undefined) - }) + test("Should return undefined if AsyncAPI file doesn't exists.", async () => { + expect(findSpecFile('')).toBe(undefined) + }) - test('Should return undefined if there are multiple AsyncAPI spec files.', async () => { - fs.writeFileSync(jsonPath, '') - fs.writeFileSync(yamlPath, '') - expect(findSpecFile("")).toBe(undefined) - }) + test('Should return undefined if there are multiple AsyncAPI spec files.', async () => { + fs.writeFileSync(jsonPath, '') + fs.writeFileSync(yamlPath, '') + expect(findSpecFile('')).toBe(undefined) + }) - test('Should fails if asyncapi.json is a folder.', async () => { - fs.mkdirSync(jsonPath) - expect(findSpecFile("")).toBe(undefined) - fs.rmdirSync(jsonPath) - }) + test('Should fails if asyncapi.json is a folder.', async () => { + fs.mkdirSync(jsonPath) + expect(findSpecFile('')).toBe(undefined) + fs.rmdirSync(jsonPath) + }) + + test('Should succeed in finding AsyncAPI spec if there is only one.', async () => { + fs.writeFileSync(jsonPath, '') + const resultingPath = findSpecFile('') + expect(resultingPath).toBe(jsonPath) + }) +}) + +describe('Test resolving the config file path when no config is initialized', () => { + beforeEach(async () => {}) + + test('getConfigs function should return undefined for configs', () => { + expect(getConfigs().GLEE_CONFIG_FILE_PATH).toBe(undefined) + expect(getConfigs().ASYNCAPI_FILE_PATH).toBe(undefined) + }) + + test('getConfigs function should return undefined for config file properties', () => { + expect(getConfigs().GLEE_LIFECYCLE_DIR).toBeUndefined() + expect(getConfigs().GLEE_FUNCTIONS_DIR).toBeUndefined() + expect(getConfigs().GLEE_AUTH_DIR).toBeUndefined() + }) - test('Should succeed in finding AsyncAPI spec if there is only one.', async () => { - fs.writeFileSync(jsonPath, '') - const resultingPath = findSpecFile('') - expect(resultingPath).toBe(jsonPath) + test('if no config file is found return undefined', async () => { + expect(await loadConfigsFromFile()).toBeUndefined() + }) +}) + +describe('check config features when config is initialized', () => { + beforeEach(async () => { + fs.writeFileSync(yamlPath, '') + const conf = `export default async function () { + return { + glee: { // Glee core configurations + lifecycleDir: '${lifecycleDir}', + functionsDir: '${functionDir}', + authDir: '${authDir}' + } + } + }` + fs.mkdirSync(gleePath) + fs.writeFileSync(configsPath, conf) + await initializeConfigs() + }) + + afterEach(async () => { + const promises = [configsPath, yamlPath].map(async (file) => + fs.unlinkSync(file) + ) + await Promise.allSettled(promises) + fs.rmSync(gleePath, { + recursive: true, + force: true, }) -}) \ No newline at end of file + }) + + test('loadConfigFromFile should return defined value when there is a config file', async () => { + expect(await loadConfigsFromFile()).toBeDefined() + }) + + test('Should read config data from the current working directory', () => { + console.log('config_file_path', getConfigs()) + expect(getConfigs().GLEE_PROJECT_DIR).toBe(process.cwd()) + }) + + test('expect glee dir to be /.glee in current working directory', () => { + expect(getConfigs().GLEE_DIR).toBe(path.join(process.cwd(), gleePath)) + }) + + test('GLEE_CONFIG_FILE_PATH should be glee.config.js', () => { + expect(getConfigs().GLEE_CONFIG_FILE_PATH).toBe( + path.join(process.cwd(), configsPath) + ) + }) + + test('lifecycle folder should be same as set in config file', () => { + expect(getConfigs().GLEE_LIFECYCLE_DIR).toBe( + path.join(process.cwd(), gleePath, lifecycleDir) + ) + }) + + test('function folder should be same as set in config file', () => { + expect(getConfigs().GLEE_FUNCTIONS_DIR).toBe( + path.join(process.cwd(), gleePath, functionDir) + ) + }) + + test('Auth folder should be same as set in config file', () => { + expect(getConfigs().GLEE_AUTH_DIR).toBe( + path.join(process.cwd(), gleePath, authDir) + ) + }) +})