-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from radekBednarik/dev
Dev
- Loading branch information
Showing
7 changed files
with
153 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import Client from "../../client/client.js"; | ||
import { logger } from "../../log/logger.js"; | ||
import { getAllFilePaths, readJsonFile } from "../../io/io.js"; | ||
import { globalOptsHandler } from "./globalOpts.handler.js"; | ||
import PQueue from "p-queue"; | ||
import { resolve } from "path"; | ||
import type { OptionValues } from "commander"; | ||
import type { PathOrRequestDefinition } from "mockserver-client/mockServerClient.js"; | ||
import { Expectation } from "mockserver-client"; | ||
|
||
const log = logger.child({ module: "get-active" }); | ||
|
||
async function getActiveExpectation(client: Client, expectations: PathOrRequestDefinition) { | ||
try { | ||
const response = await client.getActiveExpectations(expectations); | ||
|
||
return response; | ||
} catch (error: any) { | ||
log.error("Error getting active expectations:", error); | ||
return null; | ||
} | ||
} | ||
|
||
export async function getActiveHandler(paths: string[], options: OptionValues) { | ||
try { | ||
log.trace(`Handling get-active command with args: ${JSON.stringify({ paths, options })}`); | ||
|
||
const opts = await globalOptsHandler(options); | ||
const concurrency = parseInt(opts["concurrency"]); | ||
|
||
const allPaths: string[] = []; | ||
|
||
for (const path of paths) { | ||
const expectationsPaths = await getAllFilePaths(path); | ||
allPaths.push(...expectationsPaths); | ||
} | ||
|
||
log.trace(`All expectations filepaths resolved to: ${JSON.stringify(allPaths)}`); | ||
|
||
const client = new Client({ | ||
proto: opts["config"].proto, | ||
host: opts["config"].host, | ||
port: opts["config"].port, | ||
}); | ||
|
||
const activeExpectations: Expectation[] = []; | ||
const queue = new PQueue({ concurrency }); | ||
|
||
queue.on("completed", (result) => { | ||
log.trace(`Retrieved active expectation: ${JSON.stringify(result)}`); | ||
activeExpectations.push(result); | ||
}); | ||
|
||
log.trace(`Expectations will be set with promises concurrency: ${concurrency}`); | ||
|
||
for (const path of allPaths) { | ||
const fullPath = resolve(path); | ||
|
||
log.trace(`Getting active expectations from file: ${fullPath}`); | ||
|
||
const expectations: Expectation[] = await readJsonFile(fullPath); | ||
|
||
for (const expectation of expectations) { | ||
queue.add(() => getActiveExpectation(client, expectation)); | ||
} | ||
} | ||
|
||
await queue.onIdle(); | ||
|
||
return activeExpectations; | ||
} catch (error: any) { | ||
log.error(error.message); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters