zcatalyst-cli
Version:
Command Line Tool for CATALYST
59 lines (58 loc) • 2.66 kB
JavaScript
;
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 config_store_1 = __importDefault(require("../config-store"));
const dc_1 = require("../dc");
const error_1 = __importDefault(require("../error"));
const credential_1 = __importDefault(require("../internal/credential"));
const runtime_store_1 = __importDefault(require("../runtime-store"));
const constants_1 = require("../util_modules/constants");
const env_1 = require("../util_modules/env");
const logger_1 = require("../util_modules/logger");
const option_1 = require("../util_modules/option");
exports.default = (inScopes = []) => {
const activeDC = (0, dc_1.getActiveDC)();
const tokenOpts = [
{
option: '--token',
token: (0, option_1.getGlobalOptionValue)('token'),
temp: true
},
{
option: 'CATALYST_TOKEN env',
token: (0, env_1.getEnvVariable)('CATALYST_TOKEN', (0, env_1.getEnvVariable)('ZC_TOKEN')),
temp: true
},
{
option: 'signed-in user',
token: config_store_1.default.get(`${activeDC}.credential`),
temp: false
}
];
const tokenObj = tokenOpts.find((opts) => typeof opts.token === 'string');
if (tokenObj === undefined) {
throw new error_1.default('Command requires authentication', {
exit: 0,
errorId: 'AUTH-1',
arg: [(0, ansi_colors_1.bold)('catalyst login'), (0, ansi_colors_1.bold)('--token')]
});
}
(0, logger_1.debug)(`> authorizing via ${tokenObj.option} option`);
const existingScopes = config_store_1.default.get(`${activeDC}.scopes`, []);
const requiredScopes = [constants_1.SCOPE.projects, ...inScopes];
(0, logger_1.debug)('> command requires scopes: ' + JSON.stringify(requiredScopes));
const hasAllScope = requiredScopes.every((scope) => existingScopes.includes(scope));
if (!tokenObj.temp && !env_1.isCI && !hasAllScope) {
throw new error_1.default('Re-login required due to missing scopes!!', {
exit: 0,
errorId: 'AUTH-2',
arg: [(0, ansi_colors_1.bold)('catalyst login --force')]
});
}
runtime_store_1.default.set('auth_scopes', requiredScopes);
runtime_store_1.default.set('user', config_store_1.default.get(`${activeDC}.user`));
runtime_store_1.default.set('credential', credential_1.default.initToken(tokenObj.token, tokenObj.temp));
};