From f5e59c17a4ba951fbc032dc099298cd73d26dd03 Mon Sep 17 00:00:00 2001 From: Ronit Agarwala Date: Mon, 4 Dec 2023 16:09:53 -0500 Subject: [PATCH] Add function to call OpenSearch api file from user. OpenSearch API is called from both sandbox and deploy.end --- index.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/index.ts b/index.ts index 1498de7..e20de3b 100644 --- a/index.ts +++ b/index.ts @@ -6,6 +6,9 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { exists } from './paths' +import { join } from 'path' +import { pathToFileURL } from 'url' import { launch } from './run.js' import type { LocalSearch } from './run.js' import { populate } from './data.js' @@ -39,6 +42,25 @@ function getConfig(arc: { else return {} } +const openSearchApiFile = 'openSearchApi.js' + +async function executeSearchRequests(cwd: string) { + //Load api call file and run all api calls to cluster + const apiPath = join(cwd, openSearchApiFile) + let result + if (await exists(apiPath)) { + console.log(`Found ${openSearchApiFile} file, running api calls...`) + result = (await import(pathToFileURL(apiPath).toString())).default + + // result should be a function that returns a promise + if (typeof result === 'function') { + result = result() + } + + await result + } +} + export const deploy = { // @ts-expect-error: The Architect plugins API has no type definitions. start({ cloudformation, inventory, arc, stage }) { @@ -64,6 +86,10 @@ export const deploy = { return serverlessServices } }, + // @ts-expect-error: The Architect plugins API has no type definitions. + async end({ inventory }) { + executeSearchRequests(inventory.inv._project.cwd) + }, } let local: LocalSearch @@ -86,8 +112,10 @@ export const sandbox = { }) { const engine = getEngine(getConfig(arc).sandboxEngine) local = await launch({ engine }) + await executeSearchRequests(cwd) await populate(cwd, { node: local.url }) }, + async end() { await local.stop() },