UNPKG

msbot

Version:

MSBot command line tool for manipulating Microsoft Bot Framework .bot files

106 lines 4.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright(c) Microsoft Corporation.All rights reserved. * Licensed under the MIT License. */ // tslint:disable:no-console // tslint:disable:no-object-literal-type-assertion const botframework_config_1 = require("botframework-config"); const chalk = require("chalk"); const program = require("commander"); const txtfile = require("read-text-file"); const validurl = require("valid-url"); const stdioAsync_1 = require("./stdioAsync"); const utils_1 = require("./utils"); program.Command.prototype.unknownOption = (flag) => { console.error(chalk.default.redBright(`Unknown arguments: ${flag}`)); showErrorHelp(); }; program .name('msbot connect endpoint') .description('Connect the bot to an endpoint') .option('-n, --name <name>', 'name of the endpoint') .option('-e, --endpoint <endpoint>', 'url for the endpoint\n') .option('-a, --appId <appid>', '(OPTIONAL) Microsoft AppId used for auth with the endpoint') .option('-p, --appPassword <password>', '(OPTIONAL) Microsoft app password used for auth with the endpoint') .option('-b, --bot <path>', 'path to bot file. If omitted, local folder will look for a .bot file') .option('--input <jsonfile>', 'path to arguments in JSON format { id:\'\',name:\'\', ... }') .option('--secret <secret>', 'bot file secret password for encrypting service secrets') .option('--stdin', 'arguments are passed in as JSON object via stdin') .option('--prefix', 'Append [msbot] prefix to all messages') .action((cmd, actions) => undefined); const command = program.parse(process.argv); const args = {}; Object.assign(args, command); if (args.stdin) { //force verbosity output if args are passed via stdin process.env.PREFIX = 'prefix'; } if (process.argv.length < 3) { showErrorHelp(); } else { if (!args.bot) { botframework_config_1.BotConfiguration.loadBotFromFolder(process.cwd(), args.secret) .then(processConnectEndpointArgs) .catch((reason) => { console.error(chalk.default.redBright(reason.toString().split('\n')[0])); showErrorHelp(); }); } else { botframework_config_1.BotConfiguration.load(args.bot, args.secret) .then(processConnectEndpointArgs) .catch((reason) => { console.error(chalk.default.redBright(reason.toString().split('\n')[0])); showErrorHelp(); }); } } async function processConnectEndpointArgs(config) { if (args.stdin) { Object.assign(args, JSON.parse(await utils_1.getStdin())); } else if (args.input != null) { Object.assign(args, JSON.parse(await txtfile.read(args.input))); } if (!args.endpoint) { throw new Error('missing --endpoint'); } if (!validurl.isHttpUri(args.endpoint) && !validurl.isHttpsUri(args.endpoint)) { throw new Error(`--endpoint ${args.endpoint} is not a valid url`); } if (args.appId && !utils_1.uuidValidate(args.appId)) { throw new Error('--appId is not valid'); } if (args.appPassword && args.appPassword.length === 0) { throw new Error('zero length --appPassword'); } if (!args.hasOwnProperty('name')) { if (args.appId) { args.name = `${args.endpoint} - ${args.appId}`; } else { args.name = args.endpoint; } } const newService = new botframework_config_1.EndpointService({ name: args.name, appId: (args.appId && args.appId.length > 0) ? args.appId : '', appPassword: (args.appPassword && args.appPassword.length > 0) ? args.appPassword : '', endpoint: args.endpoint }); const id = config.connectService(newService); await config.save(args.secret); await stdioAsync_1.stdoutAsync(JSON.stringify(config.findService(id), null, 2)); return config; } function showErrorHelp() { program.outputHelp((str) => { console.error(str); return ''; }); process.exit(1); } //# sourceMappingURL=msbot-connect-endpoint.js.map