zcatalyst-cli
Version:
Command Line Tool for CATALYST
137 lines (136 loc) • 6.88 kB
JavaScript
'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 __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 index_js_1 = __importDefault(require("../../../error/index.js"));
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 index_1 = require("../../../util_modules/logger/index");
const option_1 = require("../../../util_modules/option");
const languages_1 = require("./languages");
const compile_1 = require("../../../fn-utils/lib/java/compile");
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
yield 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 index_js_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);
const nodeFns = [];
const javaFns = [];
const pythonFns = [];
refinedTargets.filter((target) => {
var _a, _b, _c;
if (!target.valid) {
return false;
}
throbber_1.default.getInstance().add('function_deploy_' + target.name, {
text: `packaging function [${target.name}]`
});
if ((_a = target.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.node.value)) {
return nodeFns.push(target);
}
if ((_b = target.stack) === null || _b === void 0 ? void 0 : _b.startsWith(runtime_1.default.language.java.value)) {
return javaFns.push(target);
}
if ((_c = target.stack) === null || _c === void 0 ? void 0 : _c.startsWith(runtime_1.default.language.python.value)) {
return pythonFns.push(target);
}
return false;
});
const [nodeTarget, javaTarget, pythonTarget] = yield Promise.all([
(0, languages_1.node)(nodeFns),
(0, languages_1.java)(javaFns),
(0, languages_1.python)(pythonFns)
]);
javaTarget && (0, compile_1.printCompilationLog)(javaTarget);
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 = index_js_1.default.getErrorInstance(e);
(0, index_1.debug)(err.stack);
target.valid = false;
target.failure_reason = err.message;
}
})));
}
const allTargets = refinedTargets.filter((target) => {
if (!target.valid) {
(0, index_1.warning)('skipping deploy of target [' + target.name + '] since ' + target.failure_reason);
}
return target.valid;
});
runtime_store_1.default.set('context.payload.functions.targets', allTargets);
if (allTargets.length > 0) {
runtime_store_1.default.set('context.payload.functions.deploy', true);
}
yield fn_utils_1.fnUtils.common.executeHook({ prefix: 'post', command: 'deploy' });
if (allTargets.length < refinedTargets.length) {
throw new index_js_1.default('Error deploying functions: Invalid functions', {
skipHelp: true
});
}
});