UNPKG

@restmatic/server

Version:

The core Pomegranate plugins for RestMatic

120 lines (116 loc) 5.01 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const fp_1 = require("lodash/fp"); const plugin_tools_1 = require("@pomegranate/plugin-tools"); const lodash_fun_1 = require("lodash-fun"); const tsGenerator = `// Generated by the Pomegranate cli on {{creationDate}} // mountPath: {{mountPath}} export const Routes = (Router, CaptureError) => { Router.get('/', (req, res, next) => { res.send('Hello from RestMatic!') }) Router.post('/', (req, res, next) => { res.json({}) }) Router.put('/', (req, res, next) => { res.json({}) }) Router.delete('/', (req, res, next) => { res.json({}) }) return Router } `; const joinMountPath = (path) => { let pathParts = fp_1.initial(path); let pathStart = pathParts.length ? pathParts : []; return `/${fp_1.last(path) === 'index' ? fp_1.join('/', pathStart) : fp_1.join('/', path)}`; }; exports.RouterPlugin = plugin_tools_1.CreatePlugin('action') .configuration({ name: 'Router', depends: ['@restmatic/PreRouter'] }) .directories([{ prop: 'main', path: '.' }]) .hooks({ load: (Injector, PluginLogger, PluginFiles, Express) => __awaiter(this, void 0, void 0, function* () { let RouteStructure = yield PluginFiles('main').fileListNested({ ext: '.js' }); let flatRoutes = lodash_fun_1.flattenKeys(RouteStructure); let RouteCount = 0; fp_1.each((route) => { let splitPath = fp_1.split('.', route); let routeFile = fp_1.get(route, flatRoutes); let pendingRoute = require(routeFile); let Routes = fp_1.get('Routes', pendingRoute); if (!Routes) { PluginLogger.error(`Router file ${route} does not export a Routes property.`, 0); throw new Error(`Route generation failed.`); } let injectedRoutes = Injector.inject(Routes); if (fp_1.isObject(injectedRoutes) && injectedRoutes.name === 'router') { let mountPath = joinMountPath(splitPath); RouteCount += 1; PluginLogger.log(`Loaded routes for ${mountPath}`); Express.use(mountPath, injectedRoutes); } else { PluginLogger.error(`Express Router: Attempted to load invalid route module: Route modules must return a Router Object.`); } }, fp_1.keys(flatRoutes)); PluginLogger.log(`Loaded ${RouteCount} route modules.`); }) }) .commands(function (PomConfig, PluginFiles, Handlebars) { return (yargs) => { return yargs .usage('usage: $0') .command({ command: 'generate <path> [filename]', aliases: 'g', describe: `Generates route file at <path>`, builder: (yargs) => { yargs .positional('path', { describe: 'The path the route file will be created.', default: '/', type: 'string' }) .positional('filename', { describe: 'The the filename to be created at path.', default: 'index.ts', type: 'string' }) .option('force', { alias: 'f', default: false, describe: 'overwrites the specified file if it exists.', type: 'boolean' }); }, handler: (argv) => __awaiter(this, void 0, void 0, function* () { let Pf = PluginFiles('main'); let filePath = Pf.join(argv.path, argv.filename); let exists = yield Pf.projectFileExists(filePath); let compile = Handlebars.compile(tsGenerator); let compiled = compile({ creationDate: new Date().toDateString(), mountPath: argv.path }); if (exists && !argv.force) { throw new Error(`${filePath} \n exists \n Rerun with --force to overwrite.`); } yield Pf.outputProjectFile(filePath, compiled); console.log(`Created @restmatic/Router route file at ${filePath}`); // console.log(argv, 'quack') }) }) .help(); }; }); //# sourceMappingURL=Router.js.map