11cb0ef41Sopenharmony_ci/* eslint-disable max-len */
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Separated out for easier unit testing
41cb0ef41Sopenharmony_cimodule.exports = async (process, validateEngines) => {
51cb0ef41Sopenharmony_ci  // set it here so that regardless of what happens later, we don't
61cb0ef41Sopenharmony_ci  // leak any private CLI configs to other programs
71cb0ef41Sopenharmony_ci  process.title = 'npm'
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci  // if npm is called as "npmg" or "npm_g", then run in global mode.
101cb0ef41Sopenharmony_ci  if (process.argv[1][process.argv[1].length - 1] === 'g') {
111cb0ef41Sopenharmony_ci    process.argv.splice(1, 1, 'npm', '-g')
121cb0ef41Sopenharmony_ci  }
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  const satisfies = require('semver/functions/satisfies')
151cb0ef41Sopenharmony_ci  const exitHandler = require('./utils/exit-handler.js')
161cb0ef41Sopenharmony_ci  const Npm = require('./npm.js')
171cb0ef41Sopenharmony_ci  const npm = new Npm()
181cb0ef41Sopenharmony_ci  exitHandler.setNpm(npm)
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // only log node and npm paths in argv initially since argv can contain sensitive info. a cleaned version will be logged later
211cb0ef41Sopenharmony_ci  const log = require('./utils/log-shim.js')
221cb0ef41Sopenharmony_ci  log.verbose('cli', process.argv.slice(0, 2).join(' '))
231cb0ef41Sopenharmony_ci  log.info('using', 'npm@%s', npm.version)
241cb0ef41Sopenharmony_ci  log.info('using', 'node@%s', process.version)
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // At this point we've required a few files and can be pretty sure we dont contain invalid syntax for this version of node. It's possible a lazy require would, but that's unlikely enough that it's not worth catching anymore and we attach the more important exit handlers.
271cb0ef41Sopenharmony_ci  validateEngines.off()
281cb0ef41Sopenharmony_ci  process.on('uncaughtException', exitHandler)
291cb0ef41Sopenharmony_ci  process.on('unhandledRejection', exitHandler)
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // It is now safe to log a warning if they are using a version of node that is not going to fail on syntax errors but is still unsupported and untested and might not work reliably. This is safe to use the logger now which we want since this will show up in the error log too.
321cb0ef41Sopenharmony_ci  if (!satisfies(validateEngines.node, validateEngines.engines)) {
331cb0ef41Sopenharmony_ci    log.warn('cli', validateEngines.unsupportedMessage)
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  let cmd
371cb0ef41Sopenharmony_ci  // Now actually fire up npm and run the command.
381cb0ef41Sopenharmony_ci  // This is how to use npm programmatically:
391cb0ef41Sopenharmony_ci  try {
401cb0ef41Sopenharmony_ci    await npm.load()
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    // npm -v
431cb0ef41Sopenharmony_ci    if (npm.config.get('version', 'cli')) {
441cb0ef41Sopenharmony_ci      npm.output(npm.version)
451cb0ef41Sopenharmony_ci      return exitHandler()
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci    // npm --versions
491cb0ef41Sopenharmony_ci    if (npm.config.get('versions', 'cli')) {
501cb0ef41Sopenharmony_ci      npm.argv = ['version']
511cb0ef41Sopenharmony_ci      npm.config.set('usage', false, 'cli')
521cb0ef41Sopenharmony_ci    }
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    cmd = npm.argv.shift()
551cb0ef41Sopenharmony_ci    if (!cmd) {
561cb0ef41Sopenharmony_ci      npm.output(npm.usage)
571cb0ef41Sopenharmony_ci      process.exitCode = 1
581cb0ef41Sopenharmony_ci      return exitHandler()
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    await npm.exec(cmd)
621cb0ef41Sopenharmony_ci    return exitHandler()
631cb0ef41Sopenharmony_ci  } catch (err) {
641cb0ef41Sopenharmony_ci    if (err.code === 'EUNKNOWNCOMMAND') {
651cb0ef41Sopenharmony_ci      const didYouMean = require('./utils/did-you-mean.js')
661cb0ef41Sopenharmony_ci      const suggestions = await didYouMean(npm.localPrefix, cmd)
671cb0ef41Sopenharmony_ci      npm.output(`Unknown command: "${cmd}"${suggestions}\n`)
681cb0ef41Sopenharmony_ci      npm.output('To see a list of supported npm commands, run:\n  npm help')
691cb0ef41Sopenharmony_ci      process.exitCode = 1
701cb0ef41Sopenharmony_ci      return exitHandler()
711cb0ef41Sopenharmony_ci    }
721cb0ef41Sopenharmony_ci    return exitHandler(err)
731cb0ef41Sopenharmony_ci  }
741cb0ef41Sopenharmony_ci}
75