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