Skip to content

Commit 5092691

Browse files
committed
update: requireModule to v3 for monorepo setup
1 parent cfa7e23 commit 5092691

File tree

1 file changed

+87
-25
lines changed

1 file changed

+87
-25
lines changed

bin/testObservability/helper/helper.js

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -715,32 +715,94 @@ exports.getOSDetailsFromSystem = async (product) => {
715715
};
716716
}
717717

718-
exports.requireModule = (module, internal = false) => {
719-
logger.debug(`Getting ${module} from ${process.cwd()}`);
720-
let local_path = "";
721-
if(process.env["browserStackCwd"]){
722-
local_path = path.join(process.env["browserStackCwd"], 'node_modules', module);
723-
} else if(internal) {
724-
local_path = path.join(process.cwd(), 'node_modules', 'browserstack-cypress-cli', 'node_modules', module);
725-
} else {
726-
local_path = path.join(process.cwd(), 'node_modules', module);
727-
}
728-
if(!fs.existsSync(local_path)) {
729-
logger.debug(`${module} doesn\'t exist at ${process.cwd()}`);
730-
logger.debug(`Getting ${module} from ${GLOBAL_MODULE_PATH}`);
731-
732-
let global_path;
733-
if(['jest-runner', 'jest-runtime'].includes(module))
734-
global_path = path.join(GLOBAL_MODULE_PATH, 'jest', 'node_modules', module);
735-
else
736-
global_path = path.join(GLOBAL_MODULE_PATH, module);
737-
if(!fs.existsSync(global_path)) {
738-
throw new Error(`${module} doesn't exist.`);
718+
exports.requireModule = (module, _package) => {
719+
const modulePath = exports.resolveModule(module, _package);
720+
if (modulePath.error) {
721+
throw new Error(`${module} doesn't exist.`);
722+
}
723+
724+
return require(modulePath.path);
725+
};
726+
727+
exports.resolveModule = (module, _package = null) => {
728+
if (_package) {
729+
try {
730+
const fileSeparator = path.sep;
731+
let packagePath = require.resolve(_package);
732+
if (packagePath) {
733+
packagePath = packagePath.split(fileSeparator);
734+
packagePath.pop();
735+
packagePath = packagePath.join(fileSeparator);
736+
737+
const v3path = path.join(packagePath, module.replace(_package + fileSeparator, ''));
738+
if (v3path && fs.existsSync(v3path)) {
739+
return {path: v3path, foundAt: 'v3Path'};
740+
}
741+
}
742+
} catch (e) {
743+
exports.debug(`Unable to resolve module with requireModuleV3 with error: ${e}`);
739744
}
740-
return require(global_path);
741745
}
742-
return require(local_path);
743-
}
746+
747+
/*
748+
Modules will be resolved in the following order,
749+
current working dir > workspaces dir > NODE_PATH env var > global node modules path
750+
*/
751+
752+
try {
753+
exports.debug('requireModuleV2');
754+
755+
return {path: require.resolve(module), foundAt: 'resolve'};
756+
} catch (_) {
757+
/* Find from current working directory */
758+
exports.debug(`Getting ${module} from ${process.cwd()}`);
759+
let local_path = '';
760+
if (process.env['browserStackCwd']) {
761+
local_path = path.join(process.env['browserStackCwd'], 'node_modules', module);
762+
} else {
763+
local_path = path.join(process.cwd(), 'node_modules', module);
764+
}
765+
if (!fs.existsSync(local_path)) {
766+
exports.debug(`${module} doesn't exist at ${process.cwd()}`);
767+
768+
/* Find from workspaces */
769+
if (WORKSPACE_MODULE_PATH) {
770+
exports.debug(`Getting ${module} from path ${WORKSPACE_MODULE_PATH}`);
771+
let workspace_path = null;
772+
if (['jest-runner', 'jest-runtime'].includes(module)) {workspace_path = path.join(WORKSPACE_MODULE_PATH, 'node_modules', 'jest', 'node_modules', module)} else {workspace_path = path.join(WORKSPACE_MODULE_PATH, 'node_modules', module)}
773+
if (workspace_path && fs.existsSync(workspace_path)) {
774+
exports.debug(`Found ${module} from ${WORKSPACE_MODULE_PATH}`);
775+
776+
return {path: workspace_path, foundAt: 'workspaces'};
777+
}
778+
}
779+
780+
/* Find from node path */
781+
let node_path = null;
782+
if (!exports.isUndefined(process.env.NODE_PATH)) {
783+
if (['jest-runner', 'jest-runtime'].includes(module)) {node_path = path.join(process.env.NODE_PATH, 'jest', 'node_modules', module)} else {node_path = path.join(process.env.NODE_PATH, module)}
784+
}
785+
if (node_path && fs.existsSync(node_path)) {
786+
exports.debug(`Getting ${module} from ${process.env.NODE_PATH}`);
787+
788+
return {path: node_path, foundAt: 'nodePath'};
789+
}
790+
791+
/* Find from global node modules path */
792+
exports.debug(`Getting ${module} from ${GLOBAL_MODULE_PATH}`);
793+
794+
let global_path = null;
795+
if (['jest-runner', 'jest-runtime'].includes(module)) {global_path = path.join(GLOBAL_MODULE_PATH, 'jest', 'node_modules', module)} else {global_path = path.join(GLOBAL_MODULE_PATH, module)}
796+
if (!global_path || !fs.existsSync(global_path)) {
797+
return {error: 'module_not_found'};
798+
}
799+
800+
return {path: global_path, foundAt: 'local'};
801+
}
802+
803+
return {path: local_path, foundAt: 'global'};
804+
}
805+
};
744806

745807
const getReRunSpecs = (rawArgs) => {
746808
if (this.isTestObservabilitySession() && this.shouldReRunObservabilityTests()) {
@@ -763,7 +825,7 @@ const getReRunSpecs = (rawArgs) => {
763825

764826
const getLocalSessionReporter = () => {
765827
if(this.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
766-
return ['--reporter', TEST_OBSERVABILITY_REPORTER];
828+
return ['--reporter', path.join(__dirname, '..', 'reporter')];
767829
} else {
768830
return [];
769831
}

0 commit comments

Comments
 (0)