mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-20 01:47:35 +00:00
feature(spawnify) exec, cd -> spawnify, rm onCloudCmd
This commit is contained in:
parent
78cd074370
commit
38f7e0b94b
2 changed files with 241 additions and 292 deletions
|
|
@ -2,46 +2,22 @@
|
|||
'use strict';
|
||||
|
||||
var DIR = '../',
|
||||
DIR_ROOT = DIR + '../',
|
||||
DIR_SERVER = './',
|
||||
|
||||
path = require('path'),
|
||||
child_process = require('child_process'),
|
||||
exec = child_process.exec,
|
||||
|
||||
Util = require(DIR + 'util'),
|
||||
CloudFunc = require(DIR + 'cloudfunc'),
|
||||
|
||||
win = require(DIR_SERVER + 'win'),
|
||||
update = require(DIR_SERVER + 'update'),
|
||||
socket = require(DIR_SERVER + 'socket'),
|
||||
find = require(DIR_SERVER + 'find'),
|
||||
spawnify = require(DIR_SERVER + 'spawnify'),
|
||||
|
||||
mainpackage = require(DIR_ROOT + 'package'),
|
||||
CLOUDCMD = mainpackage.name,
|
||||
|
||||
equalPart = Util.isContainStrAtBegin,
|
||||
|
||||
ClientFuncs = [],
|
||||
ClientDirs = [],
|
||||
Clients = [],
|
||||
WIN = process.platform === 'win32',
|
||||
|
||||
addNewLine = CloudFunc.addNewLine,
|
||||
|
||||
ConNum = 0,
|
||||
|
||||
CHANNEL = CloudFunc.CHANNEL_CONSOLE,
|
||||
CWD = process.cwd(),
|
||||
|
||||
HELP = {
|
||||
stdout : CLOUDCMD + ' exit \n' +
|
||||
CLOUDCMD + ' update \n',
|
||||
};
|
||||
|
||||
Util.exec.try(function() {
|
||||
find = require('glob');
|
||||
});
|
||||
CHANNEL = CloudFunc.CHANNEL_CONSOLE;
|
||||
|
||||
module.exports = function() {
|
||||
var ret;
|
||||
|
|
@ -67,15 +43,19 @@
|
|||
|
||||
callback({
|
||||
stdout : addNewLine(msg),
|
||||
path : process.cwd()
|
||||
path : CWD
|
||||
});
|
||||
|
||||
Clients[ConNum] = true;
|
||||
Clients[ConNum] = {
|
||||
cwd : CWD
|
||||
},
|
||||
|
||||
onMessage = Util.exec.with(getOnMessage, ConNum, callback);
|
||||
onMessage = function(command) {
|
||||
log(ConNum, command);
|
||||
spawnify(command, Clients[ConNum], callback);
|
||||
},
|
||||
onDisconnect = function(conNum) {
|
||||
Clients[conNum] =
|
||||
ClientFuncs[conNum] = null;
|
||||
Clients[conNum] = null;
|
||||
|
||||
log(conNum, 'console disconnected');
|
||||
|
||||
|
|
@ -96,262 +76,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* function gets onMessage function
|
||||
* that execute needed command
|
||||
*
|
||||
* @param pConnNum
|
||||
* @param callback
|
||||
*/
|
||||
function getOnMessage(connNum, callback, command) {
|
||||
var funcExec, firstChar,
|
||||
connName, ret,
|
||||
|
||||
isVolume = win.isChangeVolume(command),
|
||||
isCD = /^cd ?/.test(command),
|
||||
isCDWin = /^cd ?/i.test(command),
|
||||
|
||||
symbolsExec = ['*', '&', '{', '}', '|', '\'', '"', ';'],
|
||||
isSymbol = Util.isContainStr(command, symbolsExec),
|
||||
|
||||
CWD = process.cwd(),
|
||||
dir = ClientDirs[connNum],
|
||||
options = {
|
||||
cwd: dir || CWD
|
||||
};
|
||||
|
||||
Util.checkArgs(arguments, ['connNum', 'callback', 'command']);
|
||||
|
||||
if (!dir)
|
||||
dir = ClientDirs[connNum] = CWD;
|
||||
|
||||
connName = '#' + connNum + ': ';
|
||||
Util.log(connName + command);
|
||||
|
||||
if (equalPart(command, CLOUDCMD))
|
||||
ret = onCloudCmd(command, callback);
|
||||
else if (isCD || isCDWin && WIN || isVolume) {
|
||||
ret = true;
|
||||
|
||||
onCD(command, dir, function(error, json) {
|
||||
var path;
|
||||
|
||||
if (json.path) {
|
||||
path = json.path;
|
||||
ClientDirs[connNum] = path;
|
||||
}
|
||||
|
||||
callback(json);
|
||||
});
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
if (WIN)
|
||||
command = 'cmd /C ' + command;
|
||||
|
||||
if (!ClientFuncs[connNum])
|
||||
ClientFuncs[connNum] = setExec.bind(null, function(json, error, stderr) {
|
||||
log(connNum, error, 'error');
|
||||
log(connNum, stderr, 'stderror');
|
||||
|
||||
callback(json);
|
||||
});
|
||||
|
||||
funcExec = ClientFuncs[connNum];
|
||||
firstChar = command[0];
|
||||
|
||||
if (firstChar === '#') {
|
||||
command = command.slice(1);
|
||||
command = connName + command;
|
||||
command = addNewLine(command);
|
||||
|
||||
Util.exec(callback, {
|
||||
stdout: command
|
||||
}, true);
|
||||
} else if (firstChar === ' ' || isSymbol)
|
||||
exec(command, options, funcExec);
|
||||
else
|
||||
setSpawn(command, options, callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* function send result of command to client
|
||||
* @param callback
|
||||
*/
|
||||
function setExec(callback, error, stdout, stderr) {
|
||||
var json,
|
||||
errorStr = '';
|
||||
|
||||
if (stderr)
|
||||
errorStr = stderr;
|
||||
else if (error)
|
||||
errorStr = error.message;
|
||||
|
||||
json = {
|
||||
stdout : stdout,
|
||||
stderr : errorStr
|
||||
};
|
||||
|
||||
callback(json, error, stderr);
|
||||
}
|
||||
|
||||
function setSpawn(сommand, options, callback) {
|
||||
spawnify(сommand, options, function(error, stdout, stderr) {
|
||||
var errorStr = '';
|
||||
|
||||
Util.log(error);
|
||||
|
||||
if (error)
|
||||
errorStr = error.message;
|
||||
else if (stderr)
|
||||
errorStr = addNewLine(stderr);
|
||||
|
||||
errorStr = addNewLine(errorStr);
|
||||
|
||||
callback({
|
||||
stderr: errorStr,
|
||||
stdout: stdout
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function onCloudCmd(command, callback) {
|
||||
var is;
|
||||
|
||||
command = Util.rmStr(command, CLOUDCMD);
|
||||
is = !equalPart(command, ' ');
|
||||
|
||||
if (is) {
|
||||
Util.exec(callback, HELP);
|
||||
} else {
|
||||
command = Util.rmStr(command, ' ');
|
||||
is = update && equalPart(command, 'update');
|
||||
|
||||
if (is) {
|
||||
update.get();
|
||||
|
||||
Util.exec(callback, {
|
||||
stdout: addNewLine('update: ok')
|
||||
});
|
||||
}
|
||||
|
||||
is = Util.strCmp(command, 'exit');
|
||||
|
||||
if (is)
|
||||
process.exit();
|
||||
}
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
function onCD(command, currDir, callback) {
|
||||
var CD = 'cd ',
|
||||
HOME = process.env.HOME,
|
||||
|
||||
isChangeVolume = win.isChangeVolume(command),
|
||||
isVolume = win.isVolume(command),
|
||||
paramDir = Util.rmStrOnce(command, [CD, 'cd']),
|
||||
|
||||
regExpHome = new RegExp('^~'),
|
||||
regExpRoot = new RegExp('^[/\\\\]'),
|
||||
|
||||
isWildCard = Util.isContainStr(paramDir, ['*', '?']),
|
||||
isHome = regExpHome.test(paramDir) && !WIN,
|
||||
isRoot = regExpRoot.test(paramDir),
|
||||
|
||||
onExec = function (error, stdout, stderr) {
|
||||
var path = paramDir,
|
||||
errorStr = '';
|
||||
|
||||
if (stderr) {
|
||||
errorStr = stderr;
|
||||
} else if (error) {
|
||||
errorStr = error.message;
|
||||
path = '';
|
||||
}
|
||||
|
||||
callback(error || stderr, {
|
||||
stderr : addNewLine(errorStr),
|
||||
stdout : stdout,
|
||||
path : path
|
||||
});
|
||||
};
|
||||
|
||||
if (isHome) {
|
||||
command = command.replace('~', HOME);
|
||||
paramDir = paramDir.replace('~', HOME);
|
||||
}
|
||||
|
||||
if (!paramDir && !WIN)
|
||||
paramDir = '.';
|
||||
|
||||
if (!isChangeVolume || isVolume) {
|
||||
paramDir = getFirstWord(paramDir);
|
||||
paramDir = path.normalize(paramDir);
|
||||
|
||||
command = Util.rmStrOnce(command, [
|
||||
CD,
|
||||
paramDir,
|
||||
'\'' + paramDir + '\'',
|
||||
'"' + paramDir + '"',
|
||||
]);
|
||||
|
||||
if (!isHome && !isRoot)
|
||||
paramDir = path.join(currDir, paramDir);
|
||||
|
||||
if (isWildCard)
|
||||
command = CD + paramDir + ' ' + command;
|
||||
else
|
||||
command = CD + '"' + paramDir + '" ' + command;
|
||||
}
|
||||
|
||||
if (!isWildCard)
|
||||
exec(command, {cwd: paramDir}, onExec);
|
||||
else
|
||||
find(paramDir, function(error, dirs) {
|
||||
var dir;
|
||||
|
||||
if (!error)
|
||||
dir = dirs[0];
|
||||
|
||||
paramDir = dir;
|
||||
exec(command, {cwd: dir}, onExec);
|
||||
});
|
||||
}
|
||||
|
||||
function getFirstWord(str) {
|
||||
var word, result,
|
||||
regStrEnd = getRegStrEnd(),
|
||||
regStr = '^(.*?)',
|
||||
regStrQuotes = '^"(.*)"',
|
||||
regExp = new RegExp(regStr + regStrEnd),
|
||||
regExpQuotes = new RegExp(regStrQuotes + regStrEnd + '?'),
|
||||
is = Util.isString(str);
|
||||
|
||||
if (is) {
|
||||
result = str.match(regExpQuotes);
|
||||
|
||||
if (result) {
|
||||
word = result[1];
|
||||
} else {
|
||||
result = str.match(regExp);
|
||||
word = result && result[1];
|
||||
}
|
||||
|
||||
if (!word)
|
||||
word = str;
|
||||
}
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
function getRegStrEnd() {
|
||||
var regStrEnd = '(\\s|\\;|&&|\\|\\|)';
|
||||
|
||||
return regStrEnd;
|
||||
}
|
||||
|
||||
function log(connNum, str, typeParam) {
|
||||
var ret,
|
||||
type = ' ';
|
||||
|
|
|
|||
|
|
@ -1,20 +1,137 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
var child_process = require('child_process'),
|
||||
var DIR = './',
|
||||
DIR_LIB = DIR + '../',
|
||||
|
||||
path = require('path'),
|
||||
child_process = require('child_process'),
|
||||
|
||||
exec = child_process.exec,
|
||||
|
||||
WIN = process.platform === 'win32',
|
||||
spawn = child_process.spawn,
|
||||
|
||||
DIR = '../',
|
||||
Util = require(DIR_LIB + 'util'),
|
||||
CloudFunc = require(DIR_LIB + 'cloudfunc'),
|
||||
|
||||
Util = require(DIR + 'util');
|
||||
addNewLine = CloudFunc.addNewLine,
|
||||
|
||||
find = require(DIR + 'find'),
|
||||
win = require(DIR + 'win');
|
||||
|
||||
Util.exec.try(function() {
|
||||
find = require('glob');
|
||||
});
|
||||
|
||||
module.exports = function(сommand, options, callback) {
|
||||
module.exports = function(command, options, callback) {
|
||||
Util.checkArgs(arguments, ['command', 'callback']);
|
||||
|
||||
if (!callback) {
|
||||
callback = options;
|
||||
|
||||
options = {
|
||||
cwd: process.cwd()
|
||||
};
|
||||
}
|
||||
|
||||
onMessage(command, options, callback);
|
||||
};
|
||||
|
||||
|
||||
function onMessage(command, options, callback) {
|
||||
var funcExec, firstChar,
|
||||
connName, ret,
|
||||
dir = options.cwd,
|
||||
isVolume = win.isChangeVolume(command),
|
||||
isCD = /^cd ?/.test(command),
|
||||
isCDWin = /^cd ?/i.test(command),
|
||||
|
||||
symbolsExec = ['*', '&', '{', '}', '|', '\'', '"', ';'],
|
||||
isSymbol = Util.isContainStr(command, symbolsExec);
|
||||
|
||||
Util.checkArgs(arguments, ['connNum', 'callback', 'command']);
|
||||
|
||||
if (isCD || isCDWin && WIN || isVolume) {
|
||||
ret = true;
|
||||
|
||||
onCD(command, dir, function(error, json) {
|
||||
var path;
|
||||
|
||||
if (json.path) {
|
||||
path = json.path;
|
||||
options.cwd = path;
|
||||
}
|
||||
|
||||
callback(json);
|
||||
});
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
if (WIN)
|
||||
command = 'cmd /C ' + command;
|
||||
|
||||
funcExec = setExec.bind(null, callback);
|
||||
|
||||
firstChar = command[0];
|
||||
|
||||
if (firstChar === '#') {
|
||||
command = command.slice(1);
|
||||
command = connName + command;
|
||||
command = addNewLine(command);
|
||||
|
||||
Util.exec(callback, {
|
||||
stdout: command
|
||||
}, true);
|
||||
} else if (firstChar === ' ' || isSymbol)
|
||||
exec(command, options, funcExec);
|
||||
else
|
||||
setSpawn(command, options, callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* function send result of command to client
|
||||
* @param callback
|
||||
*/
|
||||
function setExec(callback, error, stdout, stderr) {
|
||||
var json,
|
||||
errorStr = '';
|
||||
|
||||
if (stderr)
|
||||
errorStr = stderr;
|
||||
else if (error)
|
||||
errorStr = error.message;
|
||||
|
||||
json = {
|
||||
stdout : stdout,
|
||||
stderr : errorStr
|
||||
};
|
||||
|
||||
callback(json, error, stderr);
|
||||
}
|
||||
|
||||
function setSpawn(сommand, options, callback) {
|
||||
var cmd, error,
|
||||
isSended = false,
|
||||
args = сommand.split(' '),
|
||||
func = function(error, stdout, stderr) {
|
||||
var errorStr = '';
|
||||
|
||||
isSended = true;
|
||||
callback(error, stdout, stderr);
|
||||
|
||||
if (error)
|
||||
errorStr = error.message;
|
||||
else if (stderr)
|
||||
errorStr = addNewLine(stderr);
|
||||
|
||||
errorStr = addNewLine(errorStr);
|
||||
|
||||
callback({
|
||||
stderr: errorStr,
|
||||
stdout: stdout
|
||||
});
|
||||
};
|
||||
|
||||
Util.checkArgs(arguments, ['command', 'callback']);
|
||||
|
|
@ -55,5 +172,113 @@
|
|||
func();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function onCD(command, currDir, callback) {
|
||||
var CD = 'cd ',
|
||||
HOME = process.env.HOME,
|
||||
|
||||
isChangeVolume = win.isChangeVolume(command),
|
||||
isVolume = win.isVolume(command),
|
||||
paramDir = Util.rmStrOnce(command, [CD, 'cd']),
|
||||
|
||||
regExpHome = new RegExp('^~'),
|
||||
regExpRoot = new RegExp('^[/\\\\]'),
|
||||
|
||||
isWildCard = Util.isContainStr(paramDir, ['*', '?']),
|
||||
isHome = regExpHome.test(paramDir) && !WIN,
|
||||
isRoot = regExpRoot.test(paramDir),
|
||||
|
||||
onExec = function (error, stdout, stderr) {
|
||||
var path = paramDir,
|
||||
errorStr = '';
|
||||
|
||||
if (stderr) {
|
||||
errorStr = stderr;
|
||||
} else if (error) {
|
||||
errorStr = error.message;
|
||||
path = '';
|
||||
}
|
||||
|
||||
callback(error || stderr, {
|
||||
stderr : addNewLine(errorStr),
|
||||
stdout : stdout,
|
||||
path : path
|
||||
});
|
||||
};
|
||||
|
||||
if (isHome) {
|
||||
command = command.replace('~', HOME);
|
||||
paramDir = paramDir.replace('~', HOME);
|
||||
}
|
||||
|
||||
if (!paramDir && !WIN)
|
||||
paramDir = '.';
|
||||
|
||||
if (!isChangeVolume || isVolume) {
|
||||
paramDir = getFirstWord(paramDir);
|
||||
paramDir = path.normalize(paramDir);
|
||||
|
||||
command = Util.rmStrOnce(command, [
|
||||
CD,
|
||||
paramDir,
|
||||
'\'' + paramDir + '\'',
|
||||
'"' + paramDir + '"',
|
||||
]);
|
||||
|
||||
if (!isHome && !isRoot)
|
||||
paramDir = path.join(currDir, paramDir);
|
||||
|
||||
if (isWildCard)
|
||||
command = CD + paramDir + ' ' + command;
|
||||
else
|
||||
command = CD + '"' + paramDir + '" ' + command;
|
||||
}
|
||||
|
||||
if (!isWildCard)
|
||||
exec(command, {cwd: paramDir}, onExec);
|
||||
else
|
||||
find(paramDir, function(error, dirs) {
|
||||
var dir;
|
||||
|
||||
if (!error)
|
||||
dir = dirs[0];
|
||||
|
||||
paramDir = dir;
|
||||
exec(command, {cwd: dir}, onExec);
|
||||
});
|
||||
}
|
||||
|
||||
function getFirstWord(str) {
|
||||
var word, result,
|
||||
regStrEnd = getRegStrEnd(),
|
||||
regStr = '^(.*?)',
|
||||
regStrQuotes = '^"(.*)"',
|
||||
regExp = new RegExp(regStr + regStrEnd),
|
||||
regExpQuotes = new RegExp(regStrQuotes + regStrEnd + '?'),
|
||||
is = Util.isString(str);
|
||||
|
||||
if (is) {
|
||||
result = str.match(regExpQuotes);
|
||||
|
||||
if (result) {
|
||||
word = result[1];
|
||||
} else {
|
||||
result = str.match(regExp);
|
||||
word = result && result[1];
|
||||
}
|
||||
|
||||
if (!word)
|
||||
word = str;
|
||||
}
|
||||
|
||||
return word;
|
||||
}
|
||||
|
||||
function getRegStrEnd() {
|
||||
var regStrEnd = '(\\s|\\;|&&|\\|\\|)';
|
||||
|
||||
return regStrEnd;
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue