Esempio n. 1
0
Spawnify.prototype._set = function set(type, command, options) {
    let args, error, child;
    let result;
    
    assert(type, 'event could not be empty!');
    assert(command, 'command could not be empty!');
    assert(options, 'options could not be empty!');
    
    options.encoding = 'buffer';
    
    switch(type) {
    default:
        throw Error('type could be exec or spawn only!');
    
    case 'exec':
        result = tryCatch(exec, command, options);
        
        error = result[0];
        child = result[1];
        break;
    
    case 'spawn':
        args = command.split(' ');
        command = args.shift();
        
        result = tryCatch(spawn, command, args, options);
        
        error = result[0];
        child = result[1];
        break;
    }
    
    if (error) {
        if (error.code === 'ENOTDIR')
            error.message += ': ' + options.cwd;
        
        this._emit('error', newLine(error));
        this._emit('close');
        this._emit('exit');
    } else {
        /*
         * would be null
         * when options.stdio: 'inherit' used
         */
        this._setListeners(child);
        
        this.on('kill', (code) => {
            child.kill(code);
        });
        
        this.on('write', (data) => {
            child.stdin.write(data);
        });
        
        this._emit('start');
    }
};
Esempio n. 2
0
function createFile(filePath) {
    let error = tryCatch(readjson.sync, filePath);
    
    if (error && error.code === 'ENOENT') {
        error = tryCatch(writejson.sync, filePath, []);
        
        if (error)
            throw Error(error.message);
    }
}
Esempio n. 3
0
 function tryRequire(path) {
     var ret,
         error = tryCatch(function() {
             ret = require(path);
         });
     
     if (error && error.code !== 'MODULE_NOT_FOUND')
         console.error(error.message);
     
     return ret;
 }
Esempio n. 4
0
function getInfo(dir) {
    const infoPath = path.join(dir, 'package.json');
    
    const result = tryCatch(readjson.sync, infoPath);
    const [error] = result;
    const info = result[1];
    
    exitIfNotEntry(infoPath, error);
    
    Info(info);
    Directory(dir);
    
    return info;
}
Esempio n. 5
0
 module.exports = function(data, callback) {
     var error, dataOptimized;
     
     assert(data);
     assert(callback);
     
     if (!uglify)
         error = ErrorMsg;
     else
         error = tryCatch(function() {
             dataOptimized = uglify.minify(data, {fromString: true}).code;
         });
     
     callback(error, dataOptimized);
 };
Esempio n. 6
0
 module.exports = function(data, callback) {
     var error, errorParse, dataOptimized;
     
     assert(data);
     assert(callback);
     
     if (!Clean)
         error   = ErrorMsg;
     else
         error   = tryCatch(function() {
             var min         = new Clean().minify(data);
             dataOptimized   = min.styles;
             errorParse      = min.errors[0];
         });
     
     callback(error || errorParse, dataOptimized);
 };
Esempio n. 7
0
function readConfig(name) {
    if (!name)
        return;
    
    const fs = require('fs');
    const tryCatch = require('try-catch');
    const jju = require('jju');
    const forEachKey = require('for-each-key');
    
    const readjsonSync = (name) => jju.parse(fs.readFileSync(name, 'utf8'), {
        mode: 'json',
    });
    
    const [error, data] = tryCatch(readjsonSync, name);
    
    if (error)
        return exit(error.message);
    
    forEachKey(config, data);
}
Esempio n. 8
0
const {apiURL} = CloudFunc;
const changeEmitter = new Emitter();

const ConfigPath = path.join(DIR, 'json/config.json');
const ConfigHome = path.join(HOME, '.cloudcmd.json');

const readjsonSync = (name) => {
    return jju.parse(fs.readFileSync(name, 'utf8'), {
        mode: 'json',
    });
};

const rootConfig = readjsonSync(ConfigPath);
const key = (a) => Object.keys(a).pop();

const [error, configHome] = tryCatch(readjsonSync, ConfigHome);

if (error && error.code !== 'ENOENT')
    exit(`cloudcmd --config ${ConfigHome}: ${error.message}`);

const config = {
    ...rootConfig,
    ...configHome,
};

const connectionWraped = wraptile(connection);

