UNPKG

@onboardbase/cli

Version:

[![Version](https://img.shields.io/npm/v/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![Downloads/week](https://img.shields.io/npm/dw/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![License](https://img

103 lines (102 loc) 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TeammateCommandService = void 0; const chalk = require("chalk"); const base_service_1 = require("../common/base.service"); const utils_1 = require("../utils"); const os = require("os"); const types_1 = require("../common/types"); const access_manager_1 = require("./access-manager"); const config_1 = require("../config"); const cli_ux_1 = require("cli-ux"); const setup_service_1 = require("./setup.service"); const defaults_1 = require("../common/defaults"); class TeammateCommandService extends base_service_1.BaseService { constructor(configManager) { super(configManager); this.accessManager = new access_manager_1.AccessManager(configManager); } async initialize({ flags }) { const { signupLink, name } = flags; if (!signupLink) { console.log(`${chalk.red("Please add the link sent to you by your admin.")}`); process.exit(1); } if (!(0, utils_1.isValidURL)(signupLink)) { console.log(`${chalk.red("Please enter a valid URL")}`); process.exit(1); } const hostname = os.hostname(); const hostARCH = os.arch(); const fingerprint = await (0, utils_1.getMachineID)(); const hostOS = os.platform(); const { pollingCode, authCode } = await this.httpInstance.generateAuthCode(fingerprint, hostOS, hostname, hostARCH); const joinUrl = new URL(signupLink); const pathUrl = joinUrl.pathname.split("/"); const confirmationCode = pathUrl[2]; const cliToken = this._getFromGlobalConfigOrThrow({ configPath: "token", envName: types_1.ENV_NAMES.TOKEN, }); const authHandshakeResult = await this.accessManager.getAuthInfoFromDeviceToken(cliToken); cli_ux_1.cli.action.start("Fetching Details About Your Account..."); const userId = await this.httpInstance.getTeamMateByCode(authHandshakeResult.accessToken, confirmationCode); cli_ux_1.cli.action.stop(); let newConfig = { scope: "/", token: undefined, }; const pollingInterval = 4000; // 4secs const pollingTimeout = 300000; // 5mins let authTokenResponse = await this.httpInstance.getAuthToken(pollingCode); let isAuthenticated = false; const dashboardHost = this.getConfigForCurrentScope("dashboard-host") || "https://app.onboardbase.com"; cli_ux_1.cli.action.start("Setting up Your Account..."); await this.httpInstance.teamMateSignup({ userId, name, authCode, confirmationCode, accessToken: authHandshakeResult.accessToken, }); if (authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.errors) { let intervalHandler; intervalHandler = setInterval(async () => { var _a; if (!isAuthenticated) { authTokenResponse = await this.httpInstance.getAuthToken(pollingCode); if (!(authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.errors)) { isAuthenticated = true; clearInterval(intervalHandler); const { token } = (_a = authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.data) === null || _a === void 0 ? void 0 : _a.verifyAuthCode; newConfig.token = token; this.setConfigForCurrentScope("dashboard-host", dashboardHost); this.setConfigForCurrentScope("api-host", defaults_1.DEFAULT_API_HOST); this.setConfigForCurrentScope("token", token); this.setConfigForCurrentScope("password", undefined); this.setConfigForCurrentScope("requirePassword", false); this.setConfigForCurrentScope("requirePasswordForCurrentSession", false); cli_ux_1.cli.action.stop(); const setupService = new setup_service_1.SetupCommandService(new config_1.ConfigManager()); await setupService.initialize({ flags: {}, args: {}, }); console.log(`${chalk.greenBright("Start your project with onboardbase run “start command”")}`); console.log(`${chalk.gray(`Check out your account at: https://app.onboardbase.com`)}`); process.exit(1); } } }, pollingInterval); const intervalTimeout = setTimeout(() => { clearInterval(intervalHandler); clearTimeout(intervalTimeout); // statusBar.dispose(); console.log("Authentication Timeout exceeded"); process.exit(1); }, pollingTimeout); } } } exports.TeammateCommandService = TeammateCommandService;