Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions lib/internal/main/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const {
JSONParse,
ObjectKeys
} = primordials;

const {
prepareMainThreadExecution,
markBootstrapComplete
} = require('internal/process/pre_execution');
const { internalModuleReadJSON } = internalBinding('fs');
const { execSync } = require('child_process');
const { getOptionValue } = require('internal/options');
const { log, error } = console;

prepareMainThreadExecution(false, false);

markBootstrapComplete();

const result = internalModuleReadJSON('package.json');
if (result.length === 0) {
error(`Can't read package.json`);
process.exit(1);
}

const json = JSONParse(result[0]);
const id = getOptionValue('--run');
const command = json.scripts[id];

if (!command) {
error(`Missing script: "${id}"`);
error('Available scripts are:\n');
for (const script of ObjectKeys(json.scripts)) {
error(` ${script}: ${json.scripts[script]}`);
}
process.exit(1);
}

log('');
log('>', id);
log('>', command);
log('');

execSync(command, { stdio: 'inherit' });
4 changes: 4 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) {
return StartExecution(env, "internal/main/watch_mode");
}

if (!env->options()->run.empty()) {
return StartExecution(env, "internal/main/run");
}

if (!first_argv.empty() && first_argv != "-") {
return StartExecution(env, "internal/main/run_main_module");
}
Expand Down
1 change: 1 addition & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
} else if (n == 7) {
if (0 == memcmp(s, "exports", 7)) break;
if (0 == memcmp(s, "imports", 7)) break;
if (0 == memcmp(s, "scripts", 7)) break;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
AddOption("--prof-process",
"process V8 profiler output generated using --prof",
&EnvironmentOptions::prof_process);
AddOption("--run",
"Run a script specified in package.json",
&EnvironmentOptions::run);
// Options after --prof-process are passed through to the prof processor.
AddAlias("--prof-process", { "--prof-process", "--" });
#if HAVE_INSPECTOR
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class EnvironmentOptions : public Options {
false;
#endif // DEBUG

std::string run;
bool watch_mode = false;
bool watch_mode_report_to_parent = false;
bool watch_mode_preserve_output = false;
Expand Down