UNPKG

bridgets

Version:

<p align="center"> <a href="https://bridgets.co"> <img src="http://bridgets.co/assets/logo-short.svg" height="48" /> <h1 align="center">BridgeTS</h1> </a> </p>

54 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compileSDK = void 0; const Utilities_1 = require("../Utilities"); const fs_1 = require("./fs"); const writeControllers_1 = require("./writeControllers"); let API; let APIImports = ''; let controllers = {}; const compileSDK = (routes, sdkLocation, typeLocation, sdkTypeName) => { API = { ...routes }; visiteRoutes(routes, [], sdkLocation, typeLocation, sdkTypeName); // WRITE A CODE TO PRETTIER THAT OBJECT let APIFile = "import { Fetch } from './fetchBridgeTS'\n\n" + APIImports + '\n' + `export const API = ${JSON.stringify(API).replaceAll(`)"`, `)`).replaceAll(`:"newBridgeTSControllerClient`, `:new`)}`; (0, fs_1.writeFile)(`${sdkLocation}/index`, APIFile); }; exports.compileSDK = compileSDK; const visiteRoutes = (routes, pathArray, sdkLocation, typeLocation, sdkTypeName) => { Object.entries(routes).forEach(([name, subRouteOrController]) => { if ((0, Utilities_1.isController)(subRouteOrController)) { console.log(subRouteOrController.constructor.name); (0, writeControllers_1.writeController)(subRouteOrController, [...pathArray, name], sdkLocation, typeLocation, sdkTypeName); let ctrlName; if (controllers[subRouteOrController.constructor.name]) { controllers[subRouteOrController.constructor.name]; ctrlName = subRouteOrController.constructor.name + (controllers[subRouteOrController.constructor.name] + 1).toString(); controllers[subRouteOrController.constructor.name] += 1; APIImports += `import { ${subRouteOrController.constructor.name} as ${ctrlName} } from '${(0, Utilities_1.pathArrayToPath)([...pathArray, name], '.')}';\n`; } else { ctrlName = subRouteOrController.constructor.name; APIImports += `import { ${ctrlName} } from '${(0, Utilities_1.pathArrayToPath)([...pathArray, name], '.')}';\n`; controllers[subRouteOrController.constructor.name] = 1; } const APIchild = getElemFromObjectWithPathArray(API, pathArray); APIchild[name] = `newBridgeTSControllerClient ${ctrlName}(Fetch)`; } else { (0, fs_1.createFolder)((0, Utilities_1.pathArrayToPath)([...pathArray, name], sdkLocation)); visiteRoutes(subRouteOrController, [...pathArray, name], sdkLocation, typeLocation, sdkTypeName); } }); }; const getElemFromObjectWithPathArray = (object, path) => { if (path.length === 0) return object; else return getElemFromObjectWithPathArray(object[path[0]], path.slice(1)); }; //# sourceMappingURL=compileSDK.js.map