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 6, 2024
1 parent ee85d93 commit ac45289
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 11 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 });
});
};
8 changes: 6 additions & 2 deletions 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,8 +77,8 @@ export default (program: Command): void => {
const packageFile = packageFileOptionValue(options);

const { graalvmHome, jvmClasspath } = await installGatlingJs({ gatlingHome });
await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, postman, simulations });
await enterpriseDeploy({
graalvmHome,
jvmClasspath,
Expand Down
8 changes: 6 additions & 2 deletions 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,10 +35,11 @@ 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 });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, postman, simulations });
});
};
8 changes: 6 additions & 2 deletions 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,8 +98,8 @@ export default (program: Command): void => {
}

const { graalvmHome, jvmClasspath } = await installGatlingJs({ gatlingHome });
await bundle({ sourcesFolder, bundleFile, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, simulations });
await bundle({ sourcesFolder, bundleFile, postman, typescript, simulations });
await enterprisePackage({ bundleFile, resourcesFolder, packageFile, postman, simulations });
await enterpriseStart({
graalvmHome,
jvmClasspath,
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",
"Add postman compatibility options: polyfills, etc."
).hideHelp();
export const postmanOptionValueWithDefaults = (
options: any,
): boolean => {
const postmanOptionValue = getBooleanValueOptional(waitForRunEndOption)(options);
if (postmanOptionValue !== undefined) {
return postmanOptionValue;
} else {
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;
}
};

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
7 changes: 6 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: boolean = 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 All @@ -72,6 +76,7 @@ export default (program: Command): void => {
resourcesFolder,
resultsFolder,
memory,
postman,
runParameters
});
});
Expand Down
5 changes: 5 additions & 0 deletions js/cli/src/commands/runOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
memoryOption,
memoryOptionValue,
parseRunParametersArgument,
postmanOption,
postmanOptionValueWithDefaults,
resourcesFolderOption,
resourcesFolderOptionValue,
resultsFolderOption,
Expand All @@ -31,6 +33,7 @@ export default (program: Command): void => {
.addOption(resourcesFolderOption)
.addOption(resultsFolderOption)
.addOption(memoryOption)
.addOption(postmanOption)
.addArgument(runParametersArgument)
.action(async (args: string[], options) => {
const graalvmHome: string = graalvmHomeMandatoryOptionValue(options);
Expand All @@ -40,6 +43,7 @@ export default (program: Command): void => {
const resourcesFolder: string = resourcesFolderOptionValue(options);
const resultsFolder: string = resultsFolderOptionValue(options);
const memory: number | undefined = memoryOptionValue(options);
const postman = postmanOptionValueWithDefaults(options);
const runParameters = parseRunParametersArgument(args);

await runSimulation({
Expand All @@ -50,6 +54,7 @@ export default (program: Command): void => {
resourcesFolder,
resultsFolder,
memory,
postman,
runParameters
});
});
Expand Down
10 changes: 8 additions & 2 deletions js/cli/src/enterprise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface EnterprisePackageOptions {
bundleFile: string;
resourcesFolder: string;
packageFile: string;
postman: boolean;
simulations: SimulationFile[];
}

Expand All @@ -20,7 +21,7 @@ export const enterprisePackage = async (options: EnterprisePackageOptions): Prom
- bundleFile: ${options.bundleFile}
- packageFile: ${options.packageFile}`);

const manifest = generateManifest(options.simulations.map((s) => s.name));
const manifest = generateManifest(options);

const output = fs.createWriteStream(options.packageFile);

Expand All @@ -42,7 +43,9 @@ export const enterprisePackage = async (options: EnterprisePackageOptions): Prom
logger.info(`Package for Gatling Enterprise created at ${options.packageFile}`);
};

const generateManifest = (simulationNames: string[]) => {
const generateManifest = (options: EnterprisePackageOptions) => {
const simulationNames = options.simulations.map((s) => s.name);

const utf8Encoder = new TextEncoder();
const eol = utf8Encoder.encode("\n");
const continuation = utf8Encoder.encode("\n ");
Expand All @@ -56,6 +59,9 @@ const generateManifest = (simulationNames: string[]) => {
`Gatling-Simulations: ${simulationNames.join(",")}`,
`Java-Version: ${versions.java.compilerRelease}`
];
if (options.postman) {
lines.push("Gatling-Extensions: postman");
}
const pkg = getPackageNameAndVersion();
lines.push(`Implementation-Title: ${pkg.name}`);
if (pkg.version !== undefined) {
Expand Down
1 change: 1 addition & 0 deletions js/cli/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface RunSimulationOptions extends RunJavaProcessOptions {
resourcesFolder: string;
resultsFolder: string;
memory?: number;
postman: boolean;
runParameters: Record<string, string>;
}

Expand Down

0 comments on commit ac45289

Please sign in to comment.