All Projects → npm → run-script

npm / run-script

Licence: ISC License
Run a lifecycle script for a package (descendant of npm-lifecycle)

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to run-script

lint
lint the npmcli way
Stars: ✭ 27 (-6.9%)
Mutual labels:  npm-cli
proggy
Progress bar updates at a distance
Stars: ✭ 12 (-58.62%)
Mutual labels:  npm-cli
pacote
npm fetcher
Stars: ✭ 179 (+517.24%)
Mutual labels:  npm-cli
parse-conflict-json
Parse a JSON string that has git merge conflicts, resolving if possible
Stars: ✭ 24 (-17.24%)
Mutual labels:  npm-cli
libnpmpublish
programmatically publish and unpublish npm packages
Stars: ✭ 44 (+51.72%)
Mutual labels:  npm-cli
config
Configuration management for https://github.com/npm/cli
Stars: ✭ 22 (-24.14%)
Mutual labels:  npm-cli
cacache
npm's content-addressable cache
Stars: ✭ 181 (+524.14%)
Mutual labels:  npm-cli
cmd-shim
The cmd-shim used in npm
Stars: ✭ 65 (+124.14%)
Mutual labels:  npm-cli
libnpmsearch
programmatic API for the shiny new npm search endpoint
Stars: ✭ 25 (-13.79%)
Mutual labels:  npm-cli
cross-post
Cross Post a blog to multiple websites
Stars: ✭ 66 (+127.59%)
Mutual labels:  npm-cli
git
a util for spawning git from npm CLI contexts
Stars: ✭ 48 (+65.52%)
Mutual labels:  npm-cli
fs-minipass
fs read and write streams based on minipass
Stars: ✭ 15 (-48.28%)
Mutual labels:  npm-cli
npm-registry-mock
mock the npm registry
Stars: ✭ 26 (-10.34%)
Mutual labels:  npm-cli
node-cli-boilerplate
🪓 Create node cli with this user friendly boilerplate
Stars: ✭ 17 (-41.38%)
Mutual labels:  npm-cli
npm-profile
Make changes to your npmjs.com profile via cli or library
Stars: ✭ 29 (+0%)
Mutual labels:  npm-cli
inflight
Add callbacks to requests in flight to avoid async duplication
Stars: ✭ 63 (+117.24%)
Mutual labels:  npm-cli
Cli
the package manager for JavaScript
Stars: ✭ 5,277 (+18096.55%)
Mutual labels:  npm-cli
Node Semver
The semver parser for node (the one npm uses)
Stars: ✭ 3,944 (+13500%)
Mutual labels:  npm-cli

@npmcli/run-script

Run a lifecycle script for a package (descendant of npm-lifecycle)

USAGE

const runScript = require('@npmcli/run-script')

runScript({
  // required, the script to run
  event: 'install',

  // extra args to pass to the command, defaults to []
  args: [],

  // required, the folder where the package lives
  path: '/path/to/package/folder',

  // optional, defaults to /bin/sh on unix, or cmd.exe on windows
  scriptShell: '/bin/bash',

  // optional, defaults to false
  // return stdout and stderr as strings rather than buffers
  stdioString: true,

  // optional, additional environment variables to add
  // note that process.env IS inherited by default
  // Always set:
  // - npm_package_json The package.json file in the folder
  // - npm_lifecycle_event The event that this is being run for
  // - npm_lifecycle_script The script being run
  // The fields described in https://github.com/npm/rfcs/pull/183
  env: {
    npm_package_from: 'foo@bar',
    npm_package_resolved: 'https://registry.npmjs.org/foo/-/foo-1.2.3.tgz',
    npm_package_integrity: 'sha512-foobarbaz',
  },

  // defaults to 'pipe'.  Can also pass an array like you would to node's
  // exec or spawn functions.  Note that if it's anything other than
  // 'pipe' then the stdout/stderr values on the result will be missing.
  // npm cli sets this to 'inherit' for explicit run-scripts (test, etc.)
  // but leaves it as 'pipe' for install scripts that run in parallel.
  stdio: 'inherit',

  // print the package id and script, and the command to be run, like:
  // > [email protected] postinstall
  // > make all-the-things
  // Defaults true when stdio:'inherit', otherwise suppressed
  banner: true,
})
  .then(({ code, signal, stdout, stderr, pkgid, path, event, script }) => {
    // do something with the results
  })
  .catch(er => {
    // command did not work.
    // er is decorated with:
    // - code
    // - signal
    // - stdout
    // - stderr
    // - path
    // - pkgid (name@version string)
    // - event
    // - script
  })

API

Call the exported runScript function with an options object.

Returns a promise that resolves to the result of the execution. Promise rejects if the execution fails (exits non-zero) or has any other error. Rejected errors are decorated with the same values as the result object.

If the stdio options mean that it'll have a piped stdin, then the stdin is ended immediately on the child process. If stdin is shared with the parent terminal, then it is up to the user to end it, of course.

Results

  • code Process exit code
  • signal Process exit signal
  • stdout stdout data (Buffer, or String when stdioString set to true)
  • stderr stderr data (Buffer, or String when stdioString set to true)
  • path Path to the package executing its script
  • event Lifecycle event being run
  • script Command being run

Options

  • path Required. The path to the package having its script run.
  • event Required. The event being executed.
  • args Optional, default []. Extra arguments to pass to the script.
  • env Optional, object of fields to add to the environment of the subprocess. Note that process.env IS inherited by default These are always set:
    • npm_package_json The package.json file in the folder
    • npm_lifecycle_event The event that this is being run for
    • npm_lifecycle_script The script being run
    • The package.json fields described in RFC183.
  • scriptShell Optional, defaults to /bin/sh on Unix, defaults to env.ComSpec or cmd on Windows. Custom script to use to execute the command.
  • stdio Optional, defaults to 'pipe'. The same as the stdio argument passed to child_process functions in Node.js. Note that if a stdio output is set to anything other than pipe, it will not be present in the result/error object.
  • cmd Optional. Override the script from the package.json with something else, which will be run in an otherwise matching environment.
  • stdioString Optional, defaults to false. Return string values for stderr and stdout rather than Buffers.
  • banner Optional, defaults to true. If the stdio option is set to 'inherit', then print a banner with the package name and version, event name, and script command to be run. Set explicitly to false to disable for inherited stdio.

Note that this does not run pre-event and post-event scripts. The caller has to manage that process themselves.

Differences from npm-lifecycle

This is an implementation to satisfy RFC 90, RFC 77, and RFC 73.

Apart from those behavior changes in npm v7, this is also just refresh of the codebase, with modern coding techniques and better test coverage.

Functionally, this means:

  • Output is not dumped to the top level process's stdio by default.
  • Less stuff is put into the environment.
  • It is not opinionated about logging. (So, at least with the logging framework in npm v7.0 and before, the caller has to call log.disableProgress() and log.enableProgress() at the appropriate times, if necessary.)
  • The directory containing the node executable is never added to the PATH environment variable. (Ie, --scripts-prepend-node-path is effectively always set to false.) Doing so causes more unintended side effects than it ever prevented.
  • Hook scripts are not run by this module. If the caller wishes to run hook scripts, then they can override the default package script with an explicit cmd option pointing to the node_modules/.hook/${event} script.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].