|
| 1 | +import BaseGenerator from "./BaseGenerator.js"; |
| 2 | +import handlebars from "handlebars"; |
| 3 | +import hbhComparison from "handlebars-helpers/lib/comparison.js"; |
| 4 | +import hbhString from "handlebars-helpers/lib/string.js"; |
| 5 | + |
| 6 | +export default class extends BaseGenerator { |
| 7 | + constructor(params) { |
| 8 | + super(params); |
| 9 | + |
| 10 | + this.registerTemplates("angular/", [ |
| 11 | + // COMMON COMPONENTS |
| 12 | + "app/components/common/delete/delete.component.html", |
| 13 | + "app/components/common/delete/delete.component.ts", |
| 14 | + "app/components/common/form/form.component.html", |
| 15 | + "app/components/common/form/form.component.ts", |
| 16 | + "app/components/common/header/header.component.html", |
| 17 | + "app/components/common/header/header.component.ts", |
| 18 | + "app/components/common/sidebar/sidebar.component.html", |
| 19 | + "app/components/common/sidebar/sidebar.component.ts", |
| 20 | + "app/components/common/table/table.component.html", |
| 21 | + "app/components/common/table/table.component.ts", |
| 22 | + |
| 23 | + // COMPONENTS |
| 24 | + "app/components/foo/create/create.component.html", |
| 25 | + "app/components/foo/create/create.component.ts", |
| 26 | + "app/components/foo/edit/edit.component.html", |
| 27 | + "app/components/foo/edit/edit.component.ts", |
| 28 | + "app/components/foo/list/list.component.html", |
| 29 | + "app/components/foo/list/list.component.ts", |
| 30 | + "app/components/foo/show/show.component.html", |
| 31 | + "app/components/foo/show/show.component.ts", |
| 32 | + "app/app.component.html", |
| 33 | + "app/app.component.ts", |
| 34 | + |
| 35 | + //SVG COMPONENT |
| 36 | + "app/components/svg/list-svg/list-svg.component.svg", |
| 37 | + "app/components/svg/list-svg/list-svg.component.ts", |
| 38 | + "app/components/svg/menu/menu.component.svg", |
| 39 | + "app/components/svg/menu/menu.component.ts", |
| 40 | + |
| 41 | + //INTERFACE |
| 42 | + "app/interface/api.ts", |
| 43 | + "app/interface/foo.model.ts", |
| 44 | + "app/interface/hero.model.ts", |
| 45 | + "app/interface/list.model.ts", |
| 46 | + "app/interface/show.model.ts", |
| 47 | + "app/interface/update.model.ts", |
| 48 | + |
| 49 | + // ROUTER |
| 50 | + "app/router/foo.ts", |
| 51 | + "app/app.routes.ts", |
| 52 | + |
| 53 | + //SERVICE |
| 54 | + "app/service/hero.service.ts", |
| 55 | + ]); |
| 56 | + |
| 57 | + handlebars.registerHelper("compare", hbhComparison.compare); |
| 58 | + handlebars.registerHelper("lowercase", hbhString.lowercase); |
| 59 | + } |
| 60 | + |
| 61 | + generate(api, resource, dir) { |
| 62 | + const lc = resource.title.toLowerCase(); |
| 63 | + const titleUcFirst = |
| 64 | + resource.title.charAt(0).toUpperCase() + resource.title.slice(1); |
| 65 | + const fields = this.parseFields(resource); |
| 66 | + const hasIsRelation = fields.some((field) => field.isRelation); |
| 67 | + const hasIsRelations = fields.some((field) => field.isRelations); |
| 68 | + const hasDateField = fields.some((field) => field.type === "dateTime"); |
| 69 | + |
| 70 | + const context = { |
| 71 | + title: resource.title, |
| 72 | + name: resource.name, |
| 73 | + lc, |
| 74 | + uc: resource.title.toUpperCase(), |
| 75 | + fields, |
| 76 | + formFields: this.buildFields(fields), |
| 77 | + hydraPrefix: this.hydraPrefix, |
| 78 | + titleUcFirst, |
| 79 | + hasIsRelation, |
| 80 | + hasIsRelations, |
| 81 | + hasRelations: hasIsRelation || hasIsRelations, |
| 82 | + hasDateField, |
| 83 | + }; |
| 84 | + |
| 85 | + //CREATE DIRECTORIES - These directories may already exist |
| 86 | + [ |
| 87 | + `${dir}/assets`, |
| 88 | + `${dir}/utils`, |
| 89 | + `${dir}/app/components/${lc}`, |
| 90 | + `${dir}/app/components/common`, |
| 91 | + `${dir}/app/components/svg`, |
| 92 | + `${dir}/app/interface`, |
| 93 | + `${dir}/app/router`, |
| 94 | + `${dir}/app/service`, |
| 95 | + ].forEach((dir) => this.createDir(dir, false)); |
| 96 | + |
| 97 | + //CREATE FILE |
| 98 | + [ |
| 99 | + `${dir}/app/components/svg/list-svg/list-svg.component.svg`, |
| 100 | + `${dir}/app/components/svg/list-svg/list-svg.component.ts`, |
| 101 | + `${dir}/app/components/svg/menu/menu.component.svg`, |
| 102 | + `${dir}/app/components/svg/menu/menu.component.ts`, |
| 103 | + `${dir}/app/components/common/delete/delete.component.html`, |
| 104 | + `${dir}/app/components/common/delete/delete.component.ts`, |
| 105 | + `${dir}/app/components/common/form/form.component.html`, |
| 106 | + `${dir}/app/components/common/form/form.component.ts`, |
| 107 | + `${dir}/app/components/common/header/header.component.html`, |
| 108 | + `${dir}/app/components/common/header/header.component.ts`, |
| 109 | + `${dir}/app/components/common/sidebar/sidebar.component.html`, |
| 110 | + `${dir}/app/components/common/sidebar/sidebar.component.ts`, |
| 111 | + `${dir}/app/components/common/table/table.component.html`, |
| 112 | + `${dir}/app/app.component.html`, |
| 113 | + `${dir}/app/app.component.ts`, |
| 114 | + `${dir}/app/app.routes.ts`, |
| 115 | + ].forEach((file) => this.createFile(file, file, context, false)); |
| 116 | + |
| 117 | + [ |
| 118 | + `app/components/%s/create/create.component.html", |
| 119 | + "app/components/%s/create/create.component.ts", |
| 120 | + "app/components/%s/edit/edit.component.html", |
| 121 | + "app/components/%s/edit/edit.component.ts", |
| 122 | + "app/components/%s/list/list.component.html", |
| 123 | + "app/components/%s/list/list.component.ts", |
| 124 | + "app/components/%s/show/show.component.html", |
| 125 | + "app/components/%s/show/show.component.ts",`, |
| 126 | + ].forEach((file) => this.createFileFromPattern(file, dir, [lc], context)); |
| 127 | + } |
| 128 | + |
| 129 | + parseFields(resource) { |
| 130 | + const fields = [ |
| 131 | + ...resource.writableFields, |
| 132 | + ...resource.readableFields, |
| 133 | + ].reduce((list, field) => { |
| 134 | + if (list[field.name]) { |
| 135 | + return list; |
| 136 | + } |
| 137 | + |
| 138 | + const isReferences = Boolean( |
| 139 | + field.reference && field.maxCardinality !== 1 |
| 140 | + ); |
| 141 | + const isEmbeddeds = Boolean(field.embedded && field.maxCardinality !== 1); |
| 142 | + |
| 143 | + return { |
| 144 | + ...list, |
| 145 | + [field.name]: { |
| 146 | + ...field, |
| 147 | + isReferences, |
| 148 | + isEmbeddeds, |
| 149 | + isRelation: field.reference || field.embedded, |
| 150 | + isRelations: isEmbeddeds || isReferences, |
| 151 | + }, |
| 152 | + }; |
| 153 | + }, {}); |
| 154 | + |
| 155 | + return Object.values(fields); |
| 156 | + } |
| 157 | +} |
0 commit comments