-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typescript support to plugin
- Loading branch information
1 parent
04ba061
commit 05f25f4
Showing
9 changed files
with
422 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
|
||
function plugin(opts, Bree) { | ||
|
||
opts = opts || {}; | ||
|
||
const oldInit = Bree.prototype.init; | ||
|
||
// define accepted extensions | ||
Bree.prototype.init = function () { | ||
if (!this.config.acceptedExtensions.includes('.ts')) | ||
this.config.acceptedExtensions.push('.ts'); | ||
|
||
oldInit.bind(this)(); | ||
}; | ||
|
||
const oldCreateWorker = Bree.prototype.createWorker; | ||
|
||
Bree.prototype.createWorker = function (filename, options) { | ||
if (filename.endsWith('.ts')) { | ||
options.workerData.path = filename; | ||
|
||
return new Worker(path.resolve(__dirname, 'worker.js'), options); | ||
} | ||
|
||
return oldCreateWorker(filename, options); | ||
}; | ||
} | ||
|
||
module.exports = plugin; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const { workerData } = require('worker_threads'); | ||
|
||
require('ts-node').register(); | ||
|
||
require(workerData.path); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
setTimeout(() => { | ||
console.log('hello'); | ||
}, 100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
setTimeout(() => { | ||
console.log('hello'); | ||
}, 100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,94 @@ | ||
const path = require('path'); | ||
const test = require('ava'); | ||
const delay = require('delay'); | ||
const Bree = require('../../bree/src'); | ||
|
||
const Script = require('..'); | ||
const plugin = require('../src'); | ||
|
||
test.beforeEach((t) => { | ||
const script = new Script({}); | ||
Object.assign(t.context, { script }); | ||
}); | ||
Bree.extend(plugin); | ||
|
||
const root = path.join(__dirname, 'jobs'); | ||
const baseConfig = { | ||
root, | ||
timeout: 0, | ||
interval: 0, | ||
hasSeconds: false, | ||
defaultExtension: 'js' | ||
}; | ||
|
||
test('returns itself', (t) => { | ||
t.true(t.context.script instanceof Script); | ||
test('will validate job when given ts file string', (t) => { | ||
t.notThrows(() => { | ||
// eslint-disable-next-line no-unused-vars | ||
const bree = new Bree({ | ||
jobs: ['short.ts'], | ||
...baseConfig | ||
}); | ||
}); | ||
}); | ||
|
||
test('sets a config object', (t) => { | ||
const script = new Script(false); | ||
t.true(script instanceof Script); | ||
test('will validate job when ".ts" is defined in config', (t) => { | ||
t.notThrows(() => { | ||
// eslint-disable-next-line no-unused-vars | ||
const bree = new Bree({ | ||
...baseConfig, | ||
jobs: ['short.ts'], | ||
acceptedExtensions: ['.ts', '.js', '.mjs'] | ||
}); | ||
}); | ||
}); | ||
|
||
test('renders name', (t) => { | ||
const { script } = t.context; | ||
t.is(script.renderName(), 'script'); | ||
test('will run job defined in ts', async (t) => { | ||
t.plan(2); | ||
|
||
const logger = { | ||
info: () => {} | ||
}; | ||
|
||
const bree = new Bree({ | ||
jobs: ['short.ts'], | ||
...baseConfig, | ||
logger | ||
}); | ||
|
||
bree.start(); | ||
|
||
bree.on('worker created', (name) => { | ||
t.true(typeof bree.workers[name] === 'object'); | ||
}); | ||
|
||
bree.on('worker deleted', (name) => { | ||
t.true(typeof bree.workers[name] === 'undefined'); | ||
}); | ||
|
||
await delay(100); | ||
|
||
await bree.stop(); | ||
}); | ||
|
||
test('sets a default name', (t) => { | ||
const { script } = t.context; | ||
t.is(script._name, 'script'); | ||
test('will run job defined in js', async (t) => { | ||
t.plan(2); | ||
|
||
const logger = { | ||
info: () => {} | ||
}; | ||
|
||
const bree = new Bree({ | ||
jobs: ['short.js'], | ||
...baseConfig, | ||
logger | ||
}); | ||
|
||
bree.start(); | ||
|
||
bree.on('worker created', (name) => { | ||
t.true(typeof bree.workers[name] === 'object'); | ||
}); | ||
|
||
bree.on('worker deleted', (name) => { | ||
t.true(typeof bree.workers[name] === 'undefined'); | ||
}); | ||
|
||
await delay(100); | ||
|
||
await bree.stop(); | ||
}); |
Oops, something went wrong.