|
| 1 | +import mkdirp from 'mkdirp'; |
| 2 | +import handlebars from 'handlebars'; |
| 3 | +import fs from 'fs'; |
| 4 | +import urlapi from 'url'; |
| 5 | +import chalk from 'chalk'; |
| 6 | + |
| 7 | +export default class VueCrudGenerator { |
| 8 | + templates = {}; |
| 9 | + |
| 10 | + constructor({hydraPrefix, templateDirectory}) { |
| 11 | + const templatePath = `${templateDirectory}/vue/`; |
| 12 | + |
| 13 | + this.hydraPrefix = hydraPrefix; |
| 14 | + |
| 15 | + // modules |
| 16 | + this.registerTemplate(templatePath, 'store/modules/foo/index.js'); |
| 17 | + this.registerTemplate(templatePath, 'store/modules/foo/create.js'); |
| 18 | + this.registerTemplate(templatePath, 'store/modules/foo/delete.js'); |
| 19 | + this.registerTemplate(templatePath, 'store/modules/foo/list.js'); |
| 20 | + this.registerTemplate(templatePath, 'store/modules/foo/update.js'); |
| 21 | + this.registerTemplate(templatePath, 'store/modules/foo/show.js'); |
| 22 | + this.registerTemplate(templatePath, 'store/modules/foo/mutation-types.js'); |
| 23 | + |
| 24 | + // components |
| 25 | + this.registerTemplate(templatePath, 'components/foo/Create.vue'); |
| 26 | + this.registerTemplate(templatePath, 'components/foo/Form.vue'); |
| 27 | + this.registerTemplate(templatePath, 'components/foo/List.vue'); |
| 28 | + this.registerTemplate(templatePath, 'components/foo/Update.vue'); |
| 29 | + this.registerTemplate(templatePath, 'components/foo/Show.vue'); |
| 30 | + |
| 31 | + // routes |
| 32 | + this.registerTemplate(templatePath, 'routes/foo.js'); |
| 33 | + |
| 34 | + // entrypoint |
| 35 | + this.registerTemplate(templatePath, 'config/_entrypoint.js'); |
| 36 | + |
| 37 | + // utils |
| 38 | + this.registerTemplate(templatePath, 'utils/fetch.js'); |
| 39 | + } |
| 40 | + |
| 41 | + registerTemplate(templatePath, path) { |
| 42 | + this.templates[path] = handlebars.compile(fs.readFileSync(templatePath+path).toString()); |
| 43 | + } |
| 44 | + |
| 45 | + help(resource) { |
| 46 | + const titleLc = resource.title.toLowerCase() |
| 47 | + |
| 48 | + console.log('Code for the "%s" resource type has been generated!', resource.title); |
| 49 | + console.log('Paste the following definitions in your application configuration:'); |
| 50 | + console.log(chalk.green(` |
| 51 | +//import routes |
| 52 | +import ${titleLc}Routes from './routes/${titleLc}'; |
| 53 | +
|
| 54 | +// Add routes to VueRouter |
| 55 | +const router = new VueRouter({ |
| 56 | + // ... |
| 57 | + routes: [ |
| 58 | + ...{ ${titleLc}Routes }, |
| 59 | + ] |
| 60 | +}); |
| 61 | +
|
| 62 | +// Add the modules in the store |
| 63 | +import { ${titleLc} from './store/modules/{ ${titleLc}/'; |
| 64 | +
|
| 65 | +export const store = new Vuex.Store({ |
| 66 | + // ... |
| 67 | + modules: { |
| 68 | + { ${titleLc} |
| 69 | + } |
| 70 | +}); |
| 71 | +`)); |
| 72 | + } |
| 73 | + |
| 74 | + generate(api, resource, dir) { |
| 75 | + const lc = resource.title.toLowerCase(); |
| 76 | + const titleUcFirst = resource.title.charAt(0).toUpperCase() + resource.title.slice(1); |
| 77 | + |
| 78 | + const context = { |
| 79 | + title: resource.title, |
| 80 | + name: resource.name, |
| 81 | + lc, |
| 82 | + uc: resource.title.toUpperCase(), |
| 83 | + fields: resource.readableFields, |
| 84 | + formFields: this.buildFields(resource.writableFields), |
| 85 | + hydraPrefix: this.hydraPrefix, |
| 86 | + titleUcFirst |
| 87 | + }; |
| 88 | + |
| 89 | + |
| 90 | + // Create directories |
| 91 | + // These directories may already exist |
| 92 | + mkdirp.sync(`${dir}/config`); |
| 93 | + mkdirp.sync(`${dir}/routes`); |
| 94 | + mkdirp.sync(`${dir}/utils`); |
| 95 | + |
| 96 | + this.createDir(`${dir}/store/modules/${lc}`); |
| 97 | + this.createDir(`${dir}/components/${lc}`); |
| 98 | + |
| 99 | + // modules |
| 100 | + this.createFile('store/modules/foo/index.js', `${dir}/store/modules/${lc}/index.js`, context); |
| 101 | + this.createFile('store/modules/foo/create.js', `${dir}/store/modules/${lc}/create.js`, context); |
| 102 | + this.createFile('store/modules/foo/delete.js', `${dir}/store/modules/${lc}/delete.js`, context); |
| 103 | + this.createFile('store/modules/foo/list.js', `${dir}/store/modules/${lc}/list.js`, context); |
| 104 | + this.createFile('store/modules/foo/update.js', `${dir}/store/modules/${lc}/update.js`, context); |
| 105 | + this.createFile('store/modules/foo/show.js', `${dir}/store/modules/${lc}/show.js`, context); |
| 106 | + this.createFile('store/modules/foo/mutation-types.js', `${dir}/store/modules/${lc}/mutation-types.js`, context); |
| 107 | + |
| 108 | + // components |
| 109 | + this.createFile('components/foo/Create.vue', `${dir}/components/${lc}/Create.vue`, context); |
| 110 | + this.createFile('components/foo/Form.vue', `${dir}/components/${lc}/Form.vue`, context); |
| 111 | + this.createFile('components/foo/List.vue', `${dir}/components/${lc}/List.vue`, context); |
| 112 | + this.createFile('components/foo/Update.vue', `${dir}/components/${lc}/Update.vue`, context); |
| 113 | + this.createFile('components/foo/Show.vue', `${dir}/components/${lc}/Show.vue`, context); |
| 114 | + |
| 115 | + // config |
| 116 | + this.createFile('config/_entrypoint.js', `${dir}/config/_entrypoint.js`, context); |
| 117 | + |
| 118 | + // routes |
| 119 | + this.createFile('routes/foo.js', `${dir}/routes/${lc}.js`, context); |
| 120 | + } |
| 121 | + |
| 122 | + entrypoint(apiEntry, dir) { |
| 123 | + const url = urlapi.parse(apiEntry); |
| 124 | + const {protocol, host, port, pathname} = url; |
| 125 | + const hostUrl = `${protocol}//${host}${port ? `:${port}` : ''}`; |
| 126 | + |
| 127 | + const context = { |
| 128 | + host: hostUrl, |
| 129 | + path: pathname |
| 130 | + } |
| 131 | + |
| 132 | + this.createFile('config/_entrypoint.js', `${dir}/config/_entrypoint.js`, context); |
| 133 | + } |
| 134 | + |
| 135 | + utils(dir) { |
| 136 | + const context = { |
| 137 | + hydraPrefix: this.hydraPrefix |
| 138 | + } |
| 139 | + |
| 140 | + this.createFile('utils/fetch.js', `${dir}/utils/fetch.js`, context); |
| 141 | + } |
| 142 | + |
| 143 | + getInputTypeFromField(field) { |
| 144 | + switch (field.id) { |
| 145 | + case 'http://schema.org/email': |
| 146 | + return {type: 'email'}; |
| 147 | + |
| 148 | + case 'http://schema.org/url': |
| 149 | + return {type: 'url'}; |
| 150 | + } |
| 151 | + |
| 152 | + switch (field.range) { |
| 153 | + case 'http://www.w3.org/2001/XMLSchema#integer': |
| 154 | + return {type: 'number'}; |
| 155 | + |
| 156 | + case 'http://www.w3.org/2001/XMLSchema#decimal': |
| 157 | + return {type: 'number', step: '0.1'}; |
| 158 | + |
| 159 | + case 'http://www.w3.org/2001/XMLSchema#boolean': |
| 160 | + return {type: 'checkbox'}; |
| 161 | + |
| 162 | + case 'http://www.w3.org/2001/XMLSchema#date': |
| 163 | + return {type: 'date'}; |
| 164 | + |
| 165 | + case 'http://www.w3.org/2001/XMLSchema#time': |
| 166 | + return {type: 'time'}; |
| 167 | + |
| 168 | + default: |
| 169 | + return {type: 'text'}; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + buildFields(apiFields) { |
| 174 | + let fields = []; |
| 175 | + for (let apiField of apiFields) { |
| 176 | + let field = this.getInputTypeFromField(apiField); |
| 177 | + field.required = apiField.required; |
| 178 | + field.name = apiField.name; |
| 179 | + field.description = apiField.description.replace(/"/g, "'"); // fix for Form placeholder description |
| 180 | + |
| 181 | + fields.push(field) |
| 182 | + } |
| 183 | + |
| 184 | + return fields; |
| 185 | + } |
| 186 | + |
| 187 | + createDir(dir) { |
| 188 | + if (fs.existsSync(dir)) throw new Error(`The directory "${dir}" already exists`); |
| 189 | + mkdirp.sync(dir); |
| 190 | + } |
| 191 | + |
| 192 | + createFile(template, dest, context) { |
| 193 | + fs.writeFileSync(dest, this.templates[template](context)); |
| 194 | + } |
| 195 | +} |
0 commit comments