/*!
 * ndev-framework
 * Copyright(c) 2016-2017 Javanile.org
 * MIT Licensed
 */

var cwd  = process.cwd();
var base = require("path").basename;
var exec = require("child_process").exec;
var util = require("./util");

module.exports = {
    /**
     *
     * @param args
     */
    cmdTest: function (args, callback) {
        this.cmdWithNdevModule("test", args, callback);
    },

    /**
     *
     *
     * @param args
     */
    cmdClone: function (args) {
        if (!args[1]) {
            console.error("(ndev) Required repository url or package name.");
            return;
        }

        //
        if (!args[1].match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)) {
            var pack = args[1];
            var name = args[2] ? args[2] : base(repo, ".git");
            exec(__dirname + "/../exec/ndev-clone-pack.sh " + path + " " + repo + " " + name,
                function (error, stdout, stderr) {
                    console.log("(ndev)", stderr.trim());
                }
            );
        }

        //
        var repo = args[1];
        var name = args[2] ? args[2] : basename(repo, ".git");
        exec(__dirname + "/../exec/ndev-clone.sh " + path + " " + repo + " " + name,
            function (error, stdout, stderr) {
                console.log(stderr.trim());
            }
        );
    },

    /**
     *
     *
     * @param args
     */
    cmdMount: function (args) {
        if (!args[1]) {
            console.error("(ndev) Required node module to mount.");
            return;
        }

        // cant start

        //
        var node = args[1].trim();
        exec(__dirname + "/../exec/ndev-mount.sh " + path + " " + node,
            function (error, stdout, stderr) {
                i(stderr.trim());
            }
        );
    },

    /**
     *
     * @param args
     */
    cmdInstall: function (args) {
        if (!args[1]) {
            return e("Required package name.");
        }

        //
        console.log("(ndev) Please wait during install...");
        exec(join(__dirname, + "/../exec/ndev-install.sh ")  + path + " " + args.slice(1).join(" "),
            function (error, stdout, stderr) {
                console.log(!stderr ? stdout.trim() : stderr.trim());
            }
        );
    },

    /**
     *
     * @param args
     */
    cmdFreeze: function (args, callback) {
        this.cmdWithNdevModule("freeze", args, callback);
    },

    /**
     *
     * @param args
     */
    cmdPublish: function (args) {
        if (!args[1]) {
            return e("Required ndev module to publish.");
        }

        i("Please wait during publish...");
        var name = args[1];
        exec(__dirname + "/../exec/ndev-publish.sh " + path + " " + name,
            function (error, stdout, stderr) {
                console.log(stderr.trim(), stdout);
            }
        );
    },

    /**
     *
     * @param args
     */
    cmdCommit: function (args) {
        if (!args[1]) {
            return e("Required ndev module to commit.");
        }

        i("Please wait during commit...");
        var name = args[1].trim();
        exec(__dirname + "/../exec/ndev-commit.sh " + path + " " + name,
            function (error, stdout, stderr) {
                e(stderr.trim());
            }
        );
    }

    /**
     *
     * @param cmd
     * @param args
     * @param callback
     */
    cmdWithNdevModule: function (cmd, args, callback) {
        if (!args[0]) { return util.err("&ndev-required"); }

        util.exec("freeze", [cwd, args[0]], function(resp) {
            callback(util.log(resp));
        });
    }
}
@@ -313,32 +313,39 @@
 back) %7B%0A        
+return 
 this.cmdWithNdev
@@ -2392,16 +2392,23 @@
         
+return 
 this.cmd
@@ -3329,24 +3329,25 @@
     );%0A    %7D
+,
 %0A%0A    /**%0A  
@@ -3555,24 +3555,19 @@
 il.exec(
-%22freeze%22
+cmd
 , %5Bcwd, 

version 0.0.16