-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcompile.mjs
More file actions
54 lines (43 loc) · 1.5 KB
/
compile.mjs
File metadata and controls
54 lines (43 loc) · 1.5 KB
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
51
52
53
54
import { spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Blaze } from './index.mjs';
const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
const PROJECT_ROOT = resolve(MODULE_DIR, '../..');
const BINARY_NAME = 'sourcemeta_blaze_contrib_compile';
const CANDIDATES = [
resolve(PROJECT_ROOT, 'build/contrib', BINARY_NAME),
resolve(PROJECT_ROOT, 'build/bin/Release', BINARY_NAME + '.exe'),
resolve(PROJECT_ROOT, 'build/bin/Debug', BINARY_NAME + '.exe')
];
const COMPILE_BIN = CANDIDATES.find(existsSync);
if (COMPILE_BIN) {
process.stderr.write(`Found compile CLI: ${COMPILE_BIN}\n`);
} else {
throw new Error('Could not find compile CLI. Searched:\n' + CANDIDATES.join('\n'));
}
export function compileSchema(schemaPath, options) {
const args = [];
if (options && options.mode === 'fast') {
args.push('--fast');
}
if (options && options.defaultDialect) {
args.push('--default-dialect', options.defaultDialect);
}
if (options && options.resolveDirectory) {
args.push('--resolve-directory', options.resolveDirectory);
}
if (options && options.path) {
args.push('--path', options.path);
}
args.push(schemaPath);
const result = spawnSync(COMPILE_BIN, args, {
encoding: 'utf-8',
maxBuffer: 256 * 1024 * 1024
});
if (result.status !== 0) {
throw new Error(result.stderr.trim());
}
return JSON.parse(result.stdout, Blaze.reviver);
}