-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
39 lines (35 loc) · 1.23 KB
/
index.ts
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
33
34
35
36
37
38
39
#!/usr/bin/env node
import { exec, spawn } from 'child_process';
import { existsSync } from 'fs';
const esbuildOptions = process.env.ESBUILD_OPTIONS || '--bundle --platform=node --sourcemap=inline'
const maxBuffer = process.env.JS_MAX_BUFFER || 1024^2 * 20; // 20 MB
const outfile = process.env.EVANW_OUTFILE || `/tmp/${Math.random().toString(26).split('.')[1]}.js`;
const args = process.argv.slice(2);
let filename;
const nodeArgs = args.filter(arg => {
if (arg[0] !== '-') {
if (filename) {
throw Error(`Invalid options, only one option can not start with a '-', options: ${args.join(', ')}`)
} else {
filename = arg;
return false;
}
}
return true;
});
if (!filename) {
throw Error('Please specify a filename to run');
}
if (!existsSync(filename)) {
throw Error(`Cannot find the file to run: ${filename}`);
}
exec(`esbuild ${esbuildOptions} ${filename} --outfile=${outfile}`, { maxBuffer: Number(maxBuffer) }, (error, builtCode) => {
if (error) {
console.error('Cannot run esbuild,', error);
if (error.code === 127) {
console.error('Please make sure esbuild is installed: npm install --save-dev esbuild')
}
return;
}
spawn('node', [...nodeArgs, outfile], { stdio: 'inherit'});
});