/ student-intifada / node_modules / @npmcli / run-script /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[DIR]lib/a year ago -  
[DIR]node_modules/a year ago -  
[   ]LICENSEa year ago739  
[TXT]README.mda year ago6.8K595aea1 more query options + view options [كارل مبارك]
[   ]package.jsona year ago1.3Kafd0ccc remove unused [كارل مبارك]
README.md

@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, these paths will be put at the beginning of `$PATH`, even
  // after run-script adds the node_modules/.bin folder(s) from
  // `process.cwd()`. This is for commands like `npm init`, `npm exec`,
  // and `npx` to make sure manually installed  packages come before
  // anything that happens to be in the tree in `process.cwd()`.
  binPaths: [
    '/path/to/npx/node_modules/.bin',
    '/path/to/npm/prefix/node_modules/.bin',
  ],

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

  // optional, passed directly to `@npmcli/promise-spawn` which defaults it to true
  // return stdout and stderr as strings rather than buffers
  stdioString: false,

  // 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:
  // > somepackage@1.2.3 postinstall
  // > make all-the-things
})
  .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

If stdio is inherit this package will emit a banner with the package name and version, event name, and script command to be run, and send it to proc-log.output.standard. Consuming libraries can decide whether or not to display this.

Options

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:

Escaping

In order to ensure that arguments are handled consistently, this module writes a temporary script file containing the command as it exists in the package.json, followed by the user supplied arguments having been escaped to ensure they are processed as literal strings. We then instruct the shell to execute the script file, and when the process exits we remove the temporary file.

In Windows, when the shell is cmd, and when the initial command in the script is a known batch file (i.e. something.cmd) we double escape additional arguments so that the shim scripts npm installs work correctly.

The actual implementation of the escaping is in lib/escape.js.

Apache/2.4.38 (Debian) Server at www.karls.computer Port 80