'use strict';

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    pm2 = require('pm2');

var m = require('../common.js');
var buildConfig = m.config.buildConfig;

var serverTask =  function () {
    pm2.connect(true, function (err) {
        if (err) {
            gutil.log(gutil.colors.red('Error in pm2 connection: ' + err));
            process.exit(2);
        }

        const config = {
          name: "formbird-server",
          script: buildConfig.serverDir + "/ServerApp.js",
          "node_args": ["--inspect=" + m.config.debugPort],
          "ignore_watch": [
            "node_modules",
            "client",
            "server/public",
            "server/**/.DS_Store",
            "server/test",
            "server/log",
            "server/uploadDir"
          ],
          "watch": ["server"]
        };

        pm2.start(config, function () {
            pm2.streamLogs(config.name, 0, null, null, null);
        });
    });
};

m.serverTask = serverTask;

// start the server without building the client, for when developing server only components
gulp.task('server',
    function(done){
        serverTask();
        done();
    });
