UNPKG

@constructor-io/constructorio-connect-cli

Version:

CLI tool to enable users to interface with the Constructor Connect Ecosystem

113 lines (112 loc) 5.13 kB
"use strict"; 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getConnectToken = void 0; exports.getConnectTokenStore = getConnectTokenStore; const fs = __importStar(require("fs-extra")); const prompts_1 = require("@inquirer/prompts"); const core_1 = require("@oclif/core"); const kleur_1 = __importDefault(require("kleur")); const terminal_link_1 = __importDefault(require("terminal-link")); const obtain_token_request_1 = require("../http/obtain-token-request"); const render_tip_1 = require("../rendering/render-tip"); /** * Gets the Connect Auth token that should be used for the API requests to the Connect API. * First attempts to read from the environment variable CONNECT_AUTH_TOKEN. If it is not present * it will prompt the user for the token instead. If `canSave` is true, it will also prompt the user * if they want to save the token to the .env file. * * @param canSave Whether this is being called from a state where a token * received by prompt can be saved to the .env file. For example, when running during * the init command the repository does not yet exist and so there is nowhere to save the * token when this function runs. * @returns The connect auth token that should be used for the API requests. */ function getConnectTokenStore() { let connectToken; return async function getConnectToken(canSave = true) { if (connectToken) { return connectToken; } if (process.env.CONNECT_AUTH_TOKEN) { connectToken = process.env.CONNECT_AUTH_TOKEN; return connectToken; } renderAuthHelperText(); core_1.ux.stdout(); const apiKey = await (0, prompts_1.input)({ message: "Enter your Index key 🔑" }); const apiToken = await (0, prompts_1.password)({ message: "Enter your API token 🔐", mask: true, }); connectToken = await (0, obtain_token_request_1.obtainToken)({ api_key: apiKey, api_token: apiToken, }); if (canSave) { const saveToken = await (0, prompts_1.confirm)({ message: "💾 Do you want to save this token in your .env for future use?", }); if (saveToken) { fs.appendFileSync(".env", `CONNECT_AUTH_TOKEN=${connectToken}\n`); } core_1.ux.stdout(); } return connectToken; }; } exports.getConnectToken = getConnectTokenStore(); function renderAuthHelperText() { core_1.ux.stdout([ "Working with this CLI requires you to have a Constructor Index Key and Constructor API Token.", `If you have any questions, please reach out over at partners@constructor.io.`, ].join("\n")); core_1.ux.stdout(""); (0, render_tip_1.renderTip)([ `The ${kleur_1.default.bold("Index Key")} should follow this format: key_xxxxxxxxxxxxxxxx`, ]); (0, render_tip_1.renderTip)([ `The ${kleur_1.default.bold("API Token")} should follow this format: tok_xxxxxxxxxxxxxxxx`, ]); (0, render_tip_1.renderTip)([ `You can obtain an Index key and API Token from the customer dashboard under the ${(0, terminal_link_1.default)(kleur_1.default.bold("Integration > API Integration"), "https://app.constructor.io/dashboard/integration/api_tokens")} tab.`, ], { emoji: "🔗" }); (0, render_tip_1.renderTip)([ `For more information about authentication, refer to ${(0, terminal_link_1.default)(kleur_1.default.bold("the documentation"), "https://docs.constructor.com/reference/main-authentication")}.`, ], { emoji: "📚" }); }