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

117 lines (100 loc) 6.26 kB
#!/usr/bin/env node import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; import "isomorphic-fetch"; import { program } from "commander"; import apiDocParser from "@api-platform/api-doc-parser"; import generators from "./generators.js"; var dirname = path.dirname(fileURLToPath(import.meta.url)); var packageJson = JSON.parse(fs.readFileSync("".concat(dirname, "/../package.json"), "utf-8")); function main() { return _main.apply(this, arguments); } function _main() { _main = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var options, entrypoint, outputDirectory, entrypointWithSlash, generator, resourceToGenerate, serverPath, parser; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: program.version(packageJson.version).description("Generate apps built with Next, Nuxt, Quasar, React, React Native, Vue or Vuetify for any API documented using Hydra or OpenAPI").usage("entrypoint outputDirectory").option("-r, --resource [resourceName]", "Generate CRUD for the given resource").option("-p, --hydra-prefix [hydraPrefix]", "The hydra prefix used by the API", "hydra:").option("--username [username]", "Username for basic auth (Hydra only)").option("--password [password]", "Password for basic auth (Hydra only)").option("--bearer [bearer]", "Token for bearer auth (Hydra only)").option("-g, --generator [generator]", 'The generator to use, one of "next", "nuxt", "quasar", "react", "react-native", "typescript", "vue", "vuetify" or a path to a custom generator of your choice', "next").option("-t, --template-directory [templateDirectory]", "The templates directory base to use. Final directory will be ${templateDirectory}/${generator}", "".concat(dirname, "/../templates/")).option("-f, --format [hydra|openapi3|openapi2]", '"hydra", "openapi3" or "openapi2"', "hydra").option("-s, --server-path [serverPath]", "Path to express server file to allow route dynamic addition (Next.js generator only)").parse(process.argv); if (2 !== program.args.length && (!process.env.API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT || !process.env.API_PLATFORM_CLIENT_GENERATOR_OUTPUT)) { program.help(); } options = program.opts(); entrypoint = program.args[0] || process.env.API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT; outputDirectory = program.args[1] || process.env.API_PLATFORM_CLIENT_GENERATOR_OUTPUT; entrypointWithSlash = entrypoint.endsWith("/") ? entrypoint : entrypoint + "/"; _context.next = 8; return generators(options.generator); case 8: _context.t0 = _context.sent; generator = (0, _context.t0)({ hydraPrefix: options.hydraPrefix, templateDirectory: options.templateDirectory }); resourceToGenerate = options.resource ? options.resource.toLowerCase() : null; serverPath = options.serverPath ? options.serverPath.toLowerCase() : null; parser = function parser(entrypointWithSlash) { // parserOptions are used to set headers on the hydra-requests var parserOptions = {}; // options refers to the opts set via the CLI if (options.username && options.password) { var encoded = Buffer.from("".concat(options.username, ":").concat(options.password)).toString("base64"); parserOptions.headers = new Headers(); parserOptions.headers.set("Authorization", "Basic ".concat(encoded)); } if (options.bearer) { parserOptions.headers = new Headers(); parserOptions.headers.set("Authorization", "Bearer ".concat(options.bearer)); } switch (options.format) { case "swagger": // deprecated case "openapi2": return apiDocParser.parseSwaggerDocumentation(entrypointWithSlash); case "openapi3": return apiDocParser.parseOpenApi3Documentation(entrypointWithSlash); default: return apiDocParser.parseHydraDocumentation(entrypointWithSlash, parserOptions); } }; // check generator dependencies generator.checkDependencies(outputDirectory, serverPath); parser(entrypointWithSlash).then(function (ret) { ret.api.resources.filter(function (_ref) { var deprecated = _ref.deprecated; return !deprecated; }).filter(function (resource) { var nameLc = resource.name.toLowerCase(); var titleLc = resource.title.toLowerCase(); return null === resourceToGenerate || nameLc === resourceToGenerate || titleLc === resourceToGenerate; }).map(function (resource) { var filterDeprecated = function filterDeprecated(list) { return list.filter(function (_ref2) { var deprecated = _ref2.deprecated; return !deprecated; }); }; resource.fields = filterDeprecated(resource.fields); resource.readableFields = filterDeprecated(resource.readableFields); resource.writableFields = filterDeprecated(resource.writableFields); generator.generate(ret.api, resource, outputDirectory, serverPath); return resource; }) // display helps after all resources have been generated to check relation dependency for example .forEach(function (resource) { return generator.help(resource, outputDirectory); }); })["catch"](function (e) { console.log(e); }); case 15: case "end": return _context.stop(); } } }, _callee); })); return _main.apply(this, arguments); } main();