{"size": 1587, "ext": "js", "max_stars_repo_path": "src/cmds/pair.js", "max_stars_repo_name": "alistairg/hap-client-tool", "max_stars_repo_stars_event_min_datetime": "2017-06-17T23:52:21.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-24T21:28:44.000Z", "max_issues_repo_path": "src/cmds/pair.js", "max_issues_repo_name": "alistairg/hap-client-tool", "max_issues_repo_issues_event_min_datetime": "2017-06-18T00:03:45.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-24T21:28:27.000Z", "max_forks_repo_path": "src/cmds/pair.js", "max_forks_repo_name": "alistairg/hap-client-tool", "max_forks_repo_forks_event_min_datetime": "2017-06-17T23:44:30.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-07T10:06:14.000Z"}
{"max_stars_count": 25, "max_issues_count": 9, "max_forks_count": 7, "avg_line_length": 23.0, "max_line_length": 67, "alphanum_fraction": 0.3597983617}
import { Observable } from 'rxjs';
import Prompt from 'prompt';

import HapClient from 'hap-client';

const command = 'pair';
const desc = 'Attempt to connect to and pair with a HomeKit device'

function handler(args) {
    const client =
        new HapClient(
            args.client,
            args.address,
            args.port
        );

    client
        .pair(
            prompt('pin code')
        )
        .concat(
            client
            .verifyPairing()
        )
        .subscribe(
            () => { },
            e => {
                console.error(e)
                client.close();
            },
            () => {
                console.log('pairing complete');
                client.close();
            }
        );
}

function prompt(...labels) {
    return Observable
        .create(
            subscriber => {
                Prompt.start();
                Prompt.get(
                    labels,
                    function (err, result) {
                        if (err) subscriber.error(err);
                        else {
                            if (labels.length === 1) {
                                subscriber.next(result[labels[0]]);
                            }
                            else {
                                subscriber.next(result);
                            }
                            subscriber.complete();

                            Prompt.stop();
                        }
                    }
                );
            }
        );
}

export {
    command,
    desc,
    handler
}
