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

128 lines (127 loc) 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InitCommandService = void 0; const chalk = require("chalk"); const base_service_1 = require("../common/base.service"); const access_manager_1 = require("./access-manager"); const inquirer = require("inquirer"); const os = require("os"); const node_machine_id_1 = require("node-machine-id"); const errors_1 = require("../common/errors"); const cli_ux_1 = require("cli-ux"); const setup_service_1 = require("./setup.service"); const defaults_1 = require("../common/defaults"); class InitCommandService extends base_service_1.BaseService { constructor(configManager) { super(configManager); this.accessManager = new access_manager_1.AccessManager(configManager); this.setupService = new setup_service_1.SetupCommandService(configManager); } async getMachineID() { return await (0, node_machine_id_1.machineId)(); } async initialize({ args, flags }) { const { email } = await inquirer.prompt([ { type: "input", name: "email", message: "Please enter your email address", }, ]); console.log("Creating Your Onboardbase Account..."); console.log("Creating Your Organisation..."); const hostname = os.hostname(); const hostARCH = os.arch(); const fingerprint = await this.getMachineID(); const hostOS = os.platform(); const { pollingCode, authCode } = await this.httpInstance.generateAuthCode(fingerprint, hostOS, hostname, hostARCH); const name = email.substring(0, email.lastIndexOf("@")); const domain = email.substring(email.lastIndexOf("@") + 1); if (!name || !domain) { throw new errors_1.BadInputError("Invalid Email Address"); } const teamName = domain.split(".")[0]; const orgName = `${name}_${teamName}`; await this.httpInstance.signup({ name, email, teamName: orgName, authCode, }); cli_ux_1.cli.action.stop(`Organization: ${orgName} Created Successfully`); console.log(chalk.greenBright("Account Created Successfully. Please check you email for verification")); // const allConfigs = ConfigManager.getConfigs(); let newConfig = { scope: process.cwd(), token: undefined, dashboardHost: this.getConfigForCurrentScope("dashboard-host") || "https://app.onboardbase.com", }; cli_ux_1.cli.action.start("Waiting for email verification..."); const pollingInterval = 4000; // 4secs const pollingTimeout = 300000; // 5mins let authTokenResponse = await this.httpInstance.getAuthToken(pollingCode); let isAuthenticated = false; /* Re use the polling and interval process from login */ if (authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.error) { let intervalHandler; intervalHandler = setInterval(async () => { var _a; if (!isAuthenticated) { authTokenResponse = await this.httpInstance.getAuthToken(pollingCode); if (!(authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.error)) { isAuthenticated = true; clearInterval(intervalHandler); const { token } = authTokenResponse === null || authTokenResponse === void 0 ? void 0 : authTokenResponse.verifyAuthCode; newConfig.token = token; this.configManager.setDefaultGlobalConfig({ scope: newConfig.scope, content: { token, password: flags.password || "", requirePassword: Boolean(flags.password), "api-host": defaults_1.DEFAULT_API_GRAPHQL_ENDPOINT, "dashboard-host": defaults_1.DEFAULT_DASHBOARD_HOST, requirePasswordForCurrentSession: Boolean(flags.password), merged: false, "auth-result": authTokenResponse, projectConfigLastModified: 0, }, }); cli_ux_1.cli.action.stop(); console.log(`${chalk.green("√")} ${chalk.bold(chalk.gray("Verification Complete. You are now logged in"))}`); const { projectTitle } = await inquirer.prompt([ { type: "input", name: "projectTitle", message: "Please Enter A Project Name", }, ]); await this.httpInstance.createProject(authTokenResponse.accessToken, projectTitle, "created with init command"); cli_ux_1.cli.action.start("Provisioning Your Environment..."); const projects = await this.httpInstance.fetchProjects(authTokenResponse.accessToken); const environment = (_a = projects .find(({ title }) => projectTitle.toLowerCase() === title)) === null || _a === void 0 ? void 0 : _a.environments.list.map(({ title }) => title); cli_ux_1.cli.action.stop(); console.log(chalk.greenBright(`Environment with the name ${chalk.bold.green(`${environment === null || environment === void 0 ? void 0 : environment[0]}`)} created successfully.`)); await this.setupService.initialize({ flags: { project: projectTitle, environment: environment === null || environment === void 0 ? void 0 : environment[0] }, args: {}, }); console.log(`${chalk.greenBright("Start your project with onboardbase run “start command”")}`); console.log(`${chalk.gray(`Check out your account at: ${newConfig.dashboardHost}`)}`); clearTimeout(intervalTimeout); } } }, pollingInterval); const intervalTimeout = setTimeout(() => { clearInterval(intervalHandler); //it is not an error, so, just log it with red chalk throw new errors_1.UnexpectedError(chalk.bold.red("Authentication Timeout exceeded...")); }, pollingTimeout); } } } exports.InitCommandService = InitCommandService;