zcatalyst-cli
Version:
Command Line Tool for CATALYST
164 lines (163 loc) • 9.44 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 ansi_colors_1 = require("ansi-colors");
const endpoints_1 = require("../../endpoints");
const error_1 = __importDefault(require("../../error"));
const command_1 = __importDefault(require("../../internal/command"));
const prompt_1 = __importDefault(require("../../prompt"));
const runtime_store_1 = __importDefault(require("../../runtime-store"));
const constants_1 = require("../../util_modules/constants");
const logger_1 = require("../../util_modules/logger");
const option_1 = require("../../util_modules/option");
const project_1 = require("../../util_modules/project");
exports.default = new command_1.default('functions:shell')
.description('Launch a node shell to test emulated functions of any stack or type, other than Advanced I/O functions')
.option('--http [port]', 'enables http mode for the basicIO functions and start server at specified port' +
`(default: ${constants_1.DEFAULT.serve_port.http.basicio})`)
.option('--debug [port]', 'the functions are invoked from local environment enabling debugging options on provided port.' +
`(default: ${constants_1.DEFAULT.serve_port.debug.basicio})`)
.option('--tunnel <port>', 'start tunneling functions at the specified port')
.option('--enable-tunnel [tunnel-url]', 'enable tunneling for this particular command execution')
.option('--ignore-scripts', 'ignore the pre and post lifecycle scripts')
.option('--watch', 'watch for file and directory changes and enable hot reload' +
'\n' +
'(deprecated: The watch mode will be enabled by default. To disable watch mode use the --no-watch option)')
.option('--no-watch', 'disable watching the files for changes')
.needs('auth', constants_1.NEEDED_SCOPES.SERVE)
.needs('config')
.needs('rc')
.ci(false)
.action(() => __awaiter(void 0, void 0, void 0, function* () {
const httpOpt = (0, option_1.getOptionValue)('http', false);
if (httpOpt && typeof httpOpt !== 'boolean') {
runtime_store_1.default.set('context.port.http.bio', httpOpt);
}
const debugOpt = (0, option_1.getOptionValue)('debug', false);
if (debugOpt && typeof debugOpt !== 'boolean') {
runtime_store_1.default.set('context.port.debug.bio', debugOpt);
}
const tunnelOpt = (0, option_1.getOptionValue)('tunnel', false);
if (tunnelOpt && typeof tunnelOpt !== 'boolean') {
runtime_store_1.default.set('context.port.tunnel', tunnelOpt);
}
const enableTunnelOpt = (0, option_1.getOptionValue)('enableTunnel', false);
if (!tunnelOpt && enableTunnelOpt) {
throw new error_1.default('Invalid Command. Use --enable-tunnel option with the -—tunnel option.', { exit: 1 });
}
if (enableTunnelOpt) {
const _tunnelAPI = yield (0, endpoints_1.tunnelAPI)();
const tunnelDetails = yield _tunnelAPI.getTunnel().catch((er) => {
const _er = error_1.default.getErrorInstance(er);
if (_er.status === 404) {
throw new error_1.default('Tunneling not configured', {
exit: 1,
errorId: 'FSHELL-1',
arg: [
(0, ansi_colors_1.bold)(`${(0, project_1.getProjectName)()}(${(0, project_1.getProjectId)()})`),
ansi_colors_1.italic.underline(`${constants_1.ORIGIN.console}/baas/${(0, project_1.getEnvId)()}/project/${(0, project_1.getProjectId)()}/Development#/settings/project/tunneling`)
]
});
}
throw _er;
});
if (tunnelDetails.tunnel_status === 'Disabled') {
runtime_store_1.default.set('context.fn_shell.enable_tunnel', enableTunnelOpt);
(0, logger_1.info)();
(0, logger_1.info)((0, ansi_colors_1.yellow)('Tunneling will be disabled at the end of this command execution'));
(0, logger_1.info)();
}
else {
(0, logger_1.info)();
(0, logger_1.info)(`Tunneling is already enabled for the project ${(0, ansi_colors_1.bold)((0, project_1.getProjectName)())}(${(0, project_1.getProjectId)()}) with the configurations below:`);
(0, logger_1.info)();
(0, logger_1.info)((0, ansi_colors_1.bold)('Tunneling URL: ') + ansi_colors_1.italic.underline(tunnelDetails.tunnel_url));
(0, logger_1.info)((0, ansi_colors_1.bold)('Tunneled functions: '));
tunnelDetails.function_details.forEach((fn) => {
(0, logger_1.info)('* ' + fn.name);
});
(0, logger_1.info)((0, ansi_colors_1.bold)('Enabled by: ') + (0, ansi_colors_1.italic)(tunnelDetails.created_by.email_id));
(0, logger_1.info)((0, ansi_colors_1.bold)('Enabled on: ') + tunnelDetails.enabled_time);
(0, logger_1.info)();
if (typeof enableTunnelOpt === 'boolean') {
const continueAsIs = yield prompt_1.default.ask(prompt_1.default.question('consent', 'Do you wish to continue ?', {
type: 'list',
choices: [
prompt_1.default.choice('continue', { value: true, short: 'continue' }),
prompt_1.default.choice('abort', { value: false, short: 'aborted' })
]
}));
if (continueAsIs.consent === false) {
return;
}
(0, logger_1.info)();
(0, logger_1.info)('Ignoring `--enable-tunnel` option since tunneling is already enabled');
(0, logger_1.info)();
}
else if (enableTunnelOpt !== tunnelDetails.tunnel_url) {
(0, logger_1.info)('Tunneling has already been enabled with a different tunneling URL.');
(0, logger_1.info)('Do you wish to update the tunnel request with the tunneling URL entered in the --enable-tunnel option?');
(0, logger_1.info)();
(0, logger_1.info)((0, ansi_colors_1.red)('- ' + tunnelDetails.tunnel_url));
(0, logger_1.info)((0, ansi_colors_1.green)('+ ' + enableTunnelOpt));
(0, logger_1.info)();
const updateCont = yield prompt_1.default.ask(prompt_1.default.question('consent', 'Do you wish to update and continue', {
type: 'list',
choices: [
prompt_1.default.choice('continue', { value: true, short: 'continue' }),
prompt_1.default.choice('abort', { value: false, short: 'aborted' })
]
}));
if (updateCont.consent === false) {
return;
}
yield (yield (0, endpoints_1.tunnelAPI)()).disableTunnel();
runtime_store_1.default.set('context.fn_shell.enable_tunnel', enableTunnelOpt);
(0, logger_1.info)();
(0, logger_1.info)((0, ansi_colors_1.yellow)('Tunneling will be disabled at the end of this command execution'));
(0, logger_1.info)();
}
else {
(0, logger_1.info)();
(0, logger_1.info)('Ignoring `--enable-tunnel` option since a tunnel already exists with the same tunnel url provided: ' +
ansi_colors_1.italic.underline(enableTunnelOpt));
(0, logger_1.info)();
}
}
}
return (yield Promise.resolve().then(() => __importStar(require('../../shell')))).default();
}));