async function processConnectDispatch()

in packages/MSBot/src/msbot-connect-dispatch.ts [75:136]


async function processConnectDispatch(config: BotConfiguration): Promise<BotConfiguration> {
    args.name = args.hasOwnProperty('name') ? args.name : config.name;

    if (args.stdin) {
        Object.assign(args, JSON.parse(await getStdin()));
    } else if (args.input != null) {
        Object.assign(args, await fs.readJSON(<string>args.input));
    }

    if (!args.hasOwnProperty('name')) {
        throw new Error('Bad or missing --name');
    }

    if (!args.appId || !uuidValidate(args.appId)) {
        throw new Error('bad or missing --appId');
    }

    if (!args.version) {
        throw new Error('bad or missing --version');
    }
    args.version = args.version.toString();

    if (!args.authoringKey || !uuidValidate(args.authoringKey)) {
        throw new Error('bad or missing --authoringKey');
    }

    if (args.subscriptionKey && !uuidValidate(args.subscriptionKey)) {
        throw new Error('bad --subscriptionKey');
    }

    if (args.ids && args.ids.length > 0) {
        args.serviceIds = args.ids.split(',');
    }

    if (args.services) {
        let botConfig2 = BotConfiguration.fromJSON({ services: args.services });

        for (let service of botConfig2.services) {
            if (service.id) {
                if (!config.findService(service.id)) {
                    config.services.push(service);
                }
            }
        }
    }

    const newService = new DispatchService(<any>{
        name: args.name,
        appId: args.appId,
        authoringKey: args.authoringKey,
        subscriptionKey: args.subscriptionKey,
        version: args.version,
        region: args.region,
        serviceIds: args.serviceIds
    });

    // add the service
    const id: string = config.connectService(newService);
    await config.save(args.secret);
    await stdoutAsync(JSON.stringify(config.findService(id), null, 2));
    return config;
}