{"size": 3743, "ext": "js", "max_stars_repo_path": "subdomainer.js", "max_stars_repo_name": "amirmamaghani/node-subdomainer", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "subdomainer.js", "max_issues_repo_name": "amirmamaghani/node-subdomainer", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "subdomainer.js", "max_forks_repo_name": "amirmamaghani/node-subdomainer", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.0594059406, "max_line_length": 103, "alphanum_fraction": 0.4838364948}
/**
 * Subdomainer
 *
 * @type {Function}
 */
var subdomainer = module.exports = function (settingsFile) {
    var fs = require('fs'),
        util = require('util');

    //read & parse settings file
    var settings = JSON.parse(fs.readFileSync(settingsFile));
    /**
     * Add line to /etc/hosts
     * @param subdomain
     * @returns {boolean}
     */
    var addHostline = function (subdomain) {
        var hostLine = util.format('\n%s\t%s.%s\n', settings.hostname, subdomain, settings.domain);
        fs.appendFile(settings.etcHosts.file, hostLine, function (err) {
            console.error(err);
            return false;
        });
        console.log("Added to "+ settings.etcHosts.file + ":\n" + hostLine+"\n");
        return true;
    };

    /**
     * Add Virtual host to apache file
     * @param subdomain
     * @param params
     */
    var addVhost = function (subdomain, params) {
        var tagReplacer = function (string) {
            string = string
                .replace("{subdomain}", subdomain)
                .replace("{domain}", settings.domain);
            //@TODO possibly add more params
            return string;
        };
        var block = function (blockname, blockValue, params) {
            blockValue = tagReplacer(blockValue);
            var _block = util.format('\n<%s "%s">{{params}}</%s>\n', blockname, blockValue, blockname);
            var _params = "\n";
            if (typeof params._value !== 'undefined')
                delete params._value;
            for (var param in params) {
                if (params.hasOwnProperty(param)) {
                    var placeholder = "%s ";
                    if (typeof params[param] === "string") {
                        switch (param) {
                            case "DocumentRoot":
                                placeholder += '"%s"';
                                break;
                            case "ServerName":
                                placeholder += '%s';
                                break;
                            //@TODO add another params with special formatting
                            default :
                                placeholder += "%s"; //default formatting
                        }
                        params[param] = tagReplacer(params[param]);
                        _params += "\t" + util.format(placeholder, param, params[param]) + "\n";
                    } else if (Object.prototype.toString.call(params[param]) === '[object Array]') {
                        for (var p in params[param]) {
                            _params += block(param, params[param][p]._value, params[param][p]);
                        }
                    } else {
                        _params += block(param, params[param]._value, params[param]);
                    }
                }
            }
            _block = _block.replace("{{params}}", _params);
            return _block;
        };
        var virtualHost = tagReplacer(settings.vconf.VirtualHost);
        var vhost = block("VirtualHost", virtualHost, settings.vconf.params);
        console.log(vhost);
        fs.appendFile(settings.vconf.file, vhost, function (err) {
            console.error(err);
            return false;
        });
        console.log("Added to "+ settings.vconf.file + ":\n" + vhost + "\n");
        return true;
    };

    /**
     * Add another subdomain
     * @param subdomain
     */
    this.addSubdomain = function (subdomain) {
        // Append line to /etc/hosts
        var completed = 0;
        if (addHostline(subdomain)) completed++;
        if (addVhost(subdomain)) completed++;

        if (completed == 2)
            return true;
        else return false;
    };
};
