Skip to content

Commit

Permalink
chore: added data interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
faizahmedfarooqui committed Jul 21, 2023
1 parent da35b1d commit fc3586f
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 67 deletions.
8 changes: 6 additions & 2 deletions packages/bolt/build/actions/route-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "@gluestack/helpers", "../common", "../helpers/exit-with-msg", "../helpers/validate-metadata", "../helpers/validate-services"], factory);
define(["require", "exports", "path", "@gluestack/helpers", "../common", "../helpers/exit-with-msg", "../helpers/validate-metadata", "../helpers/validate-services", "../helpers/data-interpolate"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const helpers_1 = require("@gluestack/helpers");
const common_1 = __importDefault(require("../common"));
const exit_with_msg_1 = require("../helpers/exit-with-msg");
const validate_metadata_1 = require("../helpers/validate-metadata");
const validate_services_1 = require("../helpers/validate-services");
const data_interpolate_1 = __importDefault(require("../helpers/data-interpolate"));
class RouteList {
handle() {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -36,6 +38,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
if (!_yamlContent.ingress || _yamlContent.ingress.length === 0) {
return (0, exit_with_msg_1.exitWithMsg)(">> No ingress found in config. Skipping route generation...");
}
// interpolate all variables from yaml and inject .env file's vars
const content = yield (0, data_interpolate_1.default)(_yamlContent, (0, path_1.join)(process.cwd(), ".env"));
const rows = [];
const head = [
"Domain",
Expand All @@ -46,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
"Rewrite Value",
"Client MaxBody (in MB)",
];
_yamlContent.ingress.forEach((ingress) => {
content.ingress.forEach((ingress) => {
const domain = ingress.domain || undefined;
const port = ingress.port || undefined;
if (!domain || !port) {
Expand Down
17 changes: 16 additions & 1 deletion packages/bolt/build/actions/service-up.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "chalk", "../helpers/docker-info", "../helpers/exit-with-msg", "../helpers/get-store", "../helpers/validate-metadata", "../helpers/validate-services", "../common", "../runners/service"], factory);
define(["require", "exports", "chalk", "path", "../helpers/docker-info", "../helpers/exit-with-msg", "../helpers/get-store", "../helpers/validate-metadata", "../helpers/validate-services", "../common", "../runners/service", "../helpers/data-interpolate"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const path_1 = require("path");
const docker_info_1 = require("../helpers/docker-info");
const exit_with_msg_1 = require("../helpers/exit-with-msg");
const get_store_1 = __importDefault(require("../helpers/get-store"));
const validate_metadata_1 = require("../helpers/validate-metadata");
const validate_services_1 = require("../helpers/validate-services");
const common_1 = __importDefault(require("../common"));
const service_1 = __importDefault(require("../runners/service"));
const data_interpolate_1 = __importDefault(require("../helpers/data-interpolate"));
class ServiceUp {
//
checkIfAlreadyUp(_yamlContent, serviceName) {
Expand All @@ -49,6 +51,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return true;
}
handle(serviceName, options) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
try {
let { serviceRunner: srOption, cache } = options;
Expand All @@ -68,8 +71,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
console.log(chalk_1.default.yellow(`>> Given "${srOption}" service runner is not supported for ${serviceName}, using "${content.supported_service_runners[0]}" instead!`));
srOption = content.supported_service_runners[0];
}
const { envfile: localENV } = content.service_runners["local"];
const { envfile: dockerENV } = content.service_runners["docker"];
const localENVPath = (0, path_1.join)(servicePath, localENV);
const dockerENVPath = (0, path_1.join)(servicePath, dockerENV);
// generates .env
yield common_1.default.generateEnv();
// Make data interpolate into service-runner's yaml content from given env file :: LOCAL
if ((_a = content === null || content === void 0 ? void 0 : content.service_runners) === null || _a === void 0 ? void 0 : _a.local) {
content.service_runners.local = yield (0, data_interpolate_1.default)(content.service_runners.local, localENVPath);
}
// Make data interpolate into service-runner's yaml content from given env file :: DOCKER
if ((_b = content === null || content === void 0 ? void 0 : content.service_runners) === null || _b === void 0 ? void 0 : _b.docker) {
content.service_runners.docker = yield (0, data_interpolate_1.default)(content.service_runners.docker, dockerENVPath);
}
let isConfigValid = false;
const serviceRunner = new service_1.default();
switch (srOption) {
Expand Down
97 changes: 56 additions & 41 deletions packages/bolt/build/actions/up.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
const update_store_1 = require("../helpers/update-store");
class Up {
handle(options) {
var _a, e_1, _b, _c;
var _a, e_1, _b, _c, _d, e_2, _e, _f;
return __awaiter(this, void 0, void 0, function* () {
let projectRunnerOption = "default";
const cache = options.cache || false;
Expand All @@ -63,9 +63,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
const vmRunners = ["vmlocal", "vmdocker"];
let isVmPresent = false;
try {
for (var _d = true, _e = __asyncValues(Object.entries(_yamlContent.services)), _f; _f = yield _e.next(), _a = _f.done, !_a;) {
_c = _f.value;
_d = false;
for (var _g = true, _h = __asyncValues(Object.entries(_yamlContent.services)), _j; _j = yield _h.next(), _a = _j.done, !_a;) {
_c = _j.value;
_g = false;
try {
const [serviceName, service] = _c;
if (data[serviceName] && data[serviceName].status !== "down") {
Expand All @@ -80,14 +80,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}
}
finally {
_d = true;
_g = true;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
if (!_g && !_a && (_b = _h.return)) yield _b.call(_h);
}
finally { if (e_1) throw e_1.error; }
}
Expand All @@ -97,46 +97,61 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
yield boltVm.create(cache);
yield (0, update_store_1.updateStore)("vm", "up");
}
Object.entries(_yamlContent.services).forEach(([serviceName]) => __awaiter(this, void 0, void 0, function* () {
if (data[serviceName] && data[serviceName].status !== "down") {
return;
}
// Validating and getting content from bolt.service.yaml
const { content } = yield common_1.default.getAndValidateService(serviceName, _yamlContent);
const { default_service_runner, supported_service_runners } = content;
let prepared_service_runner = default_service_runner;
if (projectRunnerOption === "host") {
if (!supported_service_runners.includes("local") &&
!supported_service_runners.includes("docker")) {
console.log(chalk_1.default.red(`>> ${serviceName} does not includes host service runners. Skipping...`));
return;
try {
for (var _k = true, _l = __asyncValues(Object.entries(_yamlContent.services)), _m; _m = yield _l.next(), _d = _m.done, !_d;) {
_f = _m.value;
_k = false;
try {
const [serviceName] = _f;
if (data[serviceName] && data[serviceName].status !== "down") {
continue;
}
// Validating and getting content from bolt.service.yaml
const { content } = yield common_1.default.getAndValidateService(serviceName, _yamlContent);
const { default_service_runner, supported_service_runners } = content;
let prepared_service_runner = default_service_runner;
if (projectRunnerOption === "host") {
if (!supported_service_runners.includes("local") &&
!supported_service_runners.includes("docker")) {
console.log(chalk_1.default.red(`>> ${serviceName} does not includes host service runners. Skipping...`));
continue;
}
else {
const availableRunners = supported_service_runners.filter((e) => !vmRunners.includes(e));
prepared_service_runner = availableRunners[0];
}
}
if (projectRunnerOption === "vm") {
if (!supported_service_runners.includes("vmlocal") &&
!supported_service_runners.includes("vmdocker")) {
console.log(chalk_1.default.red(`>> ${serviceName} does not includes vm service runners. Skipping...`));
continue;
}
else {
const availableRunners = supported_service_runners.filter((e) => !localRunners.includes(e));
prepared_service_runner = availableRunners[0];
}
}
const serviceUp = new service_up_1.default();
serviceUpPromises.push(serviceUp.handle(serviceName, {
serviceRunner: prepared_service_runner,
cache: cache,
ports,
}));
}
else {
const availableRunners = supported_service_runners.filter((e) => !vmRunners.includes(e));
prepared_service_runner = availableRunners[0];
finally {
_k = true;
}
}
if (projectRunnerOption === "vm") {
if (!supported_service_runners.includes("vmlocal") &&
!supported_service_runners.includes("vmdocker")) {
console.log(chalk_1.default.red(`>> ${serviceName} does not includes vm service runners. Skipping...`));
return;
}
else {
const availableRunners = supported_service_runners.filter((e) => !localRunners.includes(e));
prepared_service_runner = availableRunners[0];
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (!_k && !_d && (_e = _l.return)) yield _e.call(_l);
}
const serviceUp = new service_up_1.default();
serviceUpPromises.push(serviceUp.handle(serviceName, {
serviceRunner: prepared_service_runner,
cache: cache,
ports,
}));
}));
finally { if (e_2) throw e_2.error; }
}
yield Promise.all(serviceUpPromises);
// 2. generates .env
yield common_1.default.generateEnv();
// 5. starts nginx if the project runner is not vm and nginx config exists in bolt.yaml
if (_yamlContent.ingress) {
const nginxConfig = (0, path_1.join)(process.cwd(), bolt_configs_1.BOLT.NGINX_CONFIG_FILE_NAME);
Expand Down
4 changes: 1 addition & 3 deletions packages/bolt/build/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
yield (0, exit_with_msg_1.exitWithMsg)(`>> "${_yamlPath}" doesn't exists`);
}
const _yamlContent = yield (0, bolt_1.validateBolt)(yield (0, parse_yaml_1.parseYAML)(_yamlPath));
if (!_yamlContent ||
!_yamlContent.services ||
(0, lodash_1.isEmpty)(_yamlContent.services)) {
if (!(_yamlContent === null || _yamlContent === void 0 ? void 0 : _yamlContent.services) || (0, lodash_1.isEmpty)(_yamlContent.services)) {
yield (0, exit_with_msg_1.exitWithMsg)(`>> "${bolt_configs_1.BOLT.YAML_FILE_NAME}" services does not exists`);
}
return _yamlContent;
Expand Down
2 changes: 2 additions & 0 deletions packages/bolt/build/helpers/data-interpolate.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const interpolateEnvVariables: (data: any, envFilePath: string) => Promise<any>;
export default interpolateEnvVariables;
59 changes: 59 additions & 0 deletions packages/bolt/build/helpers/data-interpolate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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());
});
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./fs-readfile"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_readfile_1 = require("./fs-readfile");
const interpolateEnvVariables = (data, envFilePath) => __awaiter(void 0, void 0, void 0, function* () {
const envContent = yield (0, fs_readfile_1.readfile)(envFilePath);
if (!envContent) {
return data;
}
const envVars = envContent
.split('\n')
.filter((line) => line.trim() && !line.trim().startsWith('#'))
.map((line) => {
const [key, value] = line.split('=');
return { key: key.trim(), value: value.trim() };
});
const interpolateObject = (obj) => {
if (typeof obj === 'string') {
const regex = /\${(.+?)}/g;
return obj.replace(regex, (_, varName) => {
const envVar = envVars.find(env => env.key === varName);
return envVar ? envVar.value : '';
});
}
else if (Array.isArray(obj)) {
return obj.map(item => interpolateObject(item));
}
else if (typeof obj === 'object') {
const newObj = {};
for (const key in obj) {
newObj[key] = interpolateObject(obj[key]);
}
return newObj;
}
else {
return obj;
}
};
return interpolateObject(data);
});
exports.default = interpolateEnvVariables;
});
Loading

0 comments on commit fc3586f

Please sign in to comment.