|
| 1 | +import fs from 'fs'; |
| 2 | +import handlebars from 'handlebars'; |
| 3 | +import mkdirp from 'mkdirp'; |
| 4 | +import { sprintf } from 'sprintf-js'; |
| 5 | +import urlapi from 'url'; |
| 6 | + |
| 7 | +export default class { |
| 8 | + templates = {}; |
| 9 | + |
| 10 | + constructor({hydraPrefix, templateDirectory}) { |
| 11 | + this.hydraPrefix = hydraPrefix; |
| 12 | + this.templateDirectory = templateDirectory; |
| 13 | + |
| 14 | + this.registerTemplates('', ['_entrypoint.js']); |
| 15 | + } |
| 16 | + |
| 17 | + registerTemplates(basePath, paths) { |
| 18 | + for (let path of paths) { |
| 19 | + this.templates[path] = handlebars.compile(fs.readFileSync(`${this.templateDirectory}/${basePath}${path}`).toString()); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + createDir(dir, warn = true) { |
| 24 | + if (!fs.existsSync(dir)) { |
| 25 | + mkdirp.sync(dir); |
| 26 | + |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (warn) console.log(`The directory "${dir}" already exists`); |
| 31 | + } |
| 32 | + |
| 33 | + createFileFromPattern(pattern, dir, lc, context) { |
| 34 | + this.createFile(sprintf(pattern, 'foo'), sprintf(`${dir}/${pattern}`, lc), context); |
| 35 | + } |
| 36 | + |
| 37 | + createFile(template, dest, context = {}, warn = true) { |
| 38 | + if (!fs.existsSync(dest)) { |
| 39 | + fs.writeFileSync(dest, this.templates[template](context)); |
| 40 | + |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (warn) console.log(`The file "${dest}" already exists`); |
| 45 | + } |
| 46 | + |
| 47 | + createEntrypoint(apiEntry, dest) { |
| 48 | + const url = urlapi.parse(apiEntry); |
| 49 | + const {protocol, host, port, pathname} = url; |
| 50 | + const hostUrl = `${protocol}//${host}${port ? `:${port}` : ''}`; |
| 51 | + |
| 52 | + this.createFile('_entrypoint.js', dest, {host: hostUrl, path: pathname}, false); |
| 53 | + } |
| 54 | + |
| 55 | + getHtmlInputTypeFromField(field) { |
| 56 | + switch (field.id) { |
| 57 | + case 'http://schema.org/email': |
| 58 | + return {type: 'email'}; |
| 59 | + |
| 60 | + case 'http://schema.org/url': |
| 61 | + return {type: 'url'}; |
| 62 | + } |
| 63 | + |
| 64 | + switch (field.range) { |
| 65 | + case 'http://www.w3.org/2001/XMLSchema#integer': |
| 66 | + return {type: 'number'}; |
| 67 | + |
| 68 | + case 'http://www.w3.org/2001/XMLSchema#decimal': |
| 69 | + return {type: 'number', step: '0.1'}; |
| 70 | + |
| 71 | + case 'http://www.w3.org/2001/XMLSchema#boolean': |
| 72 | + return {type: 'checkbox'}; |
| 73 | + |
| 74 | + case 'http://www.w3.org/2001/XMLSchema#date': |
| 75 | + return {type: 'date'}; |
| 76 | + |
| 77 | + case 'http://www.w3.org/2001/XMLSchema#time': |
| 78 | + return {type: 'time'}; |
| 79 | + |
| 80 | + default: |
| 81 | + return {type: 'text'}; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + buildFields(apiFields) { |
| 86 | + let fields = []; |
| 87 | + for (let apiField of apiFields) { |
| 88 | + let field = this.getHtmlInputTypeFromField(apiField); |
| 89 | + field.required = apiField.required; |
| 90 | + field.name = apiField.name; |
| 91 | + field.description = apiField.description.replace(/"/g, "'"); // fix for Form placeholder description |
| 92 | + |
| 93 | + fields.push(field) |
| 94 | + } |
| 95 | + |
| 96 | + return fields; |
| 97 | + } |
| 98 | +} |
0 commit comments