Skip to content

Commit e574381

Browse files
authored
Convert preprocessor.js to JS module. NFC (#20854)
1 parent 8b0b7bf commit e574381

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

tools/preprocessor.js renamed to tools/preprocessor.mjs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,29 @@
1414

1515
'use strict';
1616

17-
const fs = require('fs');
18-
const path = require('path');
19-
global.vm = require('vm');
17+
import * as fs from 'fs';
18+
import * as path from 'path';
19+
import * as vm from 'vm';
20+
import assert from 'assert';
21+
import * as url from 'url';
2022

21-
const arguments_ = process.argv.slice(2);
23+
const args = process.argv.slice(2);
2224
const debug = false;
2325

26+
// Anything needed by the script that we load below must be added to the
27+
// global object. These, for example, are all needed by parseTools.js.
28+
global.vm = vm;
29+
global.assert = assert;
2430
global.print = (x) => {
2531
process.stdout.write(x + '\n');
2632
};
2733
global.printErr = (x) => {
2834
process.stderr.write(x + '\n');
2935
};
3036

31-
global.assert = require('assert');
32-
3337
function find(filename) {
34-
const prefixes = [process.cwd(), path.join(__dirname, '..', 'src')];
38+
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
39+
const prefixes = [process.cwd(), path.join(dirname, '..', 'src')];
3540
for (let i = 0; i < prefixes.length; ++i) {
3641
const combined = path.join(prefixes[i], filename);
3742
if (fs.existsSync(combined)) {
@@ -50,9 +55,10 @@ global.load = (f) => {
5055
(0, eval)(read(f) + '//# sourceURL=' + find(f));
5156
};
5257

53-
const settingsFile = arguments_[0];
54-
const inputFile = arguments_[1];
55-
const expandMacros = arguments_.includes('--expandMacros');
58+
assert(args.length >= 2);
59+
const settingsFile = args[0];
60+
const inputFile = args[1];
61+
const expandMacros = args.includes('--expandMacros');
5662

5763
load(settingsFile);
5864
load('utility.js');

tools/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ def read_and_preprocess(filename, expand_macros=False):
762762
if expand_macros:
763763
args += ['--expandMacros']
764764

765-
run_js_tool(path_from_root('tools/preprocessor.js'), args, stdout=open(stdout, 'w'), cwd=dirname)
765+
run_js_tool(path_from_root('tools/preprocessor.mjs'), args, stdout=open(stdout, 'w'), cwd=dirname)
766766
out = utils.read_file(stdout)
767767

768768
return out

0 commit comments

Comments
 (0)