Skip to content

Commit

Permalink
fix: Scripts not loaded in the order they exist in the file system
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyguerra committed May 2, 2024
1 parent a365b26 commit 6eb73e3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Robot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Listener, TextListener } from './Listener.mjs'
import Message from './Message.mjs'
import Middleware from './Middleware.mjs'

const File = fs.promises
const HUBOT_DEFAULT_ADAPTERS = ['Campfire', 'Shell']
const HUBOT_DOCUMENTATION_SECTIONS = ['description', 'dependencies', 'configuration', 'commands', 'notes', 'author', 'authors', 'examples', 'tags', 'urls']

Expand Down Expand Up @@ -380,10 +381,18 @@ class Robot {
// Returns nothing.
async load (path) {
this.logger.debug(`Loading scripts from ${path}`)

if (fs.existsSync(path)) {
const tasks = fs.readdirSync(path).sort().map(file => this.loadFile(path, file))
await Promise.all(tasks)
try {
const folder = await File.readdir(path, { withFileTypes: true })
for await (const file of folder) {
if (file.isDirectory()) continue
try {
await this.loadFile(path, file.name)
} catch (e) {
this.logger.error(`Error loading file ${file.name} - ${e.stack}`)
}
}
} catch (e) {
this.logger.error(`Path ${path} does not exist`)
}
}

Expand Down
17 changes: 17 additions & 0 deletions test/Robot_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import assert from 'node:assert/strict'
import path from 'node:path'
import { Robot, CatchAllMessage, EnterMessage, LeaveMessage, TextMessage, TopicMessage, User, Response } from '../index.mjs'
import mockAdapter from './fixtures/MockAdapter.mjs'

describe('Robot', () => {
describe('#http', () => {
let robot = null
Expand Down Expand Up @@ -282,6 +283,22 @@ describe('Robot', () => {
assert.ok(goodListenerCalled)
})
})
describe('#load', () => {
let robot = null
beforeEach(async () => {
robot = new Robot(mockAdapter, false, 'TestHubot')
await robot.loadAdapter()
await robot.run()
})
afterEach(() => {
robot.shutdown()
process.removeAllListeners()
})
it('should load scripts in the same order that they are in the folder', async () => {
await robot.load(path.resolve('./test/ordered-scripts'))
assert.deepEqual(robot.loadedScripts, ['01-First', '02-Second', '03-Third'])
})
})
describe('#loadFile', () => {
let robot = null
beforeEach(async () => {
Expand Down
4 changes: 4 additions & 0 deletions test/ordered-scripts/01-PFirst.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default async robot => {
robot.loadedScripts = []
robot.loadedScripts.push('01-First')
}
3 changes: 3 additions & 0 deletions test/ordered-scripts/02-SetupBotConfig.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async robot => {
robot.loadedScripts.push('02-Second')
}
3 changes: 3 additions & 0 deletions test/ordered-scripts/WebSetup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async robot => {
robot.loadedScripts.push('03-Third')
}

0 comments on commit 6eb73e3

Please sign in to comment.