-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
5 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const { | ||
JSONParse, | ||
ObjectKeys, | ||
} = primordials; | ||
|
||
const { | ||
prepareMainThreadExecution, | ||
markBootstrapComplete, | ||
} = require('internal/process/pre_execution'); | ||
const { getPackageJSONScripts } = internalBinding('modules'); | ||
const { execSync } = require('child_process'); | ||
const { log, error } = require('internal/console/global'); | ||
|
||
prepareMainThreadExecution(false, false); | ||
|
||
markBootstrapComplete(); | ||
|
||
const json_string = getPackageJSONScripts('package.json'); | ||
|
||
// Check if package.json exists and is parseable | ||
if (json_string === undefined) { | ||
process.exit(1); | ||
return; | ||
} | ||
const scripts = JSONParse(json_string); | ||
const id = process.argv.at(2); | ||
const command = scripts[id]; | ||
|
||
if (!command) { | ||
error(`Missing script: "${id}"`); | ||
|
||
const keys = ObjectKeys(scripts); | ||
if (keys.length === 0) { | ||
error('There are no scripts available in package.json'); | ||
} else { | ||
error('Available scripts are:\n'); | ||
for (const script of ObjectKeys(scripts)) { | ||
error(` ${script}: ${scripts[script]}`); | ||
} | ||
} | ||
process.exit(1); | ||
} | ||
|
||
log(''); | ||
log('>', id); | ||
log('>', command); | ||
log(''); | ||
|
||
execSync(command, { stdio: 'inherit' }); |
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