webpack-generator.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const path = require("path");
  2. const mkdirp = require("mkdirp");
  3. const Generator = require("yeoman-generator");
  4. const copyUtils = require("../utils/copy-utils");
  5. /**
  6. * Creates a Yeoman Generator that generates a project conforming
  7. * to webpack-defaults.
  8. *
  9. * @param {any[]} prompts An array of Yeoman prompt objects
  10. *
  11. * @param {string} templateDir Absolute path to template directory
  12. *
  13. * @param {string[]} copyFiles An array of file paths (relative to `./templates`)
  14. * of files to be copied to the generated project. File paths should be of the
  15. * form `path/to/file.js.tpl`.
  16. *
  17. * @param {string[]} copyTemplateFiles An array of file paths (relative to
  18. * `./templates`) of files to be copied to the generated project. Template
  19. * file paths should be of the form `path/to/_file.js.tpl`.
  20. *
  21. * @param {Function} templateFn A function that is passed a generator instance and
  22. * returns an object containing data to be supplied to the template files.
  23. *
  24. * @returns {Generator} A class extending Generator
  25. */
  26. function webpackGenerator(
  27. prompts,
  28. templateDir,
  29. copyFiles,
  30. copyTemplateFiles,
  31. templateFn
  32. ) {
  33. //eslint-disable-next-line
  34. return class extends Generator {
  35. prompting() {
  36. return this.prompt(prompts).then(props => {
  37. this.props = props;
  38. });
  39. }
  40. default() {
  41. const currentDirName = path.basename(this.destinationPath());
  42. if (currentDirName !== this.props.name) {
  43. this.log(`
  44. Your project must be inside a folder named ${this.props.name}
  45. I will create this folder for you.
  46. `);
  47. mkdirp(this.props.name);
  48. const pathToProjectDir = this.destinationPath(this.props.name);
  49. this.destinationRoot(pathToProjectDir);
  50. }
  51. }
  52. writing() {
  53. this.copy = copyUtils.generatorCopy(this, templateDir);
  54. this.copyTpl = copyUtils.generatorCopyTpl(
  55. this,
  56. templateDir,
  57. templateFn(this)
  58. );
  59. copyFiles.forEach(this.copy);
  60. copyTemplateFiles.forEach(this.copyTpl);
  61. }
  62. install() {
  63. this.npmInstall(["webpack-defaults", "bluebird"], {
  64. "save-dev": true
  65. }).then(() => {
  66. this.spawnCommand("npm", ["run", "defaults"]);
  67. });
  68. }
  69. };
  70. }
  71. module.exports = webpackGenerator;