Skip to content

Commit

Permalink
feat: add typescript support to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowgate15 committed Nov 4, 2021
1 parent 04ba061 commit 05f25f4
Show file tree
Hide file tree
Showing 9 changed files with 422 additions and 27 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@

> Typescript workers plugin for Bree!

## Table of Contents

* [Install](#install)
* [Usage](#usage)
* [Contributors](#contributors)
* [License](#license)


## Install

Expand Down Expand Up @@ -42,9 +48,15 @@ console.log(@breejs/tsWorker.renderName());

## Contributors

| Name | Website |
| ----------------- | --------------------------------- |
| **Taylor Schley** | <https://github.com/shadowgate15> |


## License

[MIT](LICENSE) © [Taylor Schley](https://github.com/shadowgate15)


##

Expand Down
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@
"url": "https://github.com/breejs/ts-worker/issues",
"email": "taylorschley@me.com"
},
"ava": {
"files": [
"test/*.js",
"test/**/*.js",
"!test/jobs"
],
"verbose": true
},
"contributors": [
"Taylor Schley <taylorschley@me.com> (https://github.com/shadowgate15)"
],
"dependencies": {},
"dependencies": {
"ts-node": "^10.4.0"
},
"devDependencies": {
"@commitlint/cli": "latest",
"@commitlint/config-conventional": "latest",
"ava": "latest",
"bree": "^6.4.0",
"codecov": "latest",
"cross-env": "latest",
"delay": "^5.0.0",
"eslint": "latest",
"eslint-config-xo-lass": "latest",
"fixpack": "latest",
Expand All @@ -28,7 +40,7 @@
"xo": "latest"
},
"engines": {
"node": ">= 10"
"node": ">= 12.11.0"
},
"homepage": "https://github.com/breejs/ts-worker",
"keywords": [
Expand Down
27 changes: 26 additions & 1 deletion src/index.js
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 removed src/job-builder.js
Empty file.
5 changes: 5 additions & 0 deletions src/worker.js
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);
3 changes: 3 additions & 0 deletions test/jobs/short.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(() => {
console.log('hello');
}, 100);
3 changes: 3 additions & 0 deletions test/jobs/short.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(() => {
console.log('hello');
}, 100);
99 changes: 83 additions & 16 deletions test/test.js
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();
});
Loading

0 comments on commit 05f25f4

Please sign in to comment.