Skip to content

Commit

Permalink
fix: Unable to load script in Windows; fixes #1701 (#1702)
Browse files Browse the repository at this point in the history
* fix: Unable to load script in Windows; fixes #1701
* chore: e2e test fails on local because of case
  • Loading branch information
joeyguerra committed Nov 12, 2023
1 parent 31639b3 commit 0a8e9db
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions bin/e2e-test.sh
Expand Up @@ -38,6 +38,7 @@ expect <<EOL
send "e2etest adapter\r"
expect {
"Shell" {}
"shell" {}
timeout {exit 1}
}
EOL
19 changes: 13 additions & 6 deletions src/Robot.mjs
Expand Up @@ -329,20 +329,22 @@ class Robot {
}

async loadmjs (filePath) {
const script = await import(pathToFileURL(filePath))
const forImport = this.prepareForImport(filePath)
const script = await import(forImport)
if (typeof script?.default === 'function') {
script.default(this)
} else {
this.logger.warning(`Expected ${filePath} to assign a function to export default, got ${typeof script}`)
this.logger.warning(`Expected ${filePath} (after preparing for import ${forImport}) to assign a function to export default, got ${typeof script}`)
}
}

async loadjs (filePath) {
const script = (await import(filePath)).default
const forImport = this.prepareForImport(filePath)
const script = (await import(forImport)).default
if (typeof script === 'function') {
script(this)
} else {
this.logger.warning(`Expected ${filePath} to assign a function to module.exports, got ${typeof script}`)
this.logger.warning(`Expected ${filePath} (after preparing for import ${forImport}) to assign a function to module.exports, got ${typeof script}`)
}
}

Expand Down Expand Up @@ -496,7 +498,7 @@ class Robot {
} else if (['.js', '.cjs'].includes(ext)) {
this.adapter = await this.requireAdapterFrom(path.resolve(adapterPath))
} else if (['.mjs'].includes(ext)) {
this.adapter = await this.importAdapterFrom(pathToFileURL(path.resolve(adapterPath)).href)
this.adapter = await this.importAdapterFrom(path.resolve(adapterPath))
} else {
const adapterPathInCurrentWorkingDirectory = this.adapterName
try {
Expand All @@ -520,7 +522,8 @@ class Robot {
}

async importAdapterFrom (adapterPath) {
return await (await import(adapterPath)).default.use(this)
const forImport = this.prepareForImport(adapterPath)
return await (await import(forImport)).default.use(this)
}

// Public: Help Commands for Running Scripts.
Expand Down Expand Up @@ -670,6 +673,10 @@ class Robot {
this.events.removeAllListeners()
}

prepareForImport (filePath) {
return pathToFileURL(filePath)
}

// Public: The version of Hubot from npm
//
// Returns a String of the version number.
Expand Down

0 comments on commit 0a8e9db

Please sign in to comment.