node/lib/internal/main/check_syntax.js
Joyee Cheung 84000835e2
process: group main thread execution preparation code
This patch groups the preparation of main thread execution into
`prepareMainThreadExecution()` and removes the cluster IPC
setup in worker thread bootstrap since clusters do not use
worker threads for its implementation and it's unlikely to change.

PR-URL: https://github.com/nodejs/node/pull/26000
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
2019-02-10 16:33:57 +08:00

51 lines
1.3 KiB
JavaScript

'use strict';
// If user passed `-c` or `--check` arguments to Node, check its syntax
// instead of actually running the file.
const {
prepareMainThreadExecution
} = require('internal/bootstrap/pre_execution');
const {
readStdin
} = require('internal/process/execution');
const CJSModule = require('internal/modules/cjs/loader');
const vm = require('vm');
const {
stripShebang, stripBOM
} = require('internal/modules/cjs/helpers');
// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution();
markBootstrapComplete();
if (process.argv[1] && process.argv[1] !== '-') {
// Expand process.argv[1] into a full path.
const path = require('path');
process.argv[1] = path.resolve(process.argv[1]);
// Read the source.
const filename = CJSModule._resolveFilename(process.argv[1]);
const fs = require('fs');
const source = fs.readFileSync(filename, 'utf-8');
checkScriptSyntax(source, filename);
} else {
readStdin((code) => {
checkScriptSyntax(code, '[stdin]');
});
}
function checkScriptSyntax(source, filename) {
// Remove Shebang.
source = stripShebang(source);
// Remove BOM.
source = stripBOM(source);
// Wrap it.
source = CJSModule.wrap(source);
// Compile the script, this will throw if it fails.
new vm.Script(source, { displayErrors: true, filename });
}