-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.cjs
50 lines (41 loc) · 1.2 KB
/
cli.cjs
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
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const vm = require('node:vm');
const { resolve: resolvePath } = require('node:path');
const process = require('node:process');
const { ExampleEvaluator } = require('./dist/index.cjs');
if (!('SourceTextModule' in vm)) throw new Error('No SourceTextModule in vm, try using node --experimental-vm-modules flag');
const CWD = process.cwd();
const packageDefinition = require(resolvePath(CWD, 'package.json'));
let globalContext = false;
let blockIdx = NaN;
let markdownFiles = './README.md';
for (const arg of process.argv.slice(2, 4)) {
if (arg === undefined) break;
else if (arg === '-g') globalContext = true;
else if (!isNaN(Number(arg))) blockIdx = Number(arg);
else markdownFiles = arg;
}
run().catch((err) => {
// eslint-disable-next-line no-console
console.error(err.stack);
process.exitCode = 1;
});
async function run() {
for (const filePath of markdownFiles.split(',')) {
await new ExampleEvaluator(
filePath,
packageDefinition,
CWD,
globalContext
? globalThis
: {
Buffer,
process,
Date,
console,
setTimeout,
clearTimeout,
},
).evaluate(blockIdx);
}
}