Skip to content

Commit

Permalink
fix: Loading .mjs Adapter from file and scripts failing on Windows (#…
Browse files Browse the repository at this point in the history
…1662)

* fix: on Windows, getting ERR_UNSUPPORTED_ESM_URL_SCHEME error when trying to load an adpater

* fix: ERR_UNSUPPORTED_ESM_URL_SCHEME on Windows

* chore: Fixing loading adapter by file on Windows

* Didn't seem to run

* Still failing

* Setting environment variables in Window is diff.

* adapter worked, but now the mjs scripts are failing to load

* fix: Setting HUBOT_ADAPTER to a file path on Windows failed to load the adapter
fix: .mjs files were failing to load on Windows for the same reason
  • Loading branch information
joeyguerra authored Aug 14, 2023
1 parent a98b433 commit 91cbe76
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/nodejs-windows.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
name: Node.js (Windows) CI

on:
push:
branches: [ "main" ]
branches:
- main
schedule:
- cron: '5 4 * * 0'

- cron: '5 4 * * 0'
jobs:
npm-test:

runs-on: windows-latest

strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -23,4 +19,7 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm test
- name: Run Tests
env:
HUBOT_LOG_LEVEL: debug
run: npm test
14 changes: 7 additions & 7 deletions bin/hubot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const OptParse = require('optparse')
const Hubot = require('..')

const switches = [
['-a', '--adapter ADAPTER', 'The Adapter to use, e.g. "shell" (to load the default hubot shell adapter)'],
['-f', '--file PATH', 'Path to adapter file, e.g. "./adapters/CustomAdapter.mjs"'],
['-c', '--create PATH', 'Create a deployable hubot'],
['-d', '--disable-httpd DISABLE_HTTPD', 'Disable the HTTP server'],
['-a', '--adapter HUBOT_ADAPTER', 'The Adapter to use, e.g. "shell" (to load the default hubot shell adapter)'],
['-f', '--file HUBOT_FILE', 'Path to adapter file, e.g. "./adapters/CustomAdapter.mjs"'],
['-c', '--create HUBOT_CREATE', 'Create a deployable hubot'],
['-d', '--disable-httpd HUBOT_HTTPD', 'Disable the HTTP server'],
['-h', '--help', 'Display the help information'],
['-l', '--alias ALIAS', "Enable replacing the robot's name with alias"],
['-n', '--name NAME', 'The name of the robot in chat'],
['-l', '--alias HUBOT_ALIAS', "Enable replacing the robot's name with alias"],
['-n', '--name HUBOT_NAME', 'The name of the robot in chat'],
['-r', '--require PATH', 'Alternative scripts path'],
['-t', '--config-check', "Test hubot's config to make sure it won't fail at startup"],
['-v', '--version', 'Displays the version of hubot installed']
Expand Down Expand Up @@ -97,6 +97,7 @@ if (options.create) {
if (options.file) {
options.adapter = options.file.split('/').pop().split('.')[0]
}

const robot = Hubot.loadBot(options.adapter, options.enableHttpd, options.name, options.alias)
module.exports = robot

Expand All @@ -107,7 +108,6 @@ async function loadScripts () {
loadExternalScripts()

const tasks = options.scripts.map((scriptPath) => {
console.log('loadding', scriptPath)
if (scriptPath[0] === '/') {
return robot.load(scriptPath)
}
Expand Down
4 changes: 2 additions & 2 deletions src/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class Robot {
}

async loadmjs (filePath) {
const script = await import(filePath)
const script = await import(pathToFileURL(filePath))
if (typeof script?.default === 'function') {
script.default(this)
} else {
Expand Down Expand Up @@ -526,7 +526,7 @@ class Robot {
try {
this.adapter = this.requireAdapterFrom(adapterPathInCurrentWorkingDirectory)
} catch (err) {
if (err.name === 'SyntaxError' && err.message.includes('Cannot use import statement outside a module')) {
if (err.name === 'SyntaxError') {
this.adapter = await this.importAdapterFrom(adapterPathInCurrentWorkingDirectory)
} else {
throw err
Expand Down
36 changes: 19 additions & 17 deletions test/hubot_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

/* global describe, it, before, after */
/* global describe, it */
/* eslint-disable no-unused-expressions */

const path = require('path')
Expand All @@ -10,25 +10,27 @@ const expect = chai.expect
const root = __dirname.replace(/test$/, '')
const { TextMessage, User } = require('../index.js')

describe('hubot', () => {
let hubot
before(() => {
process.env.HUBOT_ADAPTER = path.join('..', 'test', 'fixtures', 'MockAdapter.mjs')
hubot = require('../bin/hubot.js')
})
after(() => {
hubot.shutdown()
delete process.env.HUBOT_ADAPTER
})
it('should export robot instance', done => {
hubot.loadFile(path.resolve(root, 'test/fixtures'), 'TestScript.mjs').then(() => {
describe('Running bin/hubot.js', () => {
it('should load adapter from HUBOT_FILE environment variable', done => {
process.env.HUBOT_HTTPD = 'false'
process.env.HUBOT_FILE = path.resolve(root, 'test', 'fixtures', 'MockAdapter.mjs')
const hubot = require('../bin/hubot.js')
setTimeout(function () {
hubot.adapter.on('reply', (envelope, ...strings) => {
expect(strings[0]).to.equal('test response from .mjs script')
delete process.env.HUBOT_FILE
delete process.env.HUBOT_HTTPD
hubot.shutdown()
done()
})
hubot.receive(new TextMessage(new User('mocha', { room: '#mocha' }), 'Hubot test'))
expect(hubot.hasLoadedTestMjsScript).to.be.true
expect(hubot.name).to.equal('Hubot')
}).catch(done)
hubot.loadFile(path.resolve(root, 'test', 'fixtures'), 'TestScript.mjs').then(() => {
hubot.receive(new TextMessage(new User('mocha', { room: '#mocha' }), '@Hubot test'))
expect(hubot.hasLoadedTestMjsScript).to.be.true
expect(hubot.name).to.equal('Hubot')
}).catch(err => {
hubot.shutdown()
done(err)
})
}, 1000)
})
})

0 comments on commit 91cbe76

Please sign in to comment.