Skip to content

Commit

Permalink
feat: Properly handle package name and version
Browse files Browse the repository at this point in the history
  • Loading branch information
guilgaly committed Jul 24, 2024
1 parent 0cb3669 commit 05e7548
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions js/cli/src/enterprise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const generateManifest = (simulationNames: string[]) => {
const continuation = utf8Encoder.encode("\n ");
const lines = [
"Manifest-Version: 1.0",
"Implementation-Title: gatling-javascript",
`Implementation-Version: ${versions.gatling.jsAdapter}`,
"Implementation-Vendor: GatlingCorp",
"Specification-Vendor: GatlingCorp",
"Gatling-Context: js",
`Gatling-Version: ${versions.gatling.core}`,
Expand All @@ -59,6 +56,11 @@ const generateManifest = (simulationNames: string[]) => {
`Gatling-Simulations: ${simulationNames.join(",")}`,
`Java-Version: ${versions.graalvm.jdk.split(".")[0]}`
];
const pkg = getPackageNameAndVersion();
lines.push(`Implementation-Title: ${pkg.name}`);
if (pkg.version !== undefined) {
lines.push(`Implementation-Version: ${pkg.version}`);
}

let totalLength = 0;
const buffer: Uint8Array[] = [];
Expand Down Expand Up @@ -93,6 +95,30 @@ const generateManifest = (simulationNames: string[]) => {
return manifest;
};

const getPackageNameAndVersion = (): { name: string; version?: string } => {
// npm_package_* env vars are available when launching CLI with npx
let name = process.env.npm_package_name;
let version = process.env.npm_package_version;
// otherwise, try to read from package.json file
if (name === undefined || version === undefined) {
if (!fs.existsSync("package.json")) {
throw Error("package.json not found");
}
const pkg = JSON.parse(fs.readFileSync("package.json", "utf-8"));
if (name === undefined) {
if (typeof pkg.name === "string") {
name = pkg.name;
} else {
throw Error("package.json does not contain a valid package name");
}
}
if (version === undefined && typeof pkg.version === "string") {
version = pkg.version;
}
}
return { name: name as string, version: version };
};

export interface EnterprisePluginOptions extends RunJavaProcessOptions {
bundleFile: string;
resourcesFolder: string;
Expand Down Expand Up @@ -144,9 +170,7 @@ const javaArgsFromDeployOptions = (options: EnterpriseDeployOptions) => {

// Deployment info
javaArgs.push(`-Dgatling.enterprise.packageFile=${options.packageFile}`);
if (process.env.npm_package_name !== undefined) {
javaArgs.push(`-Dgatling.enterprise.artifactId=${process.env.npm_package_name}`);
}
javaArgs.push(`-Dgatling.enterprise.artifactId=${getPackageNameAndVersion().name}`);

return javaArgs;
};
Expand Down

0 comments on commit 05e7548

Please sign in to comment.