|
| 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 | +export = class StructureApiGenerator extends Generator { |
| 13 | + private name: string; |
| 14 | + private module: string; |
| 15 | + private fileConfigs: FileConfig[]; |
| 16 | + |
| 17 | + public async prompting() { |
| 18 | + const { name } = await this.prompt( |
| 19 | + nameQuestion("Router", this.options.name) |
| 20 | + ); |
| 21 | + |
| 22 | + this.name = this.options.name || name; |
| 23 | + } |
| 24 | + |
| 25 | + public makeModule() { |
| 26 | + this.module = this.options.module || this.name; |
| 27 | + } |
| 28 | + |
| 29 | + public makeConfig() { |
| 30 | + this.fileConfigs = this.getFileConfig(this.name); |
| 31 | + } |
| 32 | + |
| 33 | + public writing() { |
| 34 | + this.fileConfigs.forEach(fileConfig => { |
| 35 | + this.fs.copyTpl( |
| 36 | + this.templatePath(`${fileConfig.templateName}`), |
| 37 | + this.destinationPath( |
| 38 | + `./${this.config.get("rootDir")}/${this.module}/router/${this.name}${ |
| 39 | + fileConfig.fileNameSuffix |
| 40 | + }` |
| 41 | + ), |
| 42 | + { |
| 43 | + name: this.name, |
| 44 | + nameCamelCase: camelCase(this.name), |
| 45 | + nameUpperCase: toUpper(this.name) |
| 46 | + } |
| 47 | + ); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + private getFileConfig = (name: string): FileConfig[] => [ |
| 52 | + { |
| 53 | + templateName: `main.tsx`, |
| 54 | + fileNameSuffix: "Router.tsx" |
| 55 | + }, |
| 56 | + { |
| 57 | + templateName: `route.ts`, |
| 58 | + fileNameSuffix: ".route.ts" |
| 59 | + }, |
| 60 | + { |
| 61 | + templateName: `data.tsx`, |
| 62 | + fileNameSuffix: "Router.data.tsx" |
| 63 | + }, |
| 64 | + { |
| 65 | + templateName: `load.tsx`, |
| 66 | + fileNameSuffix: "Router.load.tsx" |
| 67 | + }, |
| 68 | + { |
| 69 | + templateName: `spec.tsx`, |
| 70 | + fileNameSuffix: "Router.spec.tsx" |
| 71 | + } |
| 72 | + ]; |
| 73 | +}; |
0 commit comments