@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
100 lines (82 loc) • 5.08 kB
JavaScript
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
import chalk from "chalk";
import handlebars from "handlebars";
import BaseGenerator from "./BaseGenerator.js";
var _default = /*#__PURE__*/function (_BaseGenerator) {
_inherits(_default, _BaseGenerator);
var _super = _createSuper(_default);
function _default(params) {
var _this;
_classCallCheck(this, _default);
_this = _super.call(this, params);
handlebars.registerHelper("ifNotResource", function (item, options) {
if (item === null) {
return options.fn(this);
}
return options.inverse(this);
});
_this.registerTemplates("common/", [// utils
"utils/mercure.js"]);
_this.registerTemplates("react-common/", [// actions
"actions/foo/create.js", "actions/foo/delete.js", "actions/foo/list.js", "actions/foo/update.js", "actions/foo/show.js", // utils
"utils/dataAccess.js", // reducers
"reducers/foo/create.js", "reducers/foo/delete.js", "reducers/foo/index.js", "reducers/foo/list.js", "reducers/foo/update.js", "reducers/foo/show.js"]);
_this.registerTemplates("react-native/", [// components
"components/foo/Create.js", "components/foo/Form.js", "components/foo/index.js", "components/foo/List.js", "components/foo/Update.js", "components/foo/Show.js", "components/Spinner.js", "components/Confirm.js", // routes
"routes/foo.js", // helpers
"utils/helpers.js"]);
return _this;
}
_createClass(_default, [{
key: "help",
value: function help(resource) {
var titleLc = resource.title.toLowerCase();
console.log('Code for the "%s" resource type has been generated!', resource.title);
console.log("Paste the following definitions in your application configuration (`client/src/index.js` by default):");
console.log(chalk.green("\n// import reducers\nimport ".concat(titleLc, " from './reducers/").concat(titleLc, "/';\n\n// Add the reducer\ncombineReducers({ ").concat(titleLc, ", /* ... */ }),\n")));
}
}, {
key: "generate",
value: function generate(api, resource, dir) {
var _this2 = this;
var lc = resource.title.toLowerCase();
var titleUcFirst = resource.title.charAt(0).toUpperCase() + resource.title.slice(1);
var context = {
title: resource.title,
name: resource.name,
lc: lc,
uc: resource.title.toUpperCase(),
fields: resource.readableFields,
formFields: this.buildFields(resource.writableFields),
hydraPrefix: this.hydraPrefix,
titleUcFirst: titleUcFirst
}; // Create directories
// These directories may already exist
["".concat(dir, "/utils"), "".concat(dir, "/config"), "".concat(dir, "/routes")].forEach(function (dir) {
return _this2.createDir(dir, false);
});
["".concat(dir, "/actions/").concat(lc), "".concat(dir, "/components/").concat(lc), "".concat(dir, "/reducers/").concat(lc)].forEach(function (dir) {
return _this2.createDir(dir);
});
[// actions
"actions/%s/create.js", "actions/%s/delete.js", "actions/%s/list.js", "actions/%s/update.js", "actions/%s/show.js", // components
"components/%s/Create.js", "components/%s/Form.js", "components/%s/index.js", "components/%s/List.js", "components/%s/Update.js", "components/%s/Show.js", // reducers
"reducers/%s/create.js", "reducers/%s/delete.js", "reducers/%s/index.js", "reducers/%s/list.js", "reducers/%s/update.js", "reducers/%s/show.js", // routes
"routes/%s.js"].forEach(function (pattern) {
return _this2.createFileFromPattern(pattern, dir, lc, context);
});
["utils/dataAccess.js", "utils/mercure.js", "utils/helpers.js", "components/Spinner.js", "components/Confirm.js"].forEach(function (file) {
return _this2.createFile(file, "".concat(dir, "/").concat(file));
});
this.createEntrypoint(api.entrypoint, "".concat(dir, "/config/entrypoint.js"));
}
}]);
return _default;
}(BaseGenerator);
export { _default as default };