From ac452897c458000d9545e00476ef346166ff12ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Corr=C3=A9?= Date: Wed, 6 Nov 2024 16:03:33 +0100 Subject: [PATCH] feat: add postman compatibility option --- js/cli/src/bundle/index.ts | 5 ++++- js/cli/src/commands/build.ts | 6 +++++- js/cli/src/commands/enterpriseDeploy.ts | 8 ++++++-- js/cli/src/commands/enterprisePackage.ts | 8 ++++++-- js/cli/src/commands/enterpriseStart.ts | 8 ++++++-- js/cli/src/commands/options.ts | 20 ++++++++++++++++++++ js/cli/src/commands/run.ts | 7 ++++++- js/cli/src/commands/runOnly.ts | 5 +++++ js/cli/src/enterprise.ts | 10 ++++++++-- js/cli/src/run.ts | 1 + 10 files changed, 67 insertions(+), 11 deletions(-) diff --git a/js/cli/src/bundle/index.ts b/js/cli/src/bundle/index.ts index ebfbc3b..6b09a3c 100644 --- a/js/cli/src/bundle/index.ts +++ b/js/cli/src/bundle/index.ts @@ -8,6 +8,7 @@ import { logger } from "../log"; export interface BundleOptions { sourcesFolder: string; bundleFile: string; + postman: boolean; typescript: boolean; simulations: SimulationFile[]; } @@ -21,7 +22,9 @@ export const bundle = async (options: BundleOptions): Promise => { 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, diff --git a/js/cli/src/commands/build.ts b/js/cli/src/commands/build.ts index beaf5ab..b77248d 100644 --- a/js/cli/src/commands/build.ts +++ b/js/cli/src/commands/build.ts @@ -3,6 +3,8 @@ import { Command } from "commander"; import { bundleFileOption, bundleFileOptionValue, + postmanOption, + postmanOptionValueWithDefaults, sourcesFolderOption, sourcesFolderOptionValue, typescriptOption, @@ -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 }); }); }; diff --git a/js/cli/src/commands/enterpriseDeploy.ts b/js/cli/src/commands/enterpriseDeploy.ts index ac1a541..d97ff29 100644 --- a/js/cli/src/commands/enterpriseDeploy.ts +++ b/js/cli/src/commands/enterpriseDeploy.ts @@ -15,6 +15,8 @@ import { packageDescriptorFilenameOptionValue, packageFileOption, packageFileOptionValue, + postmanOption, + postmanOptionValueWithDefaults, resourcesFolderOption, resourcesFolderOptionValue, resultsFolderOption, @@ -41,6 +43,7 @@ export default (program: Command): void => { .addOption(resourcesFolderOption) .addOption(bundleFileOption) .addOption(resultsFolderOption) + .addOption(postmanOption) .addOption(typescriptOption) .addOption(gatlingHomeOption) // Base @@ -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); @@ -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, diff --git a/js/cli/src/commands/enterprisePackage.ts b/js/cli/src/commands/enterprisePackage.ts index c3ed3ba..7395ffe 100644 --- a/js/cli/src/commands/enterprisePackage.ts +++ b/js/cli/src/commands/enterprisePackage.ts @@ -5,6 +5,8 @@ import { bundleFileOptionValue, packageFileOption, packageFileOptionValue, + postmanOption, + postmanOptionValueWithDefaults, resourcesFolderOption, resourcesFolderOptionValue, sourcesFolderOption, @@ -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); @@ -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 }); }); }; diff --git a/js/cli/src/commands/enterpriseStart.ts b/js/cli/src/commands/enterpriseStart.ts index 40f929e..7e01587 100644 --- a/js/cli/src/commands/enterpriseStart.ts +++ b/js/cli/src/commands/enterpriseStart.ts @@ -17,6 +17,8 @@ import { packageDescriptorFilenameOptionValue, packageFileOption, packageFileOptionValue, + postmanOption, + postmanOptionValueWithDefaults, resourcesFolderOption, resourcesFolderOptionValue, resultsFolderOption, @@ -49,6 +51,7 @@ export default (program: Command): void => { .addOption(resourcesFolderOption) .addOption(bundleFileOption) .addOption(resultsFolderOption) + .addOption(postmanOption) .addOption(typescriptOption) .addOption(gatlingHomeOption) // Base @@ -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); @@ -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, diff --git a/js/cli/src/commands/options.ts b/js/cli/src/commands/options.ts index 23134d0..8c99016 100644 --- a/js/cli/src/commands/options.ts +++ b/js/cli/src/commands/options.ts @@ -1,4 +1,5 @@ import { Option, Argument } from "commander"; +import fs from "fs"; import os from "os"; import { SimulationFile } from "../simulations"; @@ -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" diff --git a/js/cli/src/commands/run.ts b/js/cli/src/commands/run.ts index 00851cf..37273fc 100644 --- a/js/cli/src/commands/run.ts +++ b/js/cli/src/commands/run.ts @@ -9,6 +9,8 @@ import { nonInteractiveOption, nonInteractiveOptionValue, parseRunParametersArgument, + postmanOption, + postmanOptionValueWithDefaults, resourcesFolderOption, resourcesFolderOptionValue, resultsFolderOption, @@ -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) => { @@ -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); @@ -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, @@ -72,6 +76,7 @@ export default (program: Command): void => { resourcesFolder, resultsFolder, memory, + postman, runParameters }); }); diff --git a/js/cli/src/commands/runOnly.ts b/js/cli/src/commands/runOnly.ts index 944f69e..7736b61 100644 --- a/js/cli/src/commands/runOnly.ts +++ b/js/cli/src/commands/runOnly.ts @@ -10,6 +10,8 @@ import { memoryOption, memoryOptionValue, parseRunParametersArgument, + postmanOption, + postmanOptionValueWithDefaults, resourcesFolderOption, resourcesFolderOptionValue, resultsFolderOption, @@ -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); @@ -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({ @@ -50,6 +54,7 @@ export default (program: Command): void => { resourcesFolder, resultsFolder, memory, + postman, runParameters }); }); diff --git a/js/cli/src/enterprise.ts b/js/cli/src/enterprise.ts index e2d9c65..30e000e 100644 --- a/js/cli/src/enterprise.ts +++ b/js/cli/src/enterprise.ts @@ -12,6 +12,7 @@ export interface EnterprisePackageOptions { bundleFile: string; resourcesFolder: string; packageFile: string; + postman: boolean; simulations: SimulationFile[]; } @@ -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); @@ -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 "); @@ -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) { diff --git a/js/cli/src/run.ts b/js/cli/src/run.ts index 768c1d8..30d08d7 100644 --- a/js/cli/src/run.ts +++ b/js/cli/src/run.ts @@ -8,6 +8,7 @@ export interface RunSimulationOptions extends RunJavaProcessOptions { resourcesFolder: string; resultsFolder: string; memory?: number; + postman: boolean; runParameters: Record; }