feature(pawn) add

This commit is contained in:
coderaiser 2014-08-22 09:28:35 -04:00
parent 67d7cd2290
commit 5deaab7283
2 changed files with 69 additions and 51 deletions

View file

@ -8,7 +8,6 @@
path = require('path'),
child_process = require('child_process'),
exec = child_process.exec,
spawn = child_process.spawn,
Util = require(DIR + 'util'),
CloudFunc = require(DIR + 'cloudfunc'),
@ -17,6 +16,7 @@
update = require(DIR_SERVER + 'update'),
socket = require(DIR_SERVER + 'socket'),
find = require(DIR_SERVER + 'find'),
pawn = require(DIR_SERVER + 'pawn'),
mainpackage = require(DIR_ROOT + 'package'),
CLOUDCMD = mainpackage.name,
@ -201,57 +201,23 @@
}
function setSpawn(сommand, options, callback) {
var cmd, error,
isSended = false,
args = сommand.split(' '),
send = function(error, data) {
isSended = true;
callback({
stderr: error,
stdout: data
});
},
sendError = function(error) {
isSended = true;
send(error, null);
};
сommand = args.shift();
error = Util.exec.tryLog(function() {
cmd = spawn(сommand, args, options);
pawn(сommand, options, function(error, stderr, stdout) {
var errorStr = '';
Util.log(error);
if (error)
errorStr = error.message;
else if (stderr)
errorStr = addNewLine(stderr);
errorStr = addNewLine(errorStr);
callback({
stderr: errorStr,
stdout: stdout
});
});
if (error) {
sendError(error);
} else {
cmd.stderr.setEncoding('utf8');
cmd.stdout.setEncoding('utf8');
cmd.stdout.on('data', function(data) {
send(null, data);
});
cmd.stderr.on('data', function(error) {
sendError(error);
});
cmd.on('error', function(error) {
var errorStr = addNewLine(error + '');
Util.log(error);
sendError(errorStr);
});
cmd.on('close', function() {
cmd = null;
if (!isSended)
send(null, null);
});
}
}
function onCloudCmd(command, callback) {

52
lib/server/pawn.js Normal file
View file

@ -0,0 +1,52 @@
(function() {
'use strict';
var child_process = require('child_process'),
spawn = child_process.spawn,
DIR = '../',
Util = require(DIR + 'util');
module.exports = function(сommand, options, callback) {
var cmd, error,
isSended = false,
args = сommand.split(' '),
func = function(error, stderr, stdout) {
isSended = true;
callback(error, stderr, stdout);
};
сommand = args.shift();
error = Util.exec.tryLog(function() {
cmd = spawn(сommand, args, options);
});
if (error) {
callback(error);
} else {
cmd.stderr.setEncoding('utf8');
cmd.stdout.setEncoding('utf8');
cmd.stdout.on('data', function(data) {
func(null, null, data);
});
cmd.stderr.on('data', function(error) {
func(null, error);
});
cmd.on('error', function(error) {
func(error);
});
cmd.on('close', function() {
cmd = null;
if (!isSended)
func();
});
}
};
})();