11cb0ef41Sopenharmony_ci// This is separate to indicate that it should contain code we expect to work in
21cb0ef41Sopenharmony_ci// all versions of node >= 6.  This is a best effort to catch syntax errors to
31cb0ef41Sopenharmony_ci// give users a good error message if they are using a node version that doesn't
41cb0ef41Sopenharmony_ci// allow syntax we are using such as private properties, etc. This file is
51cb0ef41Sopenharmony_ci// linted with ecmaVersion=6 so we don't use invalid syntax, which is set in the
61cb0ef41Sopenharmony_ci// .eslintrc.local.json file
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst { engines: { node: engines }, version } = require('../../package.json')
91cb0ef41Sopenharmony_ciconst npm = `v${version}`
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cimodule.exports = (process, getCli) => {
121cb0ef41Sopenharmony_ci  const node = process.version
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  /* eslint-disable-next-line max-len */
151cb0ef41Sopenharmony_ci  const unsupportedMessage = `npm ${npm} does not support Node.js ${node}. This version of npm supports the following node versions: \`${engines}\`. You can find the latest version at https://nodejs.org/.`
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  /* eslint-disable-next-line max-len */
181cb0ef41Sopenharmony_ci  const brokenMessage = `ERROR: npm ${npm} is known not to run on Node.js ${node}.  This version of npm supports the following node versions: \`${engines}\`. You can find the latest version at https://nodejs.org/.`
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  // coverage ignored because this is only hit in very unsupported node versions
211cb0ef41Sopenharmony_ci  // and it's a best effort attempt to show something nice in those cases
221cb0ef41Sopenharmony_ci  /* istanbul ignore next */
231cb0ef41Sopenharmony_ci  const syntaxErrorHandler = (err) => {
241cb0ef41Sopenharmony_ci    if (err instanceof SyntaxError) {
251cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-console
261cb0ef41Sopenharmony_ci      console.error(`${brokenMessage}\n\nERROR:`)
271cb0ef41Sopenharmony_ci      // eslint-disable-next-line no-console
281cb0ef41Sopenharmony_ci      console.error(err)
291cb0ef41Sopenharmony_ci      return process.exit(1)
301cb0ef41Sopenharmony_ci    }
311cb0ef41Sopenharmony_ci    throw err
321cb0ef41Sopenharmony_ci  }
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  process.on('uncaughtException', syntaxErrorHandler)
351cb0ef41Sopenharmony_ci  process.on('unhandledRejection', syntaxErrorHandler)
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  // require this only after setting up the error handlers
381cb0ef41Sopenharmony_ci  const cli = getCli()
391cb0ef41Sopenharmony_ci  return cli(process, {
401cb0ef41Sopenharmony_ci    node,
411cb0ef41Sopenharmony_ci    npm,
421cb0ef41Sopenharmony_ci    engines,
431cb0ef41Sopenharmony_ci    unsupportedMessage,
441cb0ef41Sopenharmony_ci    off: () => {
451cb0ef41Sopenharmony_ci      process.off('uncaughtException', syntaxErrorHandler)
461cb0ef41Sopenharmony_ci      process.off('unhandledRejection', syntaxErrorHandler)
471cb0ef41Sopenharmony_ci    },
481cb0ef41Sopenharmony_ci  })
491cb0ef41Sopenharmony_ci}
50