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