entry.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. "use strict";
  2. const InputValidate = require("webpack-addons").InputValidate;
  3. const validate = require("./validate");
  4. /**
  5. *
  6. * Prompts for entry points, either if it has multiple or one entry
  7. *
  8. * @param {Object} self - A variable holding the instance of the prompting
  9. * @param {Object} answer - Previous answer from asking if the user wants single or multiple entries
  10. * @returns {Object} An Object that holds the answers given by the user, later used to scaffold
  11. */
  12. module.exports = (self, answer) => {
  13. let entryIdentifiers;
  14. let result;
  15. if (answer["entryType"] === true) {
  16. result = self
  17. .prompt([
  18. InputValidate(
  19. "multipleEntries",
  20. "Type the names you want for your modules (entry files), separated by comma [example: app,vendor]",
  21. validate
  22. )
  23. ])
  24. .then(multipleEntriesAnswer => {
  25. let webpackEntryPoint = {};
  26. entryIdentifiers = multipleEntriesAnswer["multipleEntries"].split(",");
  27. function forEachPromise(obj, fn) {
  28. return obj.reduce((promise, prop) => {
  29. const trimmedProp = prop.trim();
  30. return promise.then(n => {
  31. if (n) {
  32. Object.keys(n).forEach(val => {
  33. if (
  34. n[val].charAt(0) !== "(" &&
  35. n[val].charAt(0) !== "[" &&
  36. !n[val].includes("function") &&
  37. !n[val].includes("path") &&
  38. !n[val].includes("process")
  39. ) {
  40. n[val] = `\'${n[val].replace(/"|'/g, "").concat(".js")}\'`;
  41. }
  42. webpackEntryPoint[val] = n[val];
  43. });
  44. } else {
  45. n = {};
  46. }
  47. return fn(trimmedProp);
  48. });
  49. }, Promise.resolve());
  50. }
  51. return forEachPromise(entryIdentifiers, entryProp =>
  52. self.prompt([
  53. InputValidate(
  54. `${entryProp}`,
  55. `What is the location of "${entryProp}"? [example: ./src/${entryProp}]`,
  56. validate
  57. )
  58. ])
  59. ).then(entryPropAnswer => {
  60. Object.keys(entryPropAnswer).forEach(val => {
  61. if (
  62. entryPropAnswer[val].charAt(0) !== "(" &&
  63. entryPropAnswer[val].charAt(0) !== "[" &&
  64. !entryPropAnswer[val].includes("function") &&
  65. !entryPropAnswer[val].includes("path") &&
  66. !entryPropAnswer[val].includes("process")
  67. ) {
  68. entryPropAnswer[val] = `\'${entryPropAnswer[val]
  69. .replace(/"|'/g, "")
  70. .concat(".js")}\'`;
  71. }
  72. webpackEntryPoint[val] = entryPropAnswer[val];
  73. });
  74. return webpackEntryPoint;
  75. });
  76. });
  77. } else {
  78. result = self
  79. .prompt([
  80. InputValidate(
  81. "singularEntry",
  82. "Which module will be the first to enter the application? [default: ./src/index]"
  83. )
  84. ])
  85. .then(singularEntryAnswer => {
  86. let { singularEntry } = singularEntryAnswer;
  87. if (singularEntry.indexOf("\"") >= 0)
  88. singularEntry = singularEntry.replace(/"/g, "'");
  89. return singularEntry;
  90. });
  91. }
  92. return result;
  93. };