Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add utility functions to get module type #3405

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/artillery/lib/util/prepare-test-execution-plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const csv = require('csv-parse');
const fs = require('node:fs');
const path = require('node:path');
const p = require('util').promisify;
const { isPackageESM } = require('@artilleryio/int-core').util;

const {
readScript,
Expand Down Expand Up @@ -118,6 +119,8 @@ function replaceProcessorIfTypescript(script, scriptPath) {
return script;
}

const packageType = isPackageESM(scriptPath) ? 'esm' : 'cjs';

const actualProcessorPath = path.resolve(
path.dirname(scriptPath),
relativeProcessorPath
Expand All @@ -140,7 +143,7 @@ function replaceProcessorIfTypescript(script, scriptPath) {
outfile: newProcessorPath,
bundle: true,
platform: 'node',
format: 'cjs',
format: packageType,
sourcemap: 'inline',
external: ['@playwright/test', ...userExternalPackages]
});
Expand Down
3 changes: 2 additions & 1 deletion packages/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ module.exports = {
engine_http: require('./lib/engine_http'),
ssms: require('./lib/ssms'),
isIdlePhase: require('./lib/is-idle-phase'),
updateGlobalObject
updateGlobalObject,
util: require('./lib/util')
};
3 changes: 2 additions & 1 deletion packages/core/lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const uuidv4 = require('uuid').v4;
const { SSMS } = require('./ssms');
const createPhaser = require('./phases');
const createReader = require('./readers');
const { isPackageESM } = require('./util');
const engineUtil = require('@artilleryio/int-commons').engine_util;
const wl = require('./weighted-pick');
const { pathToFileURL } = require('url');
Expand Down Expand Up @@ -83,7 +84,7 @@ async function loadProcessor(script, options) {
script.config.processor
);

if (processorPath.endsWith('.mjs')) {
if (isPackageESM(processorPath)) {
const fileUrl = pathToFileURL(processorPath);
const exports = await import(fileUrl.href);
script.config.processor = Object.assign(
Expand Down
63 changes: 63 additions & 0 deletions packages/core/lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const path = require('path');
const fs = require('fs');

function determineModuleTypeByPackageJson(filePath) {
// If it's .js, we need to check the package.json
try {
// Start from script directory and move up until we find package.json
let dir = path.dirname(filePath);
while (dir !== path.parse(dir).root) {
try {
const pkgPath = path.join(dir, 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
return pkg.type === 'module' ? 'esm' : 'commonjs';
} catch (err) {
dir = path.dirname(dir);
}
}

// No package.json found, default to commonjs
return 'commonjs';
} catch (err) {
return 'commonjs';
}
}

function determineModuleType(filePath) {
if (filePath.endsWith('.mjs')) return 'esm';
if (filePath.endsWith('.cjs')) return 'commonjs';

return determineModuleTypeByPackageJson(filePath);
}

/**
* Determine if a package is ESM or not.
* @param {string | undefined} filePath Path to the package.json file. If not provided, it will default to the current working directory.
* @returns A boolean indicating if the package is ESM or not.
*/
function isPackageESM(filePath) {
if (!filePath) filePath = process.cwd();

return determineModuleType(filePath) === 'esm';
}

/**
* Determine if a package is CommonJS or not.
* @param {string | undefined} filePath Path to the package.json file. If not provided, it will default to the current working directory.
* @returns A boolean indicating if the package is CommonJS or not.
*/
function isPackageCommonJS(filePath) {
if (!filePath) filePath = process.cwd();

return determineModuleType(filePath) === 'commonjs';
}

module.exports = {
determineModuleType,
isPackageESM,
isPackageCommonJS
};
Loading