zcatalyst-cli
Version:
Command Line Tool for CATALYST
146 lines (145 loc) • 8.45 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 path_1 = require("path");
const archiver_1 = __importDefault(require("../../archiver"));
const client_utils_1 = require("../../client-utils");
const languages_1 = require("../../deploy/features/functions/languages");
const index_js_1 = __importDefault(require("../../error/index.js"));
const fn_utils_1 = require("../../fn-utils");
const command_1 = __importDefault(require("../../internal/command"));
const runtime_store_1 = __importDefault(require("../../runtime-store"));
const constants_1 = require("../../util_modules/constants");
const runtime_1 = __importDefault(require("../../util_modules/constants/lib/runtime"));
const fs_1 = require("../../util_modules/fs");
const js_1 = require("../../util_modules/js");
const index_1 = require("../../util_modules/logger/index");
const yaml_1 = require("yaml");
const ansi_colors_1 = require("ansi-colors");
exports.default = new command_1.default('iac:pack [zip_name]')
.description('Creates a import ready zip file')
.needs('auth')
.needs('config')
.action((userZipName) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c;
const config = runtime_store_1.default.get('config');
const rootDir = (0, path_1.dirname)(config.configPath);
const filesAtRoot = yield fs_1.ASYNC.walk(rootDir, {
filter: { excludeDir: true, exclude: [] },
depth: 1
});
const templateFilePth = (_a = filesAtRoot.find((file) => file.path.match(constants_1.REGEX.project.template))) === null || _a === void 0 ? void 0 : _a.path;
if (templateFilePth === undefined) {
throw new index_js_1.default('No template file found', { exit: 1 });
}
const templateFile = yield fs_1.ASYNC.readFile(templateFilePth);
if (templateFile === undefined) {
throw new index_js_1.default('Unable to read template file', { exit: 1 });
}
const templateJson = js_1.JS.parseJSON(templateFile) || (0, yaml_1.parse)(templateFile);
const templateFns = ((_b = templateJson.components) === null || _b === void 0 ? void 0 : _b.Functions) || [];
const templateClient = ((_c = templateJson.components) === null || _c === void 0 ? void 0 : _c.WebClient) || [];
const hrTime = process.hrtime();
const archiveName = (userZipName === null || userZipName === void 0 ? void 0 : userZipName.replace('.zip', '')) || `iac_${hrTime[0] * 1000000000 + hrTime[1]}`;
const finalZip = new archiver_1.default(archiveName);
const optionFilter = yield Promise.resolve().then(() => __importStar(require('../../option-filter')));
const components = optionFilter.filterTargets({ pathSense: false });
yield Promise.all(components.map((component) => __awaiter(void 0, void 0, void 0, function* () {
switch (component) {
case 'functions':
const fnTargets = yield fn_utils_1.fnUtils.common.validate();
const refinedFns = yield fn_utils_1.fnUtils.common.refineTargets(fnTargets, false);
const fnNameByCodePath = {};
const [nodeFns, javaFns, pythonFns] = refinedFns.reduce((acc, fn) => {
var _a, _b, _c;
if (!fn.valid) {
throw new index_js_1.default(`Unable to pack since "${fn.name}" is not valid. Reason: ${fn.failure_reason}`);
}
const fnTemplateConfig = templateFns.find((tfn) => fn.name === tfn.properties.name);
if (!fnTemplateConfig) {
throw new index_js_1.default(`No property found for function ${fn.name} in project template`, { exit: 1 });
}
fnNameByCodePath[fn.name] = fnTemplateConfig.properties.code.path;
if ((_a = fn.stack) === null || _a === void 0 ? void 0 : _a.startsWith(runtime_1.default.language.node.value)) {
acc[0].push(fn);
}
if ((_b = fn.stack) === null || _b === void 0 ? void 0 : _b.startsWith(runtime_1.default.language.java.value)) {
acc[1].push(fn);
}
if ((_c = fn.stack) === null || _c === void 0 ? void 0 : _c.startsWith(runtime_1.default.language.python.value)) {
acc[2].push(fn);
}
return acc;
}, [[], [], []]);
const packedNodeFns = yield (0, languages_1.node)(nodeFns);
const packedJavaFns = yield (0, languages_1.java)(javaFns);
const packedPythonFns = yield (0, languages_1.python)(pythonFns);
const packedFns = [
...(packedNodeFns || []),
...(packedJavaFns || []),
...(packedPythonFns || [])
];
packedFns.forEach((fn) => {
var _a;
if (!fn.valid) {
throw new index_js_1.default(`Unable to pack since "${fn.name}" is not valid. Reason: ${fn.failure_reason}`);
}
finalZip.add(fnNameByCodePath[fn.name], (_a = fn.zip_stream) === null || _a === void 0 ? void 0 : _a.stream);
});
break;
case 'client':
yield client_utils_1.clientUtils.validate();
const clientName = runtime_store_1.default.get('context.client.name');
const clientTemplateConfig = templateClient.find((tClient) => clientName === tClient.properties.app_name);
if (!clientTemplateConfig) {
throw new index_js_1.default(`No property found for client ${clientName} in project template`, { exit: 1 });
}
const clientZipStream = yield client_utils_1.clientUtils.pack();
finalZip.add(clientTemplateConfig.properties.code.path, clientZipStream.stream);
break;
default:
(0, index_1.debug)(`skipping ${component} since there is no processing needed`);
break;
}
})));
finalZip.add((0, path_1.parse)(templateFilePth).name + '.json', JSON.stringify(templateJson, null, 2) + '\n');
yield (yield finalZip.finalize()).writeZip(rootDir);
(0, index_1.message)((0, ansi_colors_1.bold)((0, path_1.relative)(runtime_store_1.default.get('cwd'), (0, path_1.join)(rootDir, archiveName))) +
'.zip file has been successfully created.');
(0, index_1.info)();
(0, index_1.success)('Catalyst pack complete');
}));