Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

runtime-info.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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("path");
  10. const spawn = require("cross-spawn");
  11. const os = require("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. * @returns {string} The version returned by the command.
  37. */
  38. function execCommand(cmd, args) {
  39. const key = [cmd, ...args].join(" ");
  40. if (cache.has(key)) {
  41. return cache.get(key);
  42. }
  43. const process = spawn.sync(cmd, args, { encoding: "utf8" });
  44. if (process.error) {
  45. throw process.error;
  46. }
  47. const result = process.stdout.trim();
  48. cache.set(key, result);
  49. return result;
  50. }
  51. /**
  52. * Normalizes a version number.
  53. * @param {string} versionStr The string to normalize.
  54. * @returns {string} The normalized version number.
  55. */
  56. function normalizeVersionStr(versionStr) {
  57. return versionStr.startsWith("v") ? versionStr : `v${versionStr}`;
  58. }
  59. /**
  60. * Gets bin version.
  61. * @param {string} bin The bin to check.
  62. * @returns {string} The normalized version returned by the command.
  63. */
  64. function getBinVersion(bin) {
  65. const binArgs = ["--version"];
  66. try {
  67. return normalizeVersionStr(execCommand(bin, binArgs));
  68. } catch (e) {
  69. log.error(`Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}\``);
  70. throw e;
  71. }
  72. }
  73. /**
  74. * Gets installed npm package version.
  75. * @param {string} pkg The package to check.
  76. * @param {boolean} global Whether to check globally or not.
  77. * @returns {string} The normalized version returned by the command.
  78. */
  79. function getNpmPackageVersion(pkg, { global = false } = {}) {
  80. const npmBinArgs = ["bin", "-g"];
  81. const npmLsArgs = ["ls", "--depth=0", "--json", "eslint"];
  82. if (global) {
  83. npmLsArgs.push("-g");
  84. }
  85. try {
  86. const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs));
  87. /*
  88. * Checking globally returns an empty JSON object, while local checks
  89. * include the name and version of the local project.
  90. */
  91. if (Object.keys(parsedStdout).length === 0 || !(parsedStdout.dependencies && parsedStdout.dependencies.eslint)) {
  92. return "Not found";
  93. }
  94. const [, processBinPath] = process.argv;
  95. let npmBinPath;
  96. try {
  97. npmBinPath = execCommand("npm", npmBinArgs);
  98. } catch (e) {
  99. log.error(`Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}\``);
  100. throw e;
  101. }
  102. const isGlobal = isChildOfDirectory(npmBinPath, processBinPath);
  103. let pkgVersion = parsedStdout.dependencies.eslint.version;
  104. if ((global && isGlobal) || (!global && !isGlobal)) {
  105. pkgVersion += " (Currently used)";
  106. }
  107. return normalizeVersionStr(pkgVersion);
  108. } catch (e) {
  109. log.error(`Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}\``);
  110. throw e;
  111. }
  112. }
  113. return [
  114. "Environment Info:",
  115. "",
  116. `Node version: ${getBinVersion("node")}`,
  117. `npm version: ${getBinVersion("npm")}`,
  118. `Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`,
  119. `Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`,
  120. `Operating System: ${os.platform()} ${os.release()}`
  121. ].join("\n");
  122. }
  123. /**
  124. * Returns version of currently executing ESLint.
  125. * @returns {string} The version from the currently executing ESLint's package.json.
  126. */
  127. function version() {
  128. return `v${packageJson.version}`;
  129. }
  130. //------------------------------------------------------------------------------
  131. // Public Interface
  132. //------------------------------------------------------------------------------
  133. module.exports = {
  134. environment,
  135. version
  136. };