hubot 11.0.0 → 11.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/Robot.mjs +18 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hubot",
3
- "version": "11.0.0",
3
+ "version": "11.0.2",
4
4
  "author": "hubot",
5
5
  "keywords": [
6
6
  "github",
package/src/Robot.mjs CHANGED
@@ -329,20 +329,22 @@ class Robot {
329
329
  }
330
330
 
331
331
  async loadmjs (filePath) {
332
- const script = await import(pathToFileURL(filePath))
332
+ const forImport = this.prepareForImport(filePath)
333
+ const script = await import(forImport)
333
334
  if (typeof script?.default === 'function') {
334
335
  script.default(this)
335
336
  } else {
336
- this.logger.warning(`Expected ${filePath} to assign a function to export default, got ${typeof script}`)
337
+ this.logger.warning(`Expected ${filePath} (after preparing for import ${forImport}) to assign a function to export default, got ${typeof script}`)
337
338
  }
338
339
  }
339
340
 
340
341
  async loadjs (filePath) {
341
- const script = (await import(filePath)).default
342
+ const forImport = this.prepareForImport(filePath)
343
+ const script = (await import(forImport)).default
342
344
  if (typeof script === 'function') {
343
345
  script(this)
344
346
  } else {
345
- this.logger.warning(`Expected ${filePath} to assign a function to module.exports, got ${typeof script}`)
347
+ this.logger.warning(`Expected ${filePath} (after preparing for import ${forImport}) to assign a function to module.exports, got ${typeof script}`)
346
348
  }
347
349
  }
348
350
 
@@ -489,25 +491,16 @@ class Robot {
489
491
  return
490
492
  }
491
493
  this.logger.debug(`Loading adapter ${adapterPath ?? 'from npmjs:'} ${this.adapterName}`)
492
- const ext = path.extname(adapterPath ?? '') ?? '.mjs'
494
+ const ext = path.extname(adapterPath ?? '')
493
495
  try {
494
496
  if (Array.from(HUBOT_DEFAULT_ADAPTERS).indexOf(this.adapterName) > -1) {
495
497
  this.adapter = await this.requireAdapterFrom(path.resolve(path.join(__dirname, 'adapters', `${this.adapterName}.mjs`)))
496
498
  } else if (['.js', '.cjs'].includes(ext)) {
497
499
  this.adapter = await this.requireAdapterFrom(path.resolve(adapterPath))
498
500
  } else if (['.mjs'].includes(ext)) {
499
- this.adapter = await this.importAdapterFrom(pathToFileURL(path.resolve(adapterPath)).href)
501
+ this.adapter = await this.importAdapterFrom(path.resolve(adapterPath))
500
502
  } else {
501
- const adapterPathInCurrentWorkingDirectory = this.adapterName
502
- try {
503
- this.adapter = await this.requireAdapterFrom(adapterPathInCurrentWorkingDirectory)
504
- } catch (err) {
505
- if (err.name === 'SyntaxError') {
506
- this.adapter = await this.importAdapterFrom(adapterPathInCurrentWorkingDirectory)
507
- } else {
508
- throw err
509
- }
510
- }
503
+ this.adapter = await this.importFromRepo(this.adapterName)
511
504
  }
512
505
  } catch (error) {
513
506
  this.logger.error(`Cannot load adapter ${adapterPath ?? '[no path set]'} ${this.adapterName} - ${error}`)
@@ -520,6 +513,11 @@ class Robot {
520
513
  }
521
514
 
522
515
  async importAdapterFrom (adapterPath) {
516
+ const forImport = this.prepareForImport(adapterPath)
517
+ return await (await import(forImport)).default.use(this)
518
+ }
519
+
520
+ async importFromRepo (adapterPath) {
523
521
  return await (await import(adapterPath)).default.use(this)
524
522
  }
525
523
 
@@ -670,6 +668,10 @@ class Robot {
670
668
  this.events.removeAllListeners()
671
669
  }
672
670
 
671
+ prepareForImport (filePath) {
672
+ return pathToFileURL(filePath)
673
+ }
674
+
673
675
  // Public: The version of Hubot from npm
674
676
  //
675
677
  // Returns a String of the version number.