index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. const path = require("path");
  3. const j = require("jscodeshift");
  4. const chalk = require("chalk");
  5. const pEachSeries = require("p-each-series");
  6. const runPrettier = require("../utils/run-prettier");
  7. const astTransform = require("../ast");
  8. const propTypes = require("../utils/prop-types");
  9. /**
  10. *
  11. * Maps back transforms that needs to be run using the configuration
  12. * provided.
  13. *
  14. * @param {Object} transformObject - An Object with all transformations
  15. * @param {Object} config - Configuration to transform
  16. * @returns {Object} - An Object with the transformations to be run
  17. */
  18. function mapOptionsToTransform(config) {
  19. return Object.keys(config.webpackOptions).filter(k => propTypes.has(k));
  20. }
  21. /**
  22. *
  23. * Runs the transformations from an object we get from yeoman
  24. *
  25. * @param {Object} webpackProperties - Configuration to transform
  26. * @param {String} action - Action to be done on the given ast
  27. * @returns {Promise} - A promise that writes each transform, runs prettier
  28. * and writes the file
  29. */
  30. module.exports = function runTransform(webpackProperties, action) {
  31. // webpackOptions.name sent to nameTransform if match
  32. const webpackConfig = Object.keys(webpackProperties).filter(p => {
  33. return p !== "configFile" && p !== "configPath";
  34. });
  35. const initActionNotDefined = action && action !== "init" ? true : false;
  36. webpackConfig.forEach(scaffoldPiece => {
  37. const config = webpackProperties[scaffoldPiece];
  38. const transformations = mapOptionsToTransform(config);
  39. const ast = j(
  40. initActionNotDefined
  41. ? webpackProperties.configFile
  42. : "module.exports = {}"
  43. );
  44. const transformAction = action || null;
  45. return pEachSeries(transformations, f => {
  46. return astTransform(j, ast, config.webpackOptions[f], transformAction, f);
  47. })
  48. .then(_ => {
  49. let configurationName;
  50. if (!config.configName) {
  51. configurationName = "webpack.config.js";
  52. } else {
  53. configurationName = "webpack." + config.configName + ".js";
  54. }
  55. const outputPath = initActionNotDefined
  56. ? webpackProperties.configPath
  57. : path.join(process.cwd(), configurationName);
  58. const source = ast.toSource({
  59. quote: "single"
  60. });
  61. runPrettier(outputPath, source);
  62. })
  63. .catch(err => {
  64. console.error(err.message ? err.message : err);
  65. });
  66. });
  67. if (initActionNotDefined && webpackProperties.config.item) {
  68. process.stdout.write(
  69. "\n" +
  70. chalk.green(
  71. `Congratulations! ${
  72. webpackProperties.config.item
  73. } has been ${action}ed!\n`
  74. )
  75. );
  76. } else {
  77. process.stdout.write(
  78. "\n" +
  79. chalk.green(
  80. "Congratulations! Your new webpack configuration file has been created!\n"
  81. )
  82. );
  83. }
  84. };