|
| 1 | +/** |
| 2 | + * Parse Server Configuration Builder |
| 3 | + * |
| 4 | + * This module builds the definitions file (src/Options/Definitions.js) |
| 5 | + * from the src/Options/index.js options interfaces. |
| 6 | + * The Definitions.js module is responsible for the default values as well |
| 7 | + * as the mappings for the CLI. |
| 8 | + * |
| 9 | + * To rebuild the definitions file, run |
| 10 | + * `$ node resources/buildConfigDefinitions.js` |
| 11 | + */ |
| 12 | +const parsers = require('../src/Options/parsers'); |
| 13 | + |
| 14 | +function last(array) { |
| 15 | + return array[array.length - 1]; |
| 16 | +} |
| 17 | + |
| 18 | +const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' |
| 19 | +function toENV(key) { |
| 20 | + let str = ''; |
| 21 | + let previousIsUpper = false; |
| 22 | + for(let i = 0; i < key.length; i++) { |
| 23 | + const char = key[i]; |
| 24 | + if (letters.indexOf(char) >= 0) { |
| 25 | + if (!previousIsUpper) { |
| 26 | + str += '_'; |
| 27 | + previousIsUpper = true; |
| 28 | + } |
| 29 | + } else { |
| 30 | + previousIsUpper = false; |
| 31 | + } |
| 32 | + str += char; |
| 33 | + } |
| 34 | + return str.toUpperCase(); |
| 35 | +} |
| 36 | + |
| 37 | +function getCommentValue(comment) { |
| 38 | + if (!comment) { return } |
| 39 | + return comment.value.trim(); |
| 40 | +} |
| 41 | + |
| 42 | +function getENVPrefix(iface) { |
| 43 | + if (iface.id.name === 'ParseServerOptions') { |
| 44 | + return 'PARSE_SERVER_'; |
| 45 | + } |
| 46 | + if (iface.id.name === 'CustomPagesOptions') { |
| 47 | + return 'PARSE_SERVER_CUSTOM_PAGES_'; |
| 48 | + } |
| 49 | + if (iface.id.name === 'LiveQueryServerOptions') { |
| 50 | + return 'PARSE_SERVER_LIVE_QUERY_'; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function processProperty(property, iface) { |
| 55 | + const firstComment = getCommentValue(last(property.leadingComments || [])); |
| 56 | + const lastComment = getCommentValue((property.trailingComments || [])[0]); |
| 57 | + const name = property.key.name; |
| 58 | + const prefix = getENVPrefix(iface); |
| 59 | + |
| 60 | + if (!firstComment) { |
| 61 | + return; |
| 62 | + } |
| 63 | + const components = firstComment.split(':ENV:').map((elt) => { |
| 64 | + return elt.trim(); |
| 65 | + }); |
| 66 | + let defaultValue; |
| 67 | + if (lastComment && lastComment.indexOf('=') >= 0) { |
| 68 | + const slice = lastComment.slice(lastComment.indexOf('=') + 1, lastComment.length).trim(); |
| 69 | + defaultValue = slice; |
| 70 | + } |
| 71 | + const help = components[0]; |
| 72 | + const env = components[1] || (prefix + toENV(name)); |
| 73 | + let type = property.value.type; |
| 74 | + let isRequired = true; |
| 75 | + if (type == 'NullableTypeAnnotation') { |
| 76 | + isRequired = false; |
| 77 | + type = property.value.typeAnnotation.type; |
| 78 | + } |
| 79 | + return { |
| 80 | + name, |
| 81 | + env, |
| 82 | + help, |
| 83 | + type, |
| 84 | + defaultValue, |
| 85 | + types: property.value.types, |
| 86 | + typeAnnotation: property.value.typeAnnotation, |
| 87 | + required: isRequired |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +function doInterface(iface) { |
| 93 | + return iface.body.properties |
| 94 | + .map((prop) => processProperty(prop, iface)) |
| 95 | + .filter((e) => e !== undefined); |
| 96 | +} |
| 97 | + |
| 98 | +function mapperFor(elt, t) { |
| 99 | + const p = t.identifier('parsers'); |
| 100 | + const wrap = (identifier) => t.memberExpression(p, identifier); |
| 101 | + |
| 102 | + if (t.isNumberTypeAnnotation(elt)) { |
| 103 | + return t.callExpression(wrap(t.identifier('numberParser')), [t.stringLiteral(elt.name)]); |
| 104 | + } else if (t.isArrayTypeAnnotation(elt)) { |
| 105 | + return wrap(t.identifier('arrayParser')); |
| 106 | + } else if (t.isAnyTypeAnnotation(elt)) { |
| 107 | + return wrap(t.identifier('objectParser')); |
| 108 | + } else if (t.isBooleanTypeAnnotation(elt)) { |
| 109 | + return wrap(t.identifier('booleanParser')); |
| 110 | + } else if (t.isGenericTypeAnnotation(elt)) { |
| 111 | + const type = elt.typeAnnotation.id.name; |
| 112 | + if (type == 'Adapter') { |
| 113 | + return wrap(t.identifier('moduleOrObjectParser')); |
| 114 | + } |
| 115 | + if (type == 'NumberOrBoolean') { |
| 116 | + return wrap(t.identifier('numberOrBooleanParser')); |
| 117 | + } |
| 118 | + return wrap(t.identifier('objectParser')); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function parseDefaultValue(elt, value, t) { |
| 123 | + let litteralValue; |
| 124 | + if (t.isStringTypeAnnotation(elt)) { |
| 125 | + if (value == '""' || value == "''") { |
| 126 | + litteralValue = t.stringLiteral(''); |
| 127 | + } else { |
| 128 | + litteralValue = t.stringLiteral(value); |
| 129 | + } |
| 130 | + } else if (t.isNumberTypeAnnotation(elt)) { |
| 131 | + litteralValue = t.numericLiteral(parsers.numberOrBoolParser('')(value)); |
| 132 | + } else if (t.isArrayTypeAnnotation(elt)) { |
| 133 | + const array = parsers.objectParser(value); |
| 134 | + litteralValue = t.arrayExpression(array.map((value) => { |
| 135 | + if (typeof value == 'string') { |
| 136 | + return t.stringLiteral(value); |
| 137 | + } else { |
| 138 | + throw new Error('Unable to parse array'); |
| 139 | + } |
| 140 | + })); |
| 141 | + } else if (t.isAnyTypeAnnotation(elt)) { |
| 142 | + litteralValue = t.arrayExpression([]); |
| 143 | + } else if (t.isBooleanTypeAnnotation(elt)) { |
| 144 | + litteralValue = t.booleanLiteral(parsers.booleanParser(value)); |
| 145 | + } else if (t.isGenericTypeAnnotation(elt)) { |
| 146 | + const type = elt.typeAnnotation.id.name; |
| 147 | + if (type == 'NumberOrBoolean') { |
| 148 | + litteralValue = t.numericLiteral(parsers.numberOrBoolParser('')(value)); |
| 149 | + } |
| 150 | + if (type == 'CustomPagesOptions') { |
| 151 | + const object = parsers.objectParser(value); |
| 152 | + const props = Object.keys(object).map((key) => { |
| 153 | + return t.objectProperty(key, object[value]); |
| 154 | + }); |
| 155 | + litteralValue = t.objectExpression(props); |
| 156 | + } |
| 157 | + } |
| 158 | + return litteralValue; |
| 159 | +} |
| 160 | + |
| 161 | +function inject(t, list) { |
| 162 | + return list.map((elt) => { |
| 163 | + if (!elt.name) { |
| 164 | + return; |
| 165 | + } |
| 166 | + const props = ['env', 'help'].map((key) => { |
| 167 | + if (elt[key]) { |
| 168 | + return t.objectProperty(t.stringLiteral(key), t.stringLiteral(elt[key])); |
| 169 | + } |
| 170 | + }).filter((e) => e !== undefined); |
| 171 | + if (elt.required) { |
| 172 | + props.push(t.objectProperty(t.stringLiteral('required'), t.booleanLiteral(true))) |
| 173 | + } |
| 174 | + const action = mapperFor(elt, t); |
| 175 | + if (action) { |
| 176 | + props.push(t.objectProperty(t.stringLiteral('action'), action)) |
| 177 | + } |
| 178 | + if (elt.defaultValue) { |
| 179 | + const parsedValue = parseDefaultValue(elt, elt.defaultValue, t); |
| 180 | + if (parsedValue) { |
| 181 | + props.push(t.objectProperty(t.stringLiteral('default'), parsedValue)); |
| 182 | + } else { |
| 183 | + throw new Error(`Unable to parse value for ${elt.name} `); |
| 184 | + } |
| 185 | + } |
| 186 | + const obj = t.objectExpression(props); |
| 187 | + return t.objectProperty(t.stringLiteral(elt.name), obj); |
| 188 | + }).filter((elt) => { |
| 189 | + return elt != undefined; |
| 190 | + }); |
| 191 | +} |
| 192 | + |
| 193 | +const makeRequire = function(variableName, module, t) { |
| 194 | + const decl = t.variableDeclarator(t.identifier(variableName), t.callExpression(t.identifier('require'), [t.stringLiteral(module)])); |
| 195 | + return t.variableDeclaration('var', [decl]) |
| 196 | +} |
| 197 | + |
| 198 | +const plugin = function (babel) { |
| 199 | + const t = babel.types; |
| 200 | + const moduleExports = t.memberExpression(t.identifier('module'), t.identifier('exports')); |
| 201 | + return { |
| 202 | + visitor: { |
| 203 | + Program: function(path) { |
| 204 | + // Inject the parser's loader |
| 205 | + path.unshiftContainer('body', makeRequire('parsers', './parsers', t)); |
| 206 | + }, |
| 207 | + ExportDeclaration: function(path) { |
| 208 | + // Export declaration on an interface |
| 209 | + if (path.node && path.node.declaration && path.node.declaration.type == 'InterfaceDeclaration') { |
| 210 | + const l = inject(t, doInterface(path.node.declaration)); |
| 211 | + const id = path.node.declaration.id.name; |
| 212 | + const exports = t.memberExpression(moduleExports, t.identifier(id)); |
| 213 | + path.replaceWith( |
| 214 | + t.assignmentExpression('=', exports, t.objectExpression(l)) |
| 215 | + ) |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | +}; |
| 221 | + |
| 222 | +const auxiliaryCommentBefore = ` |
| 223 | +**** GENERATED CODE **** |
| 224 | +This code has been generated by resources/buildConfigDefinitions.js |
| 225 | +Do not edit manually, but update Options/index.js |
| 226 | +` |
| 227 | + |
| 228 | +const babel = require("babel-core"); |
| 229 | +const res = babel.transformFileSync('./src/Options/index.js', { plugins: [ plugin ], auxiliaryCommentBefore }); |
| 230 | +require('fs').writeFileSync('./src/Options/Definitions.js', res.code + '\n'); |
0 commit comments