#!/usr/bin/env node
"use strict";
exports.__esModule = true;
var execute = require("cross-spawn");
var pm2Config = require("../pm2.config");
var port = pm2Config.serveUIStatic.env.PM2_SERVE_PORT;
var startCommand = ['run', 'prod'];
var program = 'npmfrog';
var firstArg = process.argv[2];
var allowedCliCommands = [
    {
        command: 'stop',
        script: 'stop'
    },
    {
        command: 'logs',
        script: 'logs'
    },
    {
        command: 'status',
        script: 'ps'
    },
];
var foundAllowedCommand = allowedCliCommands.find(function (element) { return element.command === firstArg; });
var command = foundAllowedCommand ? ['run', foundAllowedCommand.script] : startCommand;
var run = execute("npm", command, { cwd: __dirname, env: process.env });
run.stdout.on('data', function (data) {
    console.log(data.toString());
});
run.stderr.on('data', function (data) {
    console.error(data.toString());
});
run.on('exit', function (code) {
    if (code === 0) {
        if (command === startCommand) {
            console.log("Running npmFrog in background on http://localhost:" + port);
            console.log("To stop npmFrog, run `" + program + " stop`");
            console.log("Logs can be found in " + pm2Config.runServer.log + " and " + pm2Config.serveUIStatic.log + " .\n        Or run `npmfrog logs` to get all live logs.");
        }
        else if (command[1] === 'stop') {
            console.log("Stopped npmFrog.");
        }
    }
});
