ProcessUtils.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict'
  2. module.exports = {
  3. injectModules: function() {
  4. if (process.env.pmx !== 'false') {
  5. const pmx = require('@pm2/io')
  6. let conf = {}
  7. const hasSpecificConfig = typeof process.env.io === 'string' || process.env.trace === 'true'
  8. // pmx is already init, no need to do it twice
  9. if (hasSpecificConfig === false) return
  10. if (process.env.io) {
  11. const io = JSON.parse(process.env.io)
  12. conf = io.conf ? io.conf : conf
  13. }
  14. pmx.init(Object.assign({
  15. tracing: process.env.trace === 'true' || false
  16. }, conf))
  17. }
  18. },
  19. isESModule(exec_path) {
  20. var fs = require('fs')
  21. var path = require('path')
  22. var semver = require('semver')
  23. var data
  24. var findPackageJson = function(directory) {
  25. var file = path.join(directory, 'package.json')
  26. if (fs.existsSync(file) && fs.statSync(file).isFile()) {
  27. return file;
  28. }
  29. var parent = path.resolve(directory, '..')
  30. if (parent === directory) {
  31. return null;
  32. }
  33. return findPackageJson(parent)
  34. }
  35. if (semver.satisfies(process.version, '< 13.3.0'))
  36. return false
  37. if (path.extname(exec_path) === '.mjs')
  38. return true
  39. try {
  40. data = JSON.parse(fs.readFileSync(findPackageJson(path.dirname(exec_path))))
  41. if (data.type === 'module')
  42. return true
  43. else
  44. return false
  45. } catch(e) {
  46. }
  47. }
  48. }