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.

npm-utils.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @fileoverview Utility for executing npm commands.
  3. * @author Ian VanSchooten
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const fs = require("fs"),
  10. spawn = require("cross-spawn"),
  11. path = require("path"),
  12. log = require("../shared/logging");
  13. //------------------------------------------------------------------------------
  14. // Helpers
  15. //------------------------------------------------------------------------------
  16. /**
  17. * Find the closest package.json file, starting at process.cwd (by default),
  18. * and working up to root.
  19. * @param {string} [startDir=process.cwd()] Starting directory
  20. * @returns {string} Absolute path to closest package.json file
  21. */
  22. function findPackageJson(startDir) {
  23. let dir = path.resolve(startDir || process.cwd());
  24. do {
  25. const pkgFile = path.join(dir, "package.json");
  26. if (!fs.existsSync(pkgFile) || !fs.statSync(pkgFile).isFile()) {
  27. dir = path.join(dir, "..");
  28. continue;
  29. }
  30. return pkgFile;
  31. } while (dir !== path.resolve(dir, ".."));
  32. return null;
  33. }
  34. //------------------------------------------------------------------------------
  35. // Private
  36. //------------------------------------------------------------------------------
  37. /**
  38. * Install node modules synchronously and save to devDependencies in package.json
  39. * @param {string|string[]} packages Node module or modules to install
  40. * @returns {void}
  41. */
  42. function installSyncSaveDev(packages) {
  43. const packageList = Array.isArray(packages) ? packages : [packages];
  44. const npmProcess = spawn.sync("npm", ["i", "--save-dev"].concat(packageList), { stdio: "inherit" });
  45. const error = npmProcess.error;
  46. if (error && error.code === "ENOENT") {
  47. const pluralS = packageList.length > 1 ? "s" : "";
  48. log.error(`Could not execute npm. Please install the following package${pluralS} with a package manager of your choice: ${packageList.join(", ")}`);
  49. }
  50. }
  51. /**
  52. * Fetch `peerDependencies` of the given package by `npm show` command.
  53. * @param {string} packageName The package name to fetch peerDependencies.
  54. * @returns {Object} Gotten peerDependencies. Returns null if npm was not found.
  55. */
  56. function fetchPeerDependencies(packageName) {
  57. const npmProcess = spawn.sync(
  58. "npm",
  59. ["show", "--json", packageName, "peerDependencies"],
  60. { encoding: "utf8" }
  61. );
  62. const error = npmProcess.error;
  63. if (error && error.code === "ENOENT") {
  64. return null;
  65. }
  66. const fetchedText = npmProcess.stdout.trim();
  67. return JSON.parse(fetchedText || "{}");
  68. }
  69. /**
  70. * Check whether node modules are include in a project's package.json.
  71. * @param {string[]} packages Array of node module names
  72. * @param {Object} opt Options Object
  73. * @param {boolean} opt.dependencies Set to true to check for direct dependencies
  74. * @param {boolean} opt.devDependencies Set to true to check for development dependencies
  75. * @param {boolean} opt.startdir Directory to begin searching from
  76. * @returns {Object} An object whose keys are the module names
  77. * and values are booleans indicating installation.
  78. */
  79. function check(packages, opt) {
  80. const deps = new Set();
  81. const pkgJson = (opt) ? findPackageJson(opt.startDir) : findPackageJson();
  82. let fileJson;
  83. if (!pkgJson) {
  84. throw new Error("Could not find a package.json file. Run 'npm init' to create one.");
  85. }
  86. try {
  87. fileJson = JSON.parse(fs.readFileSync(pkgJson, "utf8"));
  88. } catch (e) {
  89. const error = new Error(e);
  90. error.messageTemplate = "failed-to-read-json";
  91. error.messageData = {
  92. path: pkgJson,
  93. message: e.message
  94. };
  95. throw error;
  96. }
  97. ["dependencies", "devDependencies"].forEach(key => {
  98. if (opt[key] && typeof fileJson[key] === "object") {
  99. Object.keys(fileJson[key]).forEach(dep => deps.add(dep));
  100. }
  101. });
  102. return packages.reduce((status, pkg) => {
  103. status[pkg] = deps.has(pkg);
  104. return status;
  105. }, {});
  106. }
  107. /**
  108. * Check whether node modules are included in the dependencies of a project's
  109. * package.json.
  110. *
  111. * Convenience wrapper around check().
  112. * @param {string[]} packages Array of node modules to check.
  113. * @param {string} rootDir The directory containing a package.json
  114. * @returns {Object} An object whose keys are the module names
  115. * and values are booleans indicating installation.
  116. */
  117. function checkDeps(packages, rootDir) {
  118. return check(packages, { dependencies: true, startDir: rootDir });
  119. }
  120. /**
  121. * Check whether node modules are included in the devDependencies of a project's
  122. * package.json.
  123. *
  124. * Convenience wrapper around check().
  125. * @param {string[]} packages Array of node modules to check.
  126. * @returns {Object} An object whose keys are the module names
  127. * and values are booleans indicating installation.
  128. */
  129. function checkDevDeps(packages) {
  130. return check(packages, { devDependencies: true });
  131. }
  132. /**
  133. * Check whether package.json is found in current path.
  134. * @param {string} [startDir] Starting directory
  135. * @returns {boolean} Whether a package.json is found in current path.
  136. */
  137. function checkPackageJson(startDir) {
  138. return !!findPackageJson(startDir);
  139. }
  140. //------------------------------------------------------------------------------
  141. // Public Interface
  142. //------------------------------------------------------------------------------
  143. module.exports = {
  144. installSyncSaveDev,
  145. fetchPeerDependencies,
  146. findPackageJson,
  147. checkDeps,
  148. checkDevDeps,
  149. checkPackageJson
  150. };