Skip to content

Commit

Permalink
feat: add postman compatibility option
Browse files Browse the repository at this point in the history
  • Loading branch information
notdryft committed Nov 7, 2024
1 parent 01028a3 commit 7d9652b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
5 changes: 4 additions & 1 deletion js/cli/src/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { logger } from "../log";
export interface BundleOptions {
sourcesFolder: string;
bundleFile: string;
postman: boolean;
typescript: boolean;
simulations: SimulationFile[];
}
Expand All @@ -21,7 +22,9 @@ export const bundle = async (options: BundleOptions): Promise<void> => {
const contents = options.simulations.map((s) => `export { default as "${s.name}" } from "./${s.path}";`).join("\n");

const plugins = options.typescript ? [esbuildPluginTsc({ force: true })] : [];
plugins.push(polyfill());
if (options.postman) {
plugins.push(polyfill());
}
await esbuild.build({
stdin: {
contents,
Expand Down
6 changes: 5 additions & 1 deletion js/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Command } from "commander";
import {
bundleFileOption,
bundleFileOptionValue,
postmanOption,
postmanOptionValueWithDefaults,
sourcesFolderOption,
sourcesFolderOptionValue,
typescriptOption,
Expand All @@ -17,14 +19,16 @@ export default (program: Command): void => {
.description("Build Gatling simulations")
.addOption(sourcesFolderOption)
.addOption(bundleFileOption)
.addOption(postmanOption)
.addOption(typescriptOption)
.action(async (options) => {
const sourcesFolder: string = sourcesFolderOptionValue(options);
const bundleFile = bundleFileOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const postman = postmanOptionValueWithDefaults(options);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });
});
};
6 changes: 5 additions & 1 deletion js/cli/src/commands/enterpriseDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
packageDescriptorFilenameOptionValue,
packageFileOption,
packageFileOptionValue,
postmanOption,
postmanOptionValueWithDefaults,
resourcesFolderOption,
resourcesFolderOptionValue,
resultsFolderOption,
Expand All @@ -41,6 +43,7 @@ export default (program: Command): void => {
.addOption(resourcesFolderOption)
.addOption(bundleFileOption)
.addOption(resultsFolderOption)
.addOption(postmanOption)
.addOption(typescriptOption)
.addOption(gatlingHomeOption)
// Base
Expand All @@ -58,6 +61,7 @@ export default (program: Command): void => {
const sourcesFolder: string = sourcesFolderOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const postman = postmanOptionValueWithDefaults(options);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

const resourcesFolder: string = resourcesFolderOptionValue(options);
Expand All @@ -73,7 +77,7 @@ export default (program: Command): void => {
const packageFile = packageFileOptionValue(options);

const { graalvmHome, jvmClasspath } = await installGatlingJs({ gatlingHome });
await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
await enterpriseDeploy({
graalvmHome,
Expand Down
6 changes: 5 additions & 1 deletion js/cli/src/commands/enterprisePackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
bundleFileOptionValue,
packageFileOption,
packageFileOptionValue,
postmanOption,
postmanOptionValueWithDefaults,
resourcesFolderOption,
resourcesFolderOptionValue,
sourcesFolderOption,
Expand All @@ -24,6 +26,7 @@ export default (program: Command): void => {
.addOption(resourcesFolderOption)
.addOption(bundleFileOption)
.addOption(packageFileOption)
.addOption(postmanOption)
.addOption(typescriptOption)
.action(async (options) => {
const sourcesFolder: string = sourcesFolderOptionValue(options);
Expand All @@ -32,9 +35,10 @@ export default (program: Command): void => {
const packageFile = packageFileOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const postman = postmanOptionValueWithDefaults(options);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });

await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
});
Expand Down
6 changes: 5 additions & 1 deletion js/cli/src/commands/enterpriseStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
packageDescriptorFilenameOptionValue,
packageFileOption,
packageFileOptionValue,
postmanOption,
postmanOptionValueWithDefaults,
resourcesFolderOption,
resourcesFolderOptionValue,
resultsFolderOption,
Expand Down Expand Up @@ -49,6 +51,7 @@ export default (program: Command): void => {
.addOption(resourcesFolderOption)
.addOption(bundleFileOption)
.addOption(resultsFolderOption)
.addOption(postmanOption)
.addOption(typescriptOption)
.addOption(gatlingHomeOption)
// Base
Expand All @@ -71,6 +74,7 @@ export default (program: Command): void => {
const sourcesFolder: string = sourcesFolderOptionValue(options);

const simulations = await findSimulations(sourcesFolder);
const postman = postmanOptionValueWithDefaults(options);
const typescript = typescriptOptionValueWithDefaults(options, simulations);

const resourcesFolder: string = resourcesFolderOptionValue(options);
Expand All @@ -94,7 +98,7 @@ export default (program: Command): void => {
}

const { graalvmHome, jvmClasspath } = await installGatlingJs({ gatlingHome });
await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
await enterpriseStart({
graalvmHome,
Expand Down
20 changes: 20 additions & 0 deletions js/cli/src/commands/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Option, Argument } from "commander";
import fs from "fs";
import os from "os";

import { SimulationFile } from "../simulations";
Expand Down Expand Up @@ -195,6 +196,25 @@ export const nonInteractiveOption = new Option(
).default(false);
export const nonInteractiveOptionValue = getBooleanValueMandatory(nonInteractiveOption);

export const postmanOption = new Option("--postman", "Postman compatibility option: adds polyfills, etc.").hideHelp();
export const postmanOptionValueWithDefaults = (options: any): boolean => {
const postmanOptionValue = getBooleanValueOptional(postmanOption)(options);
if (postmanOptionValue !== undefined) {
return postmanOptionValue;
} else {
try {
const file: string = fs.readFileSync("package.json", { encoding: "utf-8", flag: "r" });
const conf = JSON.parse(file);
return (
conf.dependencies?.["@gatling.io/postman"] !== undefined ||
conf.devDependencies?.["@gatling.io/postman"] !== undefined
);
} catch {
return false;
}
}
};

export const runParametersArgument = new Argument(
"[optionKey=optionValue...]",
"Specify one or more parameter which can be read in the simulation script with the getParameter() function; format must be key=value"
Expand Down
6 changes: 5 additions & 1 deletion js/cli/src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
nonInteractiveOption,
nonInteractiveOptionValue,
parseRunParametersArgument,
postmanOption,
postmanOptionValueWithDefaults,
resourcesFolderOption,
resourcesFolderOptionValue,
resultsFolderOption,
Expand Down Expand Up @@ -41,6 +43,7 @@ export default (program: Command): void => {
.addOption(resultsFolderOption)
.addOption(gatlingHomeOption)
.addOption(memoryOption)
.addOption(postmanOption)
.addOption(nonInteractiveOption)
.addArgument(runParametersArgument)
.action(async (args: string[], options) => {
Expand All @@ -51,6 +54,7 @@ export default (program: Command): void => {
const resultsFolder: string = resultsFolderOptionValue(options);
const memory: number | undefined = memoryOptionValue(options);
const nonInteractive: boolean = nonInteractiveOptionValue(options);
const postman = postmanOptionValueWithDefaults(options);
const runParameters = parseRunParametersArgument(args);

const simulations = await findSimulations(sourcesFolder);
Expand All @@ -62,7 +66,7 @@ export default (program: Command): void => {
logger.debug(`coursierBinary=${coursierBinary}`);
logger.debug(`jvmClasspath=${jvmClasspath}`);

await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });

await runSimulation({
graalvmHome,
Expand Down

0 comments on commit 7d9652b

Please sign in to comment.