"use strict";
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 cli_core_1 = require("@igniteui/cli-core");
const path = require("path");
const resolve = require("resolve");
const build_1 = require("./build");
const execSyncNpmStart = (port, options) => {
    if (port) {
        cli_core_1.Util.execSync(`npm start -- --port=${port}`, options);
        return;
    }
    cli_core_1.Util.execSync(`npm start`, options);
};
let command;
// tslint:disable:object-literal-sort-keys
command = {
    command: "start",
    desc: "starts the project",
    templateManager: null,
    builder: {
        port: {
            alias: "p",
            describe: "serve app port",
            type: "number"
        }
    },
    execute(argv) {
        return __awaiter(this, void 0, void 0, function* () {
            cli_core_1.GoogleAnalytics.post({
                t: "screenview",
                cd: "Start"
            });
            command.start(argv);
        });
    },
    start(argv) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!cli_core_1.ProjectConfig.hasLocalConfig()) {
                cli_core_1.Util.error("Start command is supported only on existing project created with igniteui-cli", "red");
                return;
            }
            //build
            yield build_1.default.build({});
            const config = cli_core_1.ProjectConfig.getConfig();
            const framework = config.project.framework;
            const projectType = config.project.projectType;
            const defaultPort = config.project.defaultPort;
            cli_core_1.Util.log(`Starting project.`, "green");
            cli_core_1.GoogleAnalytics.post({
                t: "event",
                ec: "$ig start",
                cd1: framework,
                cd2: projectType,
                cd11: !!config.skipGit,
                cd14: config.project.theme
            });
            let port = Number(argv.port) || defaultPort;
            // TODO: consider piping the stdin so that we handle the cp's termination
            // this may require additional logic to be implemented if the cp asks for input
            const options = { stdio: "inherit", killSignal: "SIGINT" };
            switch (framework.toLowerCase()) {
                case "jquery":
                    // browser-sync installed per project
                    const bs = require(resolve.sync("browser-sync", { basedir: process.cwd() }));
                    const browserSync = bs.create("igniteui-cli");
                    const filePath = path.join(process.cwd(), "bs-config.js");
                    const bsConfig = require(filePath);
                    if (port) {
                        bsConfig.port = port;
                    }
                    browserSync.init(bsConfig);
                    break;
                case "react":
                    if (port) {
                        if (projectType === "igr-ts") {
                            execSyncNpmStart(port, options);
                        }
                        else {
                            // https://facebook.github.io/create-react-app/docs/advanced-configuration
                            // react-scripts start "--port=dafaultPort" is not a valid command for all environments.
                            // .env file is included and used by both igr-es6 and es6 now,
                            // to specify the port for all environments (Windows, Mac, etc)
                            process.env.PORT = `${port}`;
                            port = null;
                        }
                    }
                /* falls through */
                case "angular":
                case "webcomponents":
                    execSyncNpmStart(port, options);
                default:
                    break;
            }
        });
    }
};
exports.default = command;
