-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: implement
node run <script-in-package-json>
- Loading branch information
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
/* eslint-disable node-core/prefer-primordials */ | ||
|
||
// There is no need to add primordials to this file. | ||
// `run.js` is a script only executed when `node run <script>` is called. | ||
const { | ||
prepareMainThreadExecution, | ||
markBootstrapComplete, | ||
} = require('internal/process/pre_execution'); | ||
const { getPackageJSONScripts } = internalBinding('modules'); | ||
const { execSync } = require('child_process'); | ||
const { resolve, delimiter } = require('path'); | ||
|
||
prepareMainThreadExecution(false, false); | ||
|
||
markBootstrapComplete(); | ||
|
||
// TODO(@anonrig): Search for all package.json's until root folder. | ||
const json_string = getPackageJSONScripts(); | ||
|
||
// Check if package.json exists and is parseable | ||
if (json_string === undefined) { | ||
process.exit(1); | ||
return; | ||
} | ||
const scripts = JSON.parse(json_string); | ||
const id = process.argv.at(2); | ||
const command = scripts[id]; | ||
|
||
if (!command) { | ||
const { error } = require('internal/console/global'); | ||
|
||
error(`Missing script: "${id}"`); | ||
|
||
const keys = Object.keys(scripts); | ||
if (keys.length === 0) { | ||
error('There are no scripts available in package.json'); | ||
} else { | ||
error('Available scripts are:\n'); | ||
for (const script of keys) { | ||
error(` ${script}: ${scripts[script]}`); | ||
} | ||
} | ||
process.exit(1); | ||
} | ||
|
||
const env = process.env; | ||
const cwd = process.cwd(); | ||
const binPath = resolve(cwd, 'node_modules/.bin'); | ||
|
||
// Filter all environment variables that contain the word "path" | ||
const keys = Object.keys(env).filter((key) => /^path$/i.test(key)); | ||
const PATH = keys.map((key) => env[key]); | ||
|
||
// Append only the current folder bin path to the PATH variable. | ||
// TODO(@anonrig): Add support for appending the bin path of all parent folders. | ||
const paths = [binPath, PATH].join(delimiter); | ||
for (const key of keys) { | ||
env[key] = paths; | ||
} | ||
execSync(command, { stdio: 'inherit', env }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters