index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var noop = function () { };
  2. var path = require('path');
  3. const semver = require('semver');
  4. var version = process.versions.node.split('.') || [null, null, null];
  5. var utils = (module.exports = {
  6. semver: semver,
  7. satisfies: test => semver.satisfies(process.versions.node, test),
  8. version: {
  9. major: parseInt(version[0] || 0, 10),
  10. minor: parseInt(version[1] || 0, 10),
  11. patch: parseInt(version[2] || 0, 10),
  12. },
  13. clone: require('./clone'),
  14. merge: require('./merge'),
  15. bus: require('./bus'),
  16. isWindows: process.platform === 'win32',
  17. isMac: process.platform === 'darwin',
  18. isLinux: process.platform === 'linux',
  19. isRequired: (function () {
  20. var p = module.parent;
  21. while (p) {
  22. // in electron.js engine it happens
  23. if (!p.filename) {
  24. return true;
  25. }
  26. if (p.filename.indexOf('bin' + path.sep + 'nodemon.js') !== -1) {
  27. return false;
  28. }
  29. p = p.parent;
  30. }
  31. return true;
  32. })(),
  33. home: process.env.HOME || process.env.HOMEPATH,
  34. quiet: function () {
  35. // nukes the logging
  36. if (!this.debug) {
  37. for (var method in utils.log) {
  38. if (typeof utils.log[method] === 'function') {
  39. utils.log[method] = noop;
  40. }
  41. }
  42. }
  43. },
  44. reset: function () {
  45. if (!this.debug) {
  46. for (var method in utils.log) {
  47. if (typeof utils.log[method] === 'function') {
  48. delete utils.log[method];
  49. }
  50. }
  51. }
  52. this.debug = false;
  53. },
  54. regexpToText: function (t) {
  55. return t
  56. .replace(/\.\*\\./g, '*.')
  57. .replace(/\\{2}/g, '^^')
  58. .replace(/\\/g, '')
  59. .replace(/\^\^/g, '\\');
  60. },
  61. stringify: function (exec, args) {
  62. // serializes an executable string and array of arguments into a string
  63. args = args || [];
  64. return [exec]
  65. .concat(
  66. args.map(function (arg) {
  67. // if an argument contains a space, we want to show it with quotes
  68. // around it to indicate that it is a single argument
  69. if (arg.length > 0 && arg.indexOf(' ') === -1) {
  70. return arg;
  71. }
  72. // this should correctly escape nested quotes
  73. return JSON.stringify(arg);
  74. })
  75. )
  76. .join(' ')
  77. .trim();
  78. },
  79. });
  80. utils.log = require('./log')(utils.isRequired);
  81. Object.defineProperty(utils, 'debug', {
  82. set: function (value) {
  83. this.log.debug = value;
  84. },
  85. get: function () {
  86. return this.log.debug;
  87. },
  88. });
  89. Object.defineProperty(utils, 'colours', {
  90. set: function (value) {
  91. this.log.useColours = value;
  92. },
  93. get: function () {
  94. return this.log.useColours;
  95. },
  96. });