forked from lukeed/taskr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (27 loc) · 830 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'use strict';
const Script = require('vm').Script;
const dirname = require('path').dirname;
const rImports = require('rewrite-imports');
const req = require('require-like');
const fn = 'function*';
const box = {};
module.exports = function (file, data) {
// setup mock env
Object.assign(box, global);
box.module = { exports };
box.exports = exports;
box.require = req(file);
box.__dirname = dirname(file);
box.__filename = file;
const scr = new Script(
rImports(data)
.replace(/await/gi, 'yield')
.replace(/export /gi, 'exports.')
.replace(/default async function/gi, `default = ${fn}`)
.replace(/async function(\s)?.+?(?=\()/gi, str => str.trim().split(' ').pop().concat(` = ${fn} `))
);
// `eval()` new content
scr.runInNewContext(box);
// return new `tasks` object
return box.module.exports;
};