Skip to content

Commit

Permalink
Convert preprocessor.js to JS module. NFC
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 committed Dec 6, 2023
1 parent 165e568 commit 59ee3ad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions tools/preprocessor.js → tools/preprocessor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@

'use strict';

const fs = require('fs');
const path = require('path');
global.vm = require('vm');
import * as fs from 'fs';
import * as path from 'path';
import * as vm from 'vm';
import assert from 'assert';
import * as url from 'url';

const arguments_ = process.argv.slice(2);
const args = process.argv.slice(2);
const debug = false;

global.vm = vm;
global.assert = assert;

global.print = (x) => {
process.stdout.write(x + '\n');
};
global.printErr = (x) => {
process.stderr.write(x + '\n');
};

global.assert = require('assert');

function find(filename) {
const prefixes = [process.cwd(), path.join(__dirname, '..', 'src')];
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
const prefixes = [process.cwd(), path.join(dirname, '..', 'src')];
for (let i = 0; i < prefixes.length; ++i) {
const combined = path.join(prefixes[i], filename);
if (fs.existsSync(combined)) {
Expand All @@ -50,9 +54,10 @@ global.load = (f) => {
(0, eval)(read(f) + '//# sourceURL=' + find(f));
};

const settingsFile = arguments_[0];
const inputFile = arguments_[1];
const expandMacros = arguments_.includes('--expandMacros');
assert(args.length >= 2);
const settingsFile = args[0];
const inputFile = args[1];
const expandMacros = args.includes('--expandMacros');

load(settingsFile);
load('utility.js');
Expand Down
2 changes: 1 addition & 1 deletion tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def read_and_preprocess(filename, expand_macros=False):
if expand_macros:
args += ['--expandMacros']

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

return out
Expand Down

0 comments on commit 59ee3ad

Please sign in to comment.