"use strict";
/**
 * Copyright © 2018 Emu Analytics
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const create_app_1 = require("../create-app/create-app");
const starters_1 = require("../create-app/starters");
const inquirer = require("inquirer");
const node_fetch_1 = require("node-fetch");
const download_1 = require("../create-app/download");
exports.command = ['new [name]'];
exports.desc = 'Create a new Flo.w app';
exports.builder = (args) => {
    return args
        .option('s', {
        alias: 'starter',
        describe: 'Starter template',
        type: 'string'
    })
        .positional('name', { describe: 'Project name', type: 'string' });
};
function handler(params) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            // Fetch starter index
            const starters = yield node_fetch_1.default(`${download_1.STARTER_BASE_URL}/index.json`).then(res => res.json());
            if (!params.starter) {
                const answers = yield inquirer.prompt([
                    {
                        type: 'list',
                        name: 'starter',
                        message: 'Starter template',
                        choices: starters.map(s => s.name),
                        default: starters[0].name
                    }
                ]);
                params.starter = answers.starter;
            }
            const starterRepo = starters_1.getStarterRepo(params.starter, starters);
            yield create_app_1.createApp(starterRepo, params.name, false);
        }
        catch (e) {
            utils_1.logError(e.message);
        }
    });
}
exports.handler = handler;
//# sourceMappingURL=new.js.map