// tslint:disable-next-line:no-require-imports
import open = require("open");

const generateQuery = (params: { [key: string]: string }) =>
    Object.keys(params)
        .map((key: string) => key + "=" + params[key])
        .join("&");

function prompt(question: string): Promise<string> {
    return new Promise((resolve, reject) => {
        const stdin = process.stdin;
        const stdout = process.stdout;

        stdin.resume();
        stdout.write(question + " ");

        stdin.once("data", data => {
            resolve(data.toString().trim());
        });
    });
}

prompt("Client ID?").then(clientId => {
    prompt("Product Id?").then(productId => {
        prompt("Redirect URI (allowed return URL)?").then(redirectURI => {
            const scopeData = {
                "alexa:all": {
                    productID: productId,
                    productInstanceAttributes: {
                        deviceSerialNumber: 123, // can be anything
                    },
                },
            };
            const getParams = generateQuery({
                client_id: clientId,
                scope: "alexa:all",
                scope_data: JSON.stringify(scopeData),
                response_type: "code",
                redirect_uri: redirectURI,
            });

            const authUrl = `https://www.amazon.com/ap/oa?${getParams}`;

            open(authUrl);
            process.exit();
        });
    });
});

// tslint:disable-next-line:no-require-imports
import open = require("open");

const generateQuery = (params: { [key: string]: string }) =>
    Object.keys(params)
        .map((key: string) => key + "=" + params[key])
        .join("&");

function prompt(question: string): Promise<string> {
    return new Promise((resolve, reject) => {
        const stdin = process.stdin;
        const stdout = process.stdout;

        stdin.resume();
        stdout.write(question + " ");

        stdin.once("data", data => {
            resolve(data.toString().trim());
        });
    });
}

console.log("Welcome to the Amazon auth helper!");
console.log("I will need three things from you:");
console.log("- Client ID");
console.log("- Product ID");
console.log("- Allowed return URL");

prompt("[Press any key to continue]").then(() => {
    prompt("Client ID?").then(clientId => {
        prompt("Product Id?").then(productId => {
            prompt("Redirect URI (allowed return URL)?").then(redirectURI => {
                prompt(
                    "Great! Next I will now open a browser where you can authorize your Alexa product\n[Press any key to continue]",
                ).then(() => {
                    const scopeData = {
                        "alexa:all": {
                            productID: productId,
                            productInstanceAttributes: {
                                deviceSerialNumber: 123, // can be anything
                            },
                        },
                    };
                    const getParams = generateQuery({
                        client_id: clientId,
                        scope: "alexa:all",
                        scope_data: JSON.stringify(scopeData),
                        response_type: "code",
                        redirect_uri: redirectURI,
                    });

                    const authUrl = `https://www.amazon.com/ap/oa?${getParams}`;

                    open(authUrl);
                    process.exit();
                });
            });
        });
    });
});

Make script easier to understand
Make script easier to understand
