|
| 1 | +import camelCase from "lodash/fp/camelCase"; |
| 2 | +import toUpper from "lodash/fp/toUpper"; |
| 3 | +import Generator from "yeoman-generator"; |
| 4 | + |
| 5 | +import { nameQuestion } from "../../src/questions"; |
| 6 | + |
| 7 | +interface FileConfig { |
| 8 | + templateName: string; |
| 9 | + fileNameSuffix?: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface Config { |
| 13 | + styling: "jss" | "styled-components" | "emotion"; |
| 14 | + rootDir: string; |
| 15 | +} |
| 16 | + |
| 17 | +export = class StructureApiGenerator extends Generator { |
| 18 | + private name: string; |
| 19 | + private module: string; |
| 20 | + private fileConfigs: FileConfig[]; |
| 21 | + |
| 22 | + public async prompting() { |
| 23 | + const { name } = await this.prompt(nameQuestion("Page", this.options.name)); |
| 24 | + |
| 25 | + this.name = this.options.name || name; |
| 26 | + } |
| 27 | + |
| 28 | + public makeModule() { |
| 29 | + this.module = this.options.module || this.name; |
| 30 | + } |
| 31 | + |
| 32 | + public makeConfig() { |
| 33 | + this.fileConfigs = this.getFileConfig(this.config.getAll() as Config); |
| 34 | + } |
| 35 | + |
| 36 | + public writing() { |
| 37 | + this.fileConfigs.forEach(fileConfig => { |
| 38 | + this.fs.copyTpl( |
| 39 | + this.templatePath(`${fileConfig.templateName}`), |
| 40 | + this.destinationPath( |
| 41 | + `./${this.config.get("rootDir")}/${this.module}/page/${ |
| 42 | + this.name |
| 43 | + }Page/${this.name}Page.${fileConfig.fileNameSuffix}` |
| 44 | + ), |
| 45 | + { |
| 46 | + name: this.name, |
| 47 | + nameCamelCase: camelCase(this.name), |
| 48 | + nameUpperCase: toUpper(this.name) |
| 49 | + } |
| 50 | + ); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private getFileConfig = (config: Config): FileConfig[] => [ |
| 55 | + { |
| 56 | + templateName: `main.tsx`, |
| 57 | + fileNameSuffix: "tsx" |
| 58 | + }, |
| 59 | + { |
| 60 | + templateName: `load.tsx`, |
| 61 | + fileNameSuffix: "load.tsx" |
| 62 | + }, |
| 63 | + { |
| 64 | + templateName: `style.${config.styling}.ts`, |
| 65 | + fileNameSuffix: "style.ts" |
| 66 | + }, |
| 67 | + { |
| 68 | + templateName: `spec.tsx`, |
| 69 | + fileNameSuffix: "spec.tsx" |
| 70 | + } |
| 71 | + ]; |
| 72 | +}; |
0 commit comments