UNPKG

@amplience/dc-cli

Version:
121 lines (120 loc) 5.58 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addHub = exports.validateHub = exports.CONFIG_PATH = void 0; const path_1 = require("path"); const fs_extra_1 = __importDefault(require("fs-extra")); const chalk_1 = __importDefault(require("chalk")); const configure_1 = require("../commands/configure"); const dynamic_content_client_factory_1 = __importDefault(require("../services/dynamic-content-client-factory")); const question_helpers_1 = require("./question-helpers"); const { AutoComplete, Input, Password } = require('enquirer'); exports.CONFIG_PATH = (0, path_1.join)(process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME'] || __dirname, '.amplience', 'hubs.json'); const validateHub = async (creds) => { const client = (0, dynamic_content_client_factory_1.default)(creds); const hub = await client.hubs.get(creds.hubId); return { ...creds, name: hub.name }; }; exports.validateHub = validateHub; const getHubs = () => { const activeHub = fs_extra_1.default.readJSONSync((0, configure_1.CONFIG_FILENAME)()); fs_extra_1.default.mkdirpSync((0, path_1.dirname)(exports.CONFIG_PATH)); if (!fs_extra_1.default.existsSync(exports.CONFIG_PATH)) { fs_extra_1.default.writeFileSync(exports.CONFIG_PATH, JSON.stringify([]), { encoding: 'utf-8' }); } const hubs = fs_extra_1.default.readJSONSync(exports.CONFIG_PATH, { encoding: 'utf-8' }); return hubs.map((hub) => { const obj = { ...hub, isActive: activeHub.hubId === hub.hubId }; return obj; }); }; const saveHub = (hub) => { const hubs = [hub, ...getHubs().filter(h => h.hubId !== hub.hubId)]; fs_extra_1.default.writeFileSync(exports.CONFIG_PATH, JSON.stringify(hubs, undefined, 4), { encoding: 'utf-8' }); }; const activateHub = (creds) => { (0, configure_1.handler)({ ...creds, config: (0, configure_1.CONFIG_FILENAME)(), _: [], $0: '' }); console.log(`${chalk_1.default.green.bold('using')} hub [ ${chalk_1.default.green(creds.name || '')} ]`); return creds; }; const ask = async (message) => await new Input({ message }).run(); const secureAsk = async (message) => await new Password({ message }).run(); const helpTag = (message) => chalk_1.default.gray(`(${message})`); const sectionHeader = (message) => console.log(`\n${message}\n`); const dcTag = chalk_1.default.bold.cyanBright('dynamic content'); const credentialsHelpText = helpTag('credentials assigned by Amplience support'); const hubIdHelpText = helpTag('found in hub settings -> properties'); const addHub = async (args) => { sectionHeader(`${dcTag} configuration ${credentialsHelpText}`); const usePAT = await (0, question_helpers_1.asyncQuestion)('Would you like to use a PAT Token? (y/n)\n'); if (usePAT) { args.patToken = args.patToken || (await secureAsk(`PAT ${chalk_1.default.magenta('token')}:`)); } else { args.clientId = args.clientId || (await ask(`client ${chalk_1.default.magenta('id')}:`)); args.clientSecret = args.clientSecret || (await secureAsk(`client ${chalk_1.default.magenta('secret')}:`)); } args.hubId = args.hubId || (await ask(`hub id ${hubIdHelpText}:`)); if (args.clientId && getHubs().find(hub => args.clientId === hub.clientId && args.hubId === hub.hubId)) { throw new Error(`config already exists for client id [ ${args.clientId} ] and hub id [ ${args.hubId} ]`); } if (usePAT && getHubs().find(hub => args.patToken === hub.patToken && args.hubId === hub.hubId)) { throw new Error(`config already exists for PAT Token and hub id [ ${args.hubId} ]`); } const validated = await (0, exports.validateHub)(args); if (validated && validated.name) { saveHub(validated); console.log(`${chalk_1.default.blueBright('added')} hub [ ${chalk_1.default.green(validated.name)} ]`); await activateHub(validated); } }; exports.addHub = addHub; const chooseHub = async (filter, exact = false) => { const filtered = getHubs().filter(hub => (hub.name && hub.name.indexOf(filter) > -1) || (exact ? hub.hubId === filter : hub.hubId.indexOf(filter) > -1)); if (filtered.length === 1) { return filtered[0]; } if (filtered.length === 0) { throw new Error(`hub configuration not found for filter [ ${chalk_1.default.red(filter)} ]`); } const hubWithId = await new AutoComplete({ name: 'hub', message: `choose a hub`, limit: filtered.length, multiple: false, choices: filtered.map(hub => `${hub.hubId} ${hub.name}`), initial: filtered.findIndex(hub => hub.isActive) }).run(); return await chooseHub(hubWithId.split(' ')[0], true); }; const useHub = async (argv) => { const hubConfig = await chooseHub(argv.hub); return await activateHub(hubConfig); }; const listHubs = () => { getHubs().forEach(hub => { var _a, _b; const hubName = hub.isActive ? chalk_1.default.green.bold(`* ${hub.name}`) : ` ${hub.name}`; console.log(`${hub.hubId} ${((_a = hub.clientId) === null || _a === void 0 ? void 0 : _a.substring(0, 8)) || ((_b = hub.patToken) === null || _b === void 0 ? void 0 : _b.substring(0, 8))} ${hubName}`); }); }; exports.default = { getHubs, addHub: exports.addHub, listHubs, useHub, validateHub: exports.validateHub };