resolve-packages.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. const path = require("path");
  3. const chalk = require("chalk");
  4. const modifyConfigHelper = require("./modify-config-helper");
  5. const getPathToGlobalPackages = require("./package-manager")
  6. .getPathToGlobalPackages;
  7. const isLocalPath = require("./is-local-path");
  8. const spawnChild = require("./package-manager").spawnChild;
  9. /**
  10. *
  11. * Attaches a promise to the installation of the package
  12. *
  13. * @param {Function} child - The function to attach a promise to
  14. * @returns {Promise} promise - Returns a promise to the installation
  15. */
  16. function processPromise(child) {
  17. return new Promise(function(resolve, reject) {
  18. //eslint-disable-line
  19. if (child.status !== 0) {
  20. reject();
  21. } else {
  22. resolve();
  23. }
  24. });
  25. }
  26. /**
  27. *
  28. * Resolves and installs the packages, later sending them to @creator
  29. *
  30. * @param {String[]} pkg - The dependencies to be installed
  31. * @returns {Function|Error} creator - Builds
  32. * a webpack configuration through yeoman or throws an error
  33. */
  34. function resolvePackages(pkg) {
  35. Error.stackTraceLimit = 30;
  36. let packageLocations = [];
  37. function invokeGeneratorIfReady() {
  38. if (packageLocations.length === pkg.length)
  39. return modifyConfigHelper("init", null, packageLocations);
  40. }
  41. pkg.forEach(addon => {
  42. // Resolve paths to modules on local filesystem
  43. if (isLocalPath(addon)) {
  44. let absolutePath = addon;
  45. try {
  46. absolutePath = path.resolve(process.cwd(), addon);
  47. require.resolve(absolutePath);
  48. packageLocations.push(absolutePath);
  49. } catch (err) {
  50. console.log(`Cannot find a generator at ${absolutePath}.`);
  51. console.log("\nReason:\n");
  52. console.error(chalk.bold.red(err));
  53. process.exitCode = 1;
  54. }
  55. invokeGeneratorIfReady();
  56. return;
  57. }
  58. // Resolve modules on npm registry
  59. processPromise(spawnChild(addon))
  60. .then(_ => {
  61. try {
  62. const globalPath = getPathToGlobalPackages();
  63. packageLocations.push(path.resolve(globalPath, addon));
  64. } catch (err) {
  65. console.log("Package wasn't validated correctly..");
  66. console.log("Submit an issue for", pkg, "if this persists");
  67. console.log("\nReason: \n");
  68. console.error(chalk.bold.red(err));
  69. process.exitCode = 1;
  70. }
  71. })
  72. .catch(err => {
  73. console.log("Package couldn't be installed, aborting..");
  74. console.log("\nReason: \n");
  75. console.error(chalk.bold.red(err));
  76. process.exitCode = 1;
  77. })
  78. .then(invokeGeneratorIfReady);
  79. });
  80. }
  81. module.exports = {
  82. resolvePackages,
  83. processPromise
  84. };