UNPKG

@api-platform/client-generator

Version:

Generate apps built with Next, Nuxt, Quasar, React, React Native, Vue or Vuetify for any API documented using Hydra or OpenAPI

238 lines (196 loc) 9.03 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } import chalk from "chalk"; import fs from "fs"; import handlebars from "handlebars"; import mkdirp from "mkdirp"; import { sprintf } from "sprintf-js"; import prettier from "prettier"; var _default = /*#__PURE__*/function () { function _default(_ref) { var hydraPrefix = _ref.hydraPrefix, templateDirectory = _ref.templateDirectory; _classCallCheck(this, _default); _defineProperty(this, "templates", {}); this.hydraPrefix = hydraPrefix; this.templateDirectory = templateDirectory; this.registerTemplates("", ["entrypoint.js"]); } _createClass(_default, [{ key: "registerTemplates", value: function registerTemplates(basePath, paths) { var _iterator = _createForOfIteratorHelper(paths), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var path = _step.value; this.templates[path] = handlebars.compile(fs.readFileSync("".concat(this.templateDirectory, "/").concat(basePath).concat(path)).toString()); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } } }, { key: "createDir", value: function createDir(dir) { var warn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!fs.existsSync(dir)) { mkdirp.sync(dir); return; } if (warn) { console.log(chalk.yellow("The directory \"".concat(dir, "\" already exists"))); } } }, { key: "createFileFromPattern", value: function createFileFromPattern(pattern, dir, value, context) { var templateValue = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : "foo"; this.createFile(sprintf(pattern, templateValue), sprintf("".concat(dir, "/").concat(pattern), value), context); } }, { key: "createFile", value: function createFile(template, dest) { var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var warn = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true; if (undefined === this.templates[template]) { console.log("The template ".concat(template, " does not exists in the registered templates.")); return; } // Format the generated code using Prettier var content = this.templates[template](context); if (template.endsWith(".js")) { content = prettier.format(content, { parser: "babel" }); } else if (template.endsWith(".ts") || template.endsWith(".tsx")) { content = prettier.format(content, { parser: "babel-ts" }); } if (!fs.existsSync(dest)) { fs.writeFileSync(dest, content); return; } if (warn) console.log("The file \"".concat(dest, "\" already exists")); } }, { key: "createEntrypoint", value: function createEntrypoint(entrypoint, dest) { this.createFile("entrypoint.js", dest, { entrypoint: entrypoint }, false); } // eslint-disable-next-line no-unused-vars }, { key: "checkDependencies", value: function checkDependencies(dir) {} }, { key: "getTargetDependencies", value: function getTargetDependencies(dir) { var packageFilePath = "".concat(dir, "/package.json"); var packageFile; var dependencies = []; try { if (!fs.existsSync(packageFilePath)) { throw new Error(); } packageFile = fs.readFileSync(packageFilePath); var configuration = JSON.parse(packageFile.toString()); dependencies = Object.keys(_objectSpread(_objectSpread({}, configuration.dependencies), configuration.devDependencies)); } catch (e) { console.log(chalk.yellow("There's no readable package file in the target directory. Generator can't check dependencies.")); } return dependencies; } }, { key: "getHtmlInputTypeFromField", value: function getHtmlInputTypeFromField(field) { switch (field.id) { case "http://schema.org/email": return { type: "email" }; case "http://schema.org/url": return { type: "url" }; } switch (field.range) { case "http://www.w3.org/2001/XMLSchema#integer": return { type: "number", number: true }; case "http://www.w3.org/2001/XMLSchema#decimal": return { type: "number", step: "0.1", number: true }; case "http://www.w3.org/2001/XMLSchema#boolean": return { type: "checkbox" }; case "http://www.w3.org/2001/XMLSchema#date": return { type: "date" }; case "http://www.w3.org/2001/XMLSchema#time": return { type: "time" }; case "http://www.w3.org/2001/XMLSchema#dateTime": return { type: "dateTime" }; default: return { type: "text" }; } } }, { key: "getType", value: function getType(field) { if (field.reference) { if (field.maxCardinality !== 1) { return "string[]"; } return "string"; } switch (field.range) { case "http://www.w3.org/2001/XMLSchema#integer": case "http://www.w3.org/2001/XMLSchema#decimal": return "number"; case "http://www.w3.org/2001/XMLSchema#boolean": return "boolean"; case "http://www.w3.org/2001/XMLSchema#date": case "http://www.w3.org/2001/XMLSchema#dateTime": case "http://www.w3.org/2001/XMLSchema#time": return "Date"; case "http://www.w3.org/2001/XMLSchema#string": return "string"; } return "any"; } }, { key: "buildFields", value: function buildFields(fields) { var _this = this; return fields.map(function (field) { return _objectSpread(_objectSpread(_objectSpread({}, field), _this.getHtmlInputTypeFromField(field)), {}, { description: field.description.replace(/"/g, "'") // fix for Form placeholder description }); }); } }]); return _default; }(); export { _default as default };