UNPKG

@becomes/cms-bundler

Version:
520 lines 26.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArrays = (this && this.__spreadArrays) || function () { for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j]; return r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cli = void 0; var childProcess = require("child_process"); var path = require("path"); var fs = require("fs"); var util = require("util"); var util_1 = require("./util"); var interfaces_1 = require("./interfaces"); function spawn(cmd, args, options) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2, new Promise(function (resolve, reject) { var proc = childProcess.spawn(cmd, args, options); proc.on('close', function (code) { if (code !== 0) { reject(code); } else { resolve(); } }); })]; }); }); } function cli(args) { return __awaiter(this, void 0, void 0, function () { var options, config; return __generator(this, function (_a) { switch (_a.label) { case 0: options = util_1.ArgParser.parse(args); return [4, Promise.resolve().then(function () { return require("" + path.join(process.cwd(), 'bcms.config.js')); })]; case 1: config = _a.sent(); util_1.General.object.compareWithSchema(config, interfaces_1.ConfigSchema, 'config'); return [4, setEnv(config)]; case 2: _a.sent(); if (!options.dev) return [3, 19]; process.env.DEV = 'true'; if (!options.plugin) return [3, 9]; if (!options.backend) return [3, 5]; return [4, prepareBackend(config, true)]; case 3: _a.sent(); return [4, spawn('bcms-backend', [], { stdio: 'inherit' })]; case 4: _a.sent(); return [3, 8]; case 5: if (!options.ui) return [3, 8]; return [4, prepareUI(config, true)]; case 6: _a.sent(); return [4, spawn('bcms-ui', ['--dev'], { cwd: path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui'), stdio: 'inherit', })]; case 7: _a.sent(); _a.label = 8; case 8: return [3, 18]; case 9: if (!options.backend) return [3, 12]; return [4, prepareBackend(config)]; case 10: _a.sent(); return [4, spawn('bcms-backend', [], { stdio: 'inherit' })]; case 11: _a.sent(); return [3, 18]; case 12: if (!options.ui) return [3, 15]; return [4, prepareUI(config)]; case 13: _a.sent(); return [4, spawn('bcms-ui', ['--dev'], { cwd: path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui'), stdio: 'inherit', })]; case 14: _a.sent(); return [3, 18]; case 15: return [4, prepareBackend(config)]; case 16: _a.sent(); return [4, prepareUI(config)]; case 17: _a.sent(); spawn('bcms-backend', undefined, { stdio: 'inherit' }).catch(function (error) { console.error('Backend', error); }); spawn('bcms-ui', ['--dev'], { cwd: path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui'), stdio: 'inherit', }).catch(function (error) { console.error('UI', error); }); _a.label = 18; case 18: return [3, 30]; case 19: if (!options.backend) return [3, 22]; return [4, prepareBackend(config)]; case 20: _a.sent(); return [4, spawn('bcms-backend', undefined, { stdio: 'inherit' })]; case 21: _a.sent(); return [3, 30]; case 22: if (!options.ui) return [3, 25]; return [4, prepareUI(config)]; case 23: _a.sent(); return [4, spawn('bcms-ui', ['--build'], { cwd: path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui'), stdio: 'inherit', })]; case 24: _a.sent(); return [3, 30]; case 25: return [4, prepareBackend(config)]; case 26: _a.sent(); return [4, prepareUI(config)]; case 27: _a.sent(); return [4, spawn('bcms-ui', ['--build'], { cwd: path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui'), stdio: 'inherit', })]; case 28: _a.sent(); return [4, spawn('bcms-backend', undefined, { stdio: 'inherit' })]; case 29: _a.sent(); _a.label = 30; case 30: return [2]; } }); }); } exports.cli = cli; function setEnv(config) { return __awaiter(this, void 0, void 0, function () { var hash; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui', 'hash.txt'))]; case 1: if (!_a.sent()) return [3, 3]; return [4, util.promisify(fs.readFile)(path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui', 'hash.txt'))]; case 2: hash = (_a.sent()).toString(); process.env.BCMS_UI_BUNDLE_HASH = hash; _a.label = 3; case 3: process.env.JWT_ISSUER = config.backend.security.jwt.issuer; process.env.JWT_SECRET = config.backend.security.jwt.secret; process.env.JWT_EXP_AFTER = config.backend.security.jwt.expireIn ? '' + config.backend.security.jwt.expireIn : '1200000'; process.env.API_PORT = '' + config.backend.port; if (config.backend.database.fs) { process.env.DB_USE_FS = 'true'; process.env.DB_PRFX = config.backend.database.fs; } else if (config.backend.database.mongodb) { if (config.backend.database.mongodb.atlas) { process.env.DB_NAME = config.backend.database.mongodb.atlas.name; process.env.DB_USER = config.backend.database.mongodb.atlas.user; process.env.DB_PASS = config.backend.database.mongodb.atlas.password; process.env.DB_PRFX = config.backend.database.mongodb.atlas.prefix; process.env.DB_CLUSTER = config.backend.database.mongodb.atlas.cluster; } else if (config.backend.database.mongodb.selfHosted) { process.env.DB_HOST = config.backend.database.mongodb.selfHosted.host; process.env.DB_PORT = '' + config.backend.database.mongodb.selfHosted.port; process.env.DB_NAME = config.backend.database.mongodb.selfHosted.name; process.env.DB_USER = config.backend.database.mongodb.selfHosted.user; process.env.DB_PASS = config.backend.database.mongodb.selfHosted.password; process.env.DB_PRFX = config.backend.database.mongodb.selfHosted.prefix; } } return [2]; } }); }); } function prepareBackend(config, inPlugin) { return __awaiter(this, void 0, void 0, function () { var _a, pluginsFile, plugins, cntlr, mdlwr, _b, _c, _i, i, plugin, controllerPath, middlewarePath, assets, importsContentToReplace, assetsContentToReplace, outputFile; return __generator(this, function (_d) { switch (_d.label) { case 0: _a = inPlugin; if (_a) return [3, 2]; return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'plugins'))]; case 1: _a = (_d.sent()); _d.label = 2; case 2: if (!_a) return [3, 4]; return [4, spawn('npm', ['run', 'plugin:build'], { stdio: 'inherit', })]; case 3: _d.sent(); _d.label = 4; case 4: return [4, util.promisify(fs.readFile)(path.join(process.cwd(), 'node_modules', '@becomes', 'cms-backend', 'plugins', 'manager.js'))]; case 5: pluginsFile = (_d.sent()).toString(); if (!inPlugin) return [3, 8]; return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'dist', 'backend', 'controller.js'))]; case 6: cntlr = (_d.sent()) ? true : false; return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'dist', 'backend', 'middleware.js'))]; case 7: mdlwr = (_d.sent()) ? true : false; plugins = [ { controller: cntlr ? { path: path.join(process.cwd(), 'dist', 'backend', 'controller.js'), name: 'my_awesome_plugin_controller', } : undefined, middleware: mdlwr ? { path: path.join(process.cwd(), 'dist', 'backend', 'middleware.js'), name: 'my_awesome_plugin_middleware', } : undefined, }, ]; return [3, 13]; case 8: plugins = []; _b = []; for (_c in config.plugins) _b.push(_c); _i = 0; _d.label = 9; case 9: if (!(_i < _b.length)) return [3, 13]; i = _b[_i]; plugin = config.plugins[i]; if (!plugin.backend) return [3, 12]; controllerPath = void 0; return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'plugins', plugin.name, 'backend', 'controller.js'))]; case 10: if (_d.sent()) { controllerPath = path.join(process.cwd(), 'plugins', plugin.name, 'backend', 'controller.js'); } else { controllerPath = path.join(process.cwd(), 'node_modules', plugin.name, 'backend', 'controller.js'); } middlewarePath = void 0; return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'plugins', plugin.name, 'backend', 'middleware.js'))]; case 11: if (_d.sent()) { middlewarePath = path.join(process.cwd(), 'plugins', plugin.name, 'backend', 'middleware.js'); } else { middlewarePath = path.join(process.cwd(), 'node_modules', plugin.name, 'backend', 'middleware.js'); } plugins.push({ controller: controllerPath && plugin.backend.controller ? { path: controllerPath, name: plugin.name .toLowerCase() .replace(/ /g, '_') .replace(/-/g, '_') .replace(/[^0-9a-z_-_]+/g, '') + '_controller', } : undefined, middleware: middlewarePath && plugin.backend.middleware ? { path: middlewarePath, name: plugin.name .toLowerCase() .replace(/ /g, '_') .replace(/-/g, '_') .replace(/[^0-9a-z_-_]+/g, '') + '_middleware', } : undefined, }); _d.label = 12; case 12: _i++; return [3, 9]; case 13: assets = __spreadArrays(plugins .filter(function (e) { return (e.controller ? true : false); }) .map(function (plugin) { return { import: "var " + plugin.controller.name + " = require(\"" + plugin.controller.path + "\")", controller: plugin.controller.name, }; }), plugins .filter(function (e) { return (e.middleware ? true : false); }) .map(function (plugin) { return { import: "var " + plugin.middleware.name + " = require(\"" + plugin.middleware.path + "\");", middleware: plugin.middleware.name, }; })); importsContentToReplace = '/*%IMPORTS_START%*/' + util_1.General.string.getTextBetween(pluginsFile, '/*%IMPORTS_START%*/', '/*%IMPORTS_END%*/') + '/*%IMPORTS_END%*/'; assetsContentToReplace = '/*%ASSETS_START%*/' + util_1.General.string.getTextBetween(pluginsFile, '/*%ASSETS_START%*/', '/*%ASSETS_END%*/') + '/*%ASSETS_END%*/'; outputFile = pluginsFile .replace(importsContentToReplace, '/*%IMPORTS_START%*/\n' + assets .map(function (e) { return e.import; }) .join('\n') + '\n/*%IMPORTS_END%*/') .replace(assetsContentToReplace, '/*%ASSETS_START%*/\n' + 'exports.controllers = [' + assets .filter(function (e) { return (e.controller ? true : false); }) .map(function (e) { return e.controller; }) .join(',') + '];\n' + 'exports.middleware = [' + assets .filter(function (e) { return (e.middleware ? true : false); }) .map(function (e) { return e.middleware; }) .join(',') + '];\n' + '/*%ASSETS_END%*/'); return [4, util.promisify(fs.writeFile)(path.join(process.cwd(), 'node_modules', '@becomes', 'cms-backend', 'plugins', 'manager.js'), outputFile)]; case 14: _d.sent(); return [2]; } }); }); } function prepareUI(config, inPlugin) { return __awaiter(this, void 0, void 0, function () { var plugins, name_1, mainFilePath, mainFile, startPluginsFlag, endPluginsFlag, startRouterPluginsFlag, endRouterPluginsFlag, startPluginsIndex, endPluginsIndex, startRouterPluginsIndex, endRouterPluginsIndex, importPaths, pluginElements, routerElements, _a, _b, _i, i, plugin, pathToPlugin, outputMain; return __generator(this, function (_c) { switch (_c.label) { case 0: if (inPlugin) { name_1 = config.plugins[0].name .toLowerCase() .replace(/ /g, '_') .replace(/-/g, '_') .replace(/[^0-9a-z_-_]+/g, ''); plugins = [ { originalName: config.plugins[0].name, component: name_1, displayName: config.plugins[0].frontend.displayName, path: name_1.replace(/_/g, '-'), props: config.plugins[0].frontend.props, icon: '/assets/icons/default-plugin.svg', }, ]; } else { plugins = config.plugins .filter(function (plugin) { return (plugin.frontend ? true : false); }) .map(function (plugin) { var name = plugin.name .toLowerCase() .replace(/ /g, '_') .replace(/-/g, '_') .replace(/[^0-9a-z_-_]+/g, ''); return { originalName: plugin.name, component: plugin.name .toLowerCase() .replace(/ /g, '_') .replace(/-/g, '_') .replace(/[^0-9a-z_-_]+/g, '') .split('_') .map(function (e) { return e.substring(0, 1).toUpperCase() + e.substring(1); }) .join(''), displayName: plugin.frontend.displayName, path: name.replace(/_/g, '-'), props: plugin.frontend.props, icon: plugin.frontend.icon ? plugin.frontend.icon : '/assets/icons/default-plugin.svg', }; }); } mainFilePath = path.join(process.cwd(), 'node_modules', '@becomes', 'cms-ui', 'src', 'app.svelte'); return [4, util.promisify(fs.readFile)(mainFilePath)]; case 1: mainFile = (_c.sent()).toString(); startPluginsFlag = '/*%PLUGINS_START%*/'; endPluginsFlag = '/*%PLUGINS_END%*/'; startRouterPluginsFlag = '/*%ROUTER_PLUGINS_START%*/'; endRouterPluginsFlag = '/*%ROUTER_PLUGINS_END%*/'; startPluginsIndex = mainFile.indexOf(startPluginsFlag); endPluginsIndex = mainFile.indexOf(endPluginsFlag) + endPluginsFlag.length; startRouterPluginsIndex = mainFile.indexOf(startRouterPluginsFlag); endRouterPluginsIndex = mainFile.indexOf(endRouterPluginsFlag) + endRouterPluginsFlag.length; importPaths = []; pluginElements = []; routerElements = []; _a = []; for (_b in plugins) _a.push(_b); _i = 0; _c.label = 2; case 2: if (!(_i < _a.length)) return [3, 7]; i = _a[_i]; plugin = plugins[i]; pathToPlugin = void 0; if (!inPlugin) return [3, 3]; pathToPlugin = path.join(process.cwd(), 'plugin', 'frontend', 'main.svelte'); return [3, 5]; case 3: return [4, util.promisify(fs.exists)(path.join(process.cwd(), 'node_modules', plugin.originalName, 'frontend', 'main.svelte'))]; case 4: if (_c.sent()) { pathToPlugin = path.join(process.cwd(), 'node_modules', plugin.originalName, 'frontend', 'main.svelte'); } else { pathToPlugin = path.join(process.cwd(), 'plugins', plugin.originalName, 'frontend', 'main.svelte'); } _c.label = 5; case 5: importPaths.push("import " + plugin.component + " from '" + pathToPlugin + "';"); { pluginElements.push("\n {\n originalName: '" + plugin.originalName + "',\n component: " + plugin.component + ",\n displayName: '" + plugin.displayName + "',\n path: '" + plugin.path + "',\n icon: '" + plugin.icon + "',\n }\n "); } { routerElements.push("{\n path: '/dashboard/plugins/" + plugin.path + "',\n component: " + plugin.component + ",\n props: " + (plugin.props ? JSON.stringify(plugin.props) : 'undefined') + "\n }"); } _c.label = 6; case 6: _i++; return [3, 2]; case 7: outputMain = mainFile.substring(0, startPluginsIndex) + (startPluginsFlag + "\n") + importPaths.join('\n') + ("\nconst plugins = [" + pluginElements.join(',\n') + "];\n") + ("" + endPluginsFlag) + mainFile.substring(endPluginsIndex, startRouterPluginsIndex) + (startRouterPluginsFlag + "\n") + routerElements.join(',') + (",\n" + endRouterPluginsFlag + "\n") + mainFile.substring(endRouterPluginsIndex); return [4, util.promisify(fs.writeFile)(mainFilePath, outputMain)]; case 8: _c.sent(); return [2]; } }); }); } //# sourceMappingURL=cli.js.map