@metacall/deploy
Version:
Tool for deploying into MetaCall FaaS platform.
170 lines (168 loc) • 6.5 kB
JavaScript
;
/*
About File:
1) CLI login
2) If there is no token present, then it asks for your loging credentials and saves the token into config.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.auth = void 0;
// remember to uninstall it
const login_1 = __importDefault(require("@metacall/protocol/login"));
const protocol_1 = __importDefault(require("@metacall/protocol/protocol"));
const signup_1 = __importDefault(require("@metacall/protocol/signup"));
const token_1 = require("@metacall/protocol/token");
const args_1 = __importDefault(require("./cli/args"));
const inputs_1 = require("./cli/inputs");
const messages_1 = require("./cli/messages");
const selection_1 = require("./cli/selection");
const config_1 = require("./config");
const deploy_1 = require("./deploy");
const utils_1 = require("./utils");
const authToken = async (config) => {
var _a, _b;
const askToken = () => (0, inputs_1.maskedInput)('Please enter your metacall token');
const shouldKeepAsking = args_1.default['token'] === undefined;
let token = args_1.default['token'] || (await askToken());
const api = (0, protocol_1.default)(token, config.baseURL);
if (process.stdout.isTTY && shouldKeepAsking) {
while (utils_1.forever) {
try {
await api.validate();
break;
}
catch (err) {
(0, messages_1.warn)(`Token invalid: ${String((_a = err.response) === null || _a === void 0 ? void 0 : _a.data)}`);
token = await askToken();
}
}
}
else {
try {
await api.validate();
}
catch (err) {
(0, messages_1.error)(`Token invalid: ${String((_b = err.response) === null || _b === void 0 ? void 0 : _b.data)}`);
}
}
if ((0, token_1.expiresIn)(token) < config.renewTime) {
// Token expires in < renewTime
token = await api.refresh();
}
return token;
};
const authLogin = async (config) => {
var _a, _b;
const askEmail = () => (0, inputs_1.input)('Please enter your email id:');
const askPassword = () => (0, inputs_1.maskedInput)('Please enter your password:');
let email = '';
let password = '';
const shouldKeepAsking = args_1.default['email'] === undefined || args_1.default['password'] === undefined;
const askCredentials = async () => {
email = args_1.default['email'] || (await askEmail());
password = args_1.default['password'] || (await askPassword());
};
await askCredentials();
// Now we got email and password let's call login api endpoint and get the token and store it int somewhere else
let token = '';
if (process.stdout.isTTY && shouldKeepAsking) {
while (utils_1.forever) {
try {
token = await (0, login_1.default)(email, password, config.baseURL);
break;
}
catch (err) {
(0, messages_1.warn)(String((_a = err.response) === null || _a === void 0 ? void 0 : _a.data));
args_1.default['email'] = args_1.default['password'] = undefined;
await askCredentials();
}
}
}
else {
try {
token = await (0, login_1.default)(email, password, config.baseURL);
}
catch (err) {
(0, messages_1.error)(String((_b = err.response) === null || _b === void 0 ? void 0 : _b.data));
}
}
return token;
};
const authSignup = async (config) => {
var _a;
const askEmail = () => (0, inputs_1.input)('Please enter your email id:');
const askAlias = () => (0, inputs_1.input)('Please enter your Alias:');
const askPassword = () => (0, inputs_1.maskedInput)('Please enter your password:');
const askPasswordConfirmation = () => (0, inputs_1.maskedInput)('Confirm password:');
let email = '';
let password = '';
let passwordConfirmation = '';
let userAlias = '';
const askCredentials = async () => {
email = email || (await askEmail());
password = password || (await askPassword());
passwordConfirmation =
passwordConfirmation || (await askPasswordConfirmation());
userAlias = userAlias || (await askAlias());
};
const askData = async () => {
while (utils_1.forever) {
await askCredentials();
if (password !== passwordConfirmation) {
(0, messages_1.warn)('Passwords did not match.');
password = '';
passwordConfirmation = '';
}
else {
break;
}
}
};
await askData();
let res;
while (utils_1.forever) {
try {
res = await (0, signup_1.default)(email, password, userAlias, config.baseURL);
(0, messages_1.info)(res);
(0, messages_1.info)('Visit Metacall Hub directly to learn more about deployments and to purchase plans: https://metacall.io/pricing/');
break;
}
catch (err) {
const errorMessage = String((_a = err.response) === null || _a === void 0 ? void 0 : _a.data);
(0, messages_1.warn)(errorMessage);
email = password = passwordConfirmation = userAlias = '';
await askData();
}
}
return process.exit(deploy_1.ErrorCode.Ok);
};
const authSelection = async (config) => {
const token = await (async () => {
if (args_1.default['email'] || args_1.default['password']) {
return await authLogin(config);
}
else if (args_1.default['token']) {
return await authToken(config);
}
else {
const methods = {
'Login by token': authToken,
'Login by email and password': authLogin,
'New user, sign up': authSignup
};
return await methods[await (0, selection_1.loginSelection)(Object.keys(methods))](config);
}
})();
await (0, config_1.save)({ token });
(0, messages_1.info)('Login Successfull!');
return token;
};
const auth = async (config) => {
const token = process.env['METACALL_API_KEY'] ||
config.token ||
(await authSelection(config));
return token;
};
exports.auth = auth;