Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/preprocessor.mjs
4128 views
1
#!/usr/bin/env node
2
// Copyright 2018 The Emscripten Authors. All rights reserved.
3
// Emscripten is available under two separate licenses, the MIT license and the
4
// University of Illinois/NCSA Open Source License. Both these licenses can be
5
// found in the LICENSE file.
6
//
7
// Preprocessor tool. This is a wrapper for the 'preprocess' function which
8
// allows it to be called as a standalone tool.
9
//
10
// Parameters:
11
// setting file. Can specify 'settings.js' here, alternatively create a temp
12
// file with modified settings and supply the filename here.
13
// input file This is the file that will be processed by the preprocessor
14
15
import assert from 'node:assert';
16
import {parseArgs} from 'node:util';
17
18
import {readFile, loadDefaultSettings, applySettings} from '../src/utility.mjs';
19
20
const options = {
21
'expand-macros': {type: 'boolean'},
22
help: {type: 'boolean', short: 'h'},
23
};
24
const {values, positionals} = parseArgs({options, allowPositionals: true});
25
26
if (values.help) {
27
console.log(`\
28
Run JS preprocessor / macro processor on an input file
29
30
Usage: preprocessor.mjs <settings.json> <input-file> [--expand-macros]`);
31
process.exit(0);
32
}
33
34
loadDefaultSettings();
35
36
assert(positionals.length == 2, 'Script requires 2 arguments');
37
38
// Load settings from JSON passed on the command line
39
let settingsFile = positionals[0];
40
assert(settingsFile, 'settings file not specified');
41
if (settingsFile == '-') {
42
// Read settings json from stdin (FD 0)
43
settingsFile = 0;
44
}
45
const userSettings = JSON.parse(readFile(settingsFile));
46
applySettings(userSettings);
47
48
const inputFile = positionals[1];
49
50
// We can't use static import statements here because several of these
51
// file depend on having the settings defined in the global scope (which
52
// we do dynamically above.
53
const parseTools = await import('../src/parseTools.mjs');
54
await import('../src/modules.mjs');
55
56
let output = parseTools.preprocess(inputFile);
57
if (values['expand-macros']) {
58
output = parseTools.processMacros(output, inputFile);
59
}
60
process.stdout.write(output);
61
62