runtime-info.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @fileoverview Utility to get information about the execution environment.
  3. * @author Kai Cataldo
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const path = require("node:path");
  10. const spawn = require("cross-spawn");
  11. const os = require("node:os");
  12. const log = require("../shared/logging");
  13. const packageJson = require("../../package.json");
  14. //------------------------------------------------------------------------------
  15. // Helpers
  16. //------------------------------------------------------------------------------
  17. /**
  18. * Generates and returns execution environment information.
  19. * @returns {string} A string that contains execution environment information.
  20. */
  21. function environment() {
  22. const cache = new Map();
  23. /**
  24. * Checks if a path is a child of a directory.
  25. * @param {string} parentPath The parent path to check.
  26. * @param {string} childPath The path to check.
  27. * @returns {boolean} Whether or not the given path is a child of a directory.
  28. */
  29. function isChildOfDirectory(parentPath, childPath) {
  30. return !path.relative(parentPath, childPath).startsWith("..");
  31. }
  32. /**
  33. * Synchronously executes a shell command and formats the result.
  34. * @param {string} cmd The command to execute.
  35. * @param {Array} args The arguments to be executed with the command.
  36. * @throws {Error} As may be collected by `cross-spawn.sync`.
  37. * @returns {string} The version returned by the command.
  38. */
  39. function execCommand(cmd, args) {
  40. const key = [cmd, ...args].join(" ");
  41. if (cache.has(key)) {
  42. return cache.get(key);
  43. }
  44. const process = spawn.sync(cmd, args, { encoding: "utf8" });
  45. if (process.error) {
  46. throw process.error;
  47. }
  48. const result = process.stdout.trim();
  49. cache.set(key, result);
  50. return result;
  51. }
  52. /**
  53. * Normalizes a version number.
  54. * @param {string} versionStr The string to normalize.
  55. * @returns {string} The normalized version number.
  56. */
  57. function normalizeVersionStr(versionStr) {
  58. return versionStr.startsWith("v") ? versionStr : `v${versionStr}`;
  59. }
  60. /**
  61. * Gets bin version.
  62. * @param {string} bin The bin to check.
  63. * @throws {Error} As may be collected by `cross-spawn.sync`.
  64. * @returns {string} The normalized version returned by the command.
  65. */
  66. function getBinVersion(bin) {
  67. const binArgs = ["--version"];
  68. try {
  69. return normalizeVersionStr(execCommand(bin, binArgs));
  70. } catch (e) {
  71. log.error(
  72. `Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}\``,
  73. );
  74. throw e;
  75. }
  76. }
  77. /**
  78. * Gets installed npm package version.
  79. * @param {string} pkg The package to check.
  80. * @param {boolean} global Whether to check globally or not.
  81. * @throws {Error} As may be collected by `cross-spawn.sync`.
  82. * @returns {string} The normalized version returned by the command.
  83. */
  84. function getNpmPackageVersion(pkg, { global = false } = {}) {
  85. const npmBinArgs = ["bin", "-g"];
  86. const npmLsArgs = ["ls", "--depth=0", "--json", pkg];
  87. if (global) {
  88. npmLsArgs.push("-g");
  89. }
  90. try {
  91. const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs));
  92. /*
  93. * Checking globally returns an empty JSON object, while local checks
  94. * include the name and version of the local project.
  95. */
  96. if (
  97. Object.keys(parsedStdout).length === 0 ||
  98. !(parsedStdout.dependencies && parsedStdout.dependencies.eslint)
  99. ) {
  100. return "Not found";
  101. }
  102. const [, processBinPath] = process.argv;
  103. let npmBinPath;
  104. try {
  105. npmBinPath = execCommand("npm", npmBinArgs);
  106. } catch (e) {
  107. log.error(
  108. `Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}\``,
  109. );
  110. throw e;
  111. }
  112. const isGlobal = isChildOfDirectory(npmBinPath, processBinPath);
  113. let pkgVersion = parsedStdout.dependencies.eslint.version;
  114. if ((global && isGlobal) || (!global && !isGlobal)) {
  115. pkgVersion += " (Currently used)";
  116. }
  117. return normalizeVersionStr(pkgVersion);
  118. } catch (e) {
  119. log.error(
  120. `Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}\``,
  121. );
  122. throw e;
  123. }
  124. }
  125. return [
  126. "Environment Info:",
  127. "",
  128. `Node version: ${process.version}`,
  129. `npm version: ${getBinVersion("npm")}`,
  130. `Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`,
  131. `Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`,
  132. `Operating System: ${os.platform()} ${os.release()}`,
  133. ].join("\n");
  134. }
  135. /**
  136. * Returns version of currently executing ESLint.
  137. * @returns {string} The version from the currently executing ESLint's package.json.
  138. */
  139. function version() {
  140. return `v${packageJson.version}`;
  141. }
  142. //------------------------------------------------------------------------------
  143. // Public Interface
  144. //------------------------------------------------------------------------------
  145. module.exports = {
  146. __esModule: true, // Indicate intent for imports, remove ambiguity for Knip (see: https://github.com/eslint/eslint/pull/18005#discussion_r1484422616)
  147. environment,
  148. version,
  149. };