zcatalyst-cli
Version:
Command Line Tool for CATALYST
128 lines (127 loc) • 7.09 kB
JavaScript
;
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ansi_colors_1 = require("ansi-colors");
const endpoints_1 = require("../../../endpoints");
const error_1 = __importDefault(require("../../../error"));
const fn_utils_1 = require("../../../fn-utils");
const runtime_store_1 = __importDefault(require("../../../runtime-store"));
const throbber_1 = __importDefault(require("../../../throbber"));
const constants_1 = require("../../../util_modules/constants");
const runtime_1 = __importDefault(require("../../../util_modules/constants/lib/runtime"));
const logger_1 = require("../../../util_modules/logger");
const option_1 = require("../../../util_modules/option");
const languages_1 = require("./languages");
const char_1 = require("../../../util_modules/char");
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
fn_utils_1.fnUtils.common.executeHook({ prefix: 'pre', command: 'deploy' });
const targets = yield fn_utils_1.fnUtils.common.validate();
if (targets.length === 0) {
const only = (0, option_1.getOptionValue)('only', false);
const except = (0, option_1.getOptionValue)('except', false);
const option = only ? 'only' : except ? 'except' : 'none';
throw new error_1.default('No functions are ready to be deployed', {
exit: 1,
errorId: 'IDX-1',
arg: [(0, ansi_colors_1.bold)(option)]
});
}
const refinedTargets = yield fn_utils_1.fnUtils.common.refineTargets(targets);
runtime_store_1.default.set('context.functions.targets', refinedTargets);
refinedTargets
.filter((target) => target.valid)
.forEach((target) => throbber_1.default.getInstance().add('function_deploy_' + target.name, {
text: `packaging function [${target.name}]`
}));
const [nodeTarget, javaTarget, pythonTarget] = yield Promise.all([
(0, languages_1.node)(refinedTargets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.node.value)) && target.valid; })),
(0, languages_1.java)(refinedTargets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.java.value)) && target.valid; })),
(0, languages_1.python)(refinedTargets.filter((target) => { var _a; return ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.python.value)) && target.valid; }))
]);
refinedTargets === null || refinedTargets === void 0 ? void 0 : refinedTargets.forEach((target) => {
if (target.compilationError) {
target.compilationError.forEach((error) => {
console.log(`${(0, ansi_colors_1.red)(char_1.CHAR.error)} Error while compiling function[${target.name}]`);
console.log(error);
});
}
if (target.compilationWarning) {
target.compilationWarning.forEach((warn) => {
console.warn(`${(0, ansi_colors_1.yellow)(char_1.CHAR.warning)} Warning while compiling function[${target.name}]`);
console.warn(warn);
});
}
});
const combinedTarget = [...(nodeTarget || []), ...(javaTarget || []), ...(pythonTarget || [])];
const batchSize = 1;
const fnAPI = yield (0, endpoints_1.functionsAPI)();
const appAPI = yield (0, endpoints_1.applogicAPI)();
while (combinedTarget.length > 0) {
const limit = combinedTarget.length > batchSize ? batchSize : combinedTarget.length;
const targetBatch = combinedTarget.splice(0, limit);
yield Promise.all(targetBatch.map((target) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
if (!target) {
return;
}
try {
let resp;
if (target.type !== undefined && target.type === constants_1.FN_TYPE.advanced) {
resp = (yield appAPI.deploy((_a = target.zip_stream) === null || _a === void 0 ? void 0 : _a.stream, {
stack: target.stack,
name: target.name,
memory: target.memory,
envVariables: target.env_var,
contentLength: (_b = target.zip_stream) === null || _b === void 0 ? void 0 : _b.length
}));
}
else {
const deployOptions = {
stack: target.stack,
name: target.name,
type: constants_1.REMOTE_REF.functions.type[target.type],
envVariables: target.env_var,
contentLength: (_c = target.zip_stream) === null || _c === void 0 ? void 0 : _c.length
};
if (target.type === constants_1.FN_TYPE.browserLogic && !target.memory && !target.id) {
deployOptions.memory = 512;
}
else {
deployOptions.memory = target.memory;
}
resp = (yield fnAPI.deploy((_d = target.zip_stream) === null || _d === void 0 ? void 0 : _d.stream, deployOptions));
}
target.id = resp.id + '';
fn_utils_1.fnUtils.common.generateUrlForTarget(target);
}
catch (e) {
const err = error_1.default.getErrorInstance(e);
(0, logger_1.debug)(err.stack);
target.valid = false;
target.failure_reason = err.message;
}
})));
}
const allTargets = refinedTargets.filter((target) => {
if (!target.valid) {
(0, logger_1.warning)('skipping deploy of target [' + target.name + '] since ' + target.failure_reason);
}
return target.valid;
});
runtime_store_1.default.set('payload.functions.targets', allTargets);
if (allTargets.length > 0) {
runtime_store_1.default.set('payload.functions.deploy', true);
}
fn_utils_1.fnUtils.common.executeHook({ prefix: 'post', command: 'deploy' });
});