module.exports = manage;
module.exports.save = save;
module.exports.middle = middle;
module.exports.subscribe = (fn) => {
Esempio n. 9
0
module.exports = (name) => {
    const result = tryCatch(require, name);
    const data = result[1];
    
    return data;
};
Esempio n. 10
0
(function() {
    'use strict';
    
    var DIR_SERVER  = __dirname     + '/',
        DIR_LIB     = DIR_SERVER    + '../',
        DIR         = DIR_SERVER    + '../../',
        
        path        = require('path'),
        
        exit        = require(DIR_SERVER    + 'exit'),
        CloudFunc   = require(DIR_LIB       + 'cloudfunc'),
        
        pipe        = require('pipe-io'),
        ponse       = require('ponse'),
        jonny       = require('jonny'),
        readjson    = require('readjson'),
        writejson   = require('writejson'),
        tryCatch    = require('try-catch'),
        exec        = require('execon'),
        criton      = require('criton'),
        
        HOME        = require('os-homedir')(),
        
        apiURL      = CloudFunc.apiURL,
        
        ConfigPath  = path.join(DIR, 'json/config.json'),
        ConfigHome  = path.join(HOME, '.cloudcmd.json'),
        
        error,
        config;
        
        error = tryCatch(function() {
            config = readjson.sync(ConfigHome);
        });
        
        if (error) {
            if (error.code !== 'ENOENT')
                console.error('cloudcmd --config ~/.cloudcmd.json:', error.message);
            
            error = tryCatch(function() {
                config = readjson.sync(ConfigPath);
            });
            
            if (error)
                exit('cloudcmd --config', ConfigPath + ':', error.message);
        }
        
    module.exports          = manage;
    module.exports.save     = save;
    module.exports.middle   = middle;
    module.exports.listen   = function(socket, authCheck) {
        if (!socket)
            throw Error('socket could not be empty!');
        
        if (authCheck && typeof authCheck !== 'function')
            throw Error('authCheck should be function!');
        
        listen(socket, authCheck);
        
        return middle;
    };
    
    function manage(key, value) {
        var result;
        
        if (key)
            if (value === undefined)
                result      = config[key];
            else
                config[key] = value;
        
        return result;
    }
    
    function save(callback) {
        writejson(ConfigHome, config, callback);
    }
    
    function listen(sock, authCheck) {
        var prefix = manage('prefix');
        
        sock.of(prefix + '/config')
            .on('connection', function(socket) {
                var connect = exec.with(connection, socket);
                
                exec.if(!manage('auth'), connect, function(fn) {
                    authCheck(socket, fn);
                });
            });
    }
    
    function connection(socket) {
        socket.emit('config', config);
        
        socket.on('message', function(json) {
            var data;
            
            if (typeof json !== 'object') {
                socket.emit('err', 'Error: Wrong data type!');
            } else {
                cryptoPass(json);
                
                data = traverse(json);
                
                save(function(error) {
                    if (error) {
                        socket.emit('err', error.message);
                    } else {
                        socket.broadcast.send(json);
                        socket.send(json);
                        socket.emit('log', data);
                    }
                });
            }
        });
    }
    
    function middle(req, res, next) {
        if (req.url !== apiURL + '/config') {
            next();
        } else {
            switch(req.method) {
            case 'GET':
                get(req, res, next);
                break;
            
            case 'PATCH':
                patch(req, res, next);
                break;
            
            default:
                next();
            }
        }
    }
    
    function get(req, res) {
        var data = jonny.stringify(config);
        
        ponse.send(data, {
            name    : 'config.json',
            request : req,
            response: res,
            cache   : false
        });
    }
    
    function patch(req, res, callback) {
        var options = {
            name    : 'config.json',
            request : req,
            response: res,
            cache   : false
        };
        
        pipe.getBody(req, function(error, body) {
            var data    = '',
                json    = jonny.parse(body) || {};
            
            if (error)
                callback(error);
            else
                cryptoPass(json);
            
            data = traverse(json);
            
            save(function(error) {
                if (error)
                    ponse.sendError(error, options);
                else
                    ponse.send(data, options);
            });
        });
    }
    
    function traverse(json) {
        var data;
        
        Object.keys(json).forEach(function(name) {
            data = CloudFunc.formatMsg('config', name);
            manage(name, json[name]);
        });
        
        return data;
    }
    
    function cryptoPass(json) {
        var algo = manage('algo');
        
        if (json && json.password)
            json.password = criton(json.password, algo);
    }
    
})();