mirror of
https://github.com/nuintun/command-manager.git
synced 2025-06-07 11:34:05 +08:00
update files
This commit is contained in:
parent
a78ff09309
commit
2b958a6f71
119
bin/emulator.js
119
bin/emulator.js
@ -6,57 +6,37 @@
|
|||||||
|
|
||||||
var ipc = require('ipc-main');
|
var ipc = require('ipc-main');
|
||||||
|
|
||||||
var util = require('util');
|
|
||||||
var EventEmitter = require('events');
|
|
||||||
var spawn = require('child_process').spawn;
|
var spawn = require('child_process').spawn;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Emulator
|
* Emulator
|
||||||
* @param name
|
* @param task
|
||||||
* @param command
|
|
||||||
* @constructor
|
* @constructor
|
||||||
*/
|
*/
|
||||||
function Emulator(name, command){
|
function Emulator(task){
|
||||||
this.name = name;
|
this.task = task;
|
||||||
this.command = command;
|
|
||||||
|
|
||||||
EventEmitter.call(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inherit functions from `EventEmitter`'s prototype
|
|
||||||
util.inherits(Emulator, EventEmitter);
|
|
||||||
|
|
||||||
Emulator.prototype = {
|
Emulator.prototype = {
|
||||||
start: function (){
|
start: function (){
|
||||||
var context = this;
|
this.thread = this.exec(this.task.command, {
|
||||||
|
env: this.task.env,
|
||||||
|
cwd: this.task.cwd
|
||||||
|
});
|
||||||
|
|
||||||
this.thread = this.exec(this.command)
|
return this.thread;
|
||||||
.on('data', function (data){
|
|
||||||
context.emit('data', data);
|
|
||||||
})
|
|
||||||
.on('error', function (error){
|
|
||||||
context.emit('error', error);
|
|
||||||
})
|
|
||||||
.on('close', function (signal){
|
|
||||||
context.emit('close', signal);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
stop: function (){
|
stop: function (){
|
||||||
if (this.thread) {
|
if (this.thread) {
|
||||||
this.thread.kill('SIGTERM');
|
this.thread.kill('SIGTERM');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
exec: function (command /*, options, callback*/){
|
exec: function (){
|
||||||
var options = normalizeExecArgs.apply(null, arguments);
|
var parsed = normalizeExecArgs.apply(null, arguments);
|
||||||
|
|
||||||
// spawn
|
console.log(JSON.stringify(parsed, null, 2));
|
||||||
return spawn(options.file, options.args, {
|
|
||||||
cwd: options.cwd,
|
return spawn(parsed.shell, parsed.args, parsed.options);
|
||||||
env: options.env,
|
|
||||||
gid: options.gid,
|
|
||||||
uid: options.uid,
|
|
||||||
windowsVerbatimArguments: !!options.windowsVerbatimArguments
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,31 +44,30 @@ Emulator.prototype = {
|
|||||||
* normalize exec args
|
* normalize exec args
|
||||||
* @param command
|
* @param command
|
||||||
* @param options
|
* @param options
|
||||||
* @returns {{cmd: *, file: *, args: *, options: *}}
|
* @returns {{cmd: *, shell: *, args: *, options: *}}
|
||||||
*/
|
*/
|
||||||
function normalizeExecArgs(command, options){
|
function normalizeExecArgs(command, options){
|
||||||
var file, args;
|
var shell, args;
|
||||||
|
|
||||||
// Make a shallow copy before patching so we don't clobber the user's
|
// Make a shallow copy before patching so we don't clobber the user's
|
||||||
// options object.
|
// options object.
|
||||||
options = options || options;
|
options = options || {};
|
||||||
|
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
file = process.env.comspec || 'cmd.exe';
|
shell = process.env.comspec || 'cmd.exe';
|
||||||
args = ['/s', '/c', '"' + command + '"'];
|
args = ['/s', '/c', '"' + command + '"'];
|
||||||
options.windowsVerbatimArguments = true;
|
options.windowsVerbatimArguments = true;
|
||||||
} else {
|
} else {
|
||||||
file = '/bin/sh';
|
shell = '/bin/sh';
|
||||||
args = ['-c', command];
|
args = ['-c', command];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.shell) {
|
if (options.shell) {
|
||||||
file = options.shell;
|
shell = options.shell;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cmd: command,
|
shell: shell,
|
||||||
file: file,
|
|
||||||
args: args,
|
args: args,
|
||||||
options: options
|
options: options
|
||||||
};
|
};
|
||||||
@ -100,37 +79,57 @@ module.exports = {
|
|||||||
Emulator: Emulator,
|
Emulator: Emulator,
|
||||||
start: function (){
|
start: function (){
|
||||||
ipc.on('emulator', function (event, project, action){
|
ipc.on('emulator', function (event, project, action){
|
||||||
|
var key = project.name + '-' + project.command.name;
|
||||||
|
var emulator = cache[key];
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'start':
|
case 'start':
|
||||||
if (!cache[project.name]) {
|
if (!emulator) {
|
||||||
var emulator = new Emulator(project.command.name, project.command.value);
|
|
||||||
var send = function (type, data){
|
var send = function (type, data){
|
||||||
event.sender.send(type, project, data);
|
event.sender.send('emulator', type, project, data);
|
||||||
};
|
};
|
||||||
|
|
||||||
emulator
|
var env = process.env;
|
||||||
.on('data', function (data){
|
|
||||||
send('data', data);
|
|
||||||
})
|
|
||||||
.on('error', function (error){
|
|
||||||
send('error', error);
|
|
||||||
})
|
|
||||||
.on('close', function (signal){
|
|
||||||
send('close', signal);
|
|
||||||
});
|
|
||||||
|
|
||||||
cache[project.name] = emulator;
|
project.env.forEach(function (item){
|
||||||
|
env[item.name] = item.value
|
||||||
|
});
|
||||||
|
|
||||||
|
emulator = new Emulator({
|
||||||
|
env: env,
|
||||||
|
cwd: project.path,
|
||||||
|
command: project.command.value
|
||||||
|
});
|
||||||
|
|
||||||
|
var stream = emulator.start();
|
||||||
|
|
||||||
|
stream.stdout.on('data', function (data){
|
||||||
|
send('data', data);
|
||||||
|
});
|
||||||
|
|
||||||
|
stream.stderr.on('error', function (error){
|
||||||
|
send('error', error + '\r\n');
|
||||||
|
|
||||||
|
emulator.stop();
|
||||||
|
|
||||||
|
delete cache[key];
|
||||||
|
});
|
||||||
|
|
||||||
|
stream.on('close', function (signal){
|
||||||
|
send('close', signal + '\r\n');
|
||||||
|
|
||||||
|
delete cache[key];
|
||||||
|
});
|
||||||
|
|
||||||
|
cache[key] = emulator;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'stop':
|
case 'stop':
|
||||||
if (cache[project.name]) {
|
if (emulator) {
|
||||||
cache[project.name].stop();
|
emulator.stop();
|
||||||
|
|
||||||
delete cache[project.name];
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var ipc = require('ipc-renderer');
|
||||||
|
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var util = require('../../util');
|
var util = require('../../util');
|
||||||
@ -72,9 +74,9 @@ function createXTerm(name, xtermNode){
|
|||||||
refresh(runtime.xterm);
|
refresh(runtime.xterm);
|
||||||
} else {
|
} else {
|
||||||
var xterm = new Terminal({
|
var xterm = new Terminal({
|
||||||
debug: true,
|
convertEol: true,
|
||||||
bgColor: 'transparent',
|
fgColor: 'inherit',
|
||||||
fgColor: 'inherit'
|
bgColor: 'transparent'
|
||||||
});
|
});
|
||||||
|
|
||||||
xterm.open();
|
xterm.open();
|
||||||
@ -132,12 +134,15 @@ module.exports = Vue.component('app-main', {
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
exec: function (name, command){
|
exec: function (name, command){
|
||||||
var runtime = window.AppRuntime[this.project.name];
|
ipc.send('emulator', {
|
||||||
|
name: this.project.name,
|
||||||
runtime.xterm.writeln('运行命令: \u001b[32m' + name + '\u001b[39m 在 \u001b[32m'
|
path: this.project.path,
|
||||||
+ (new Date().toLocaleString()) + '\u001b[39m');
|
env: this.project.env,
|
||||||
scroll(runtime.xterm);
|
command: {
|
||||||
|
name: name,
|
||||||
|
value: command
|
||||||
|
}
|
||||||
|
}, 'start');
|
||||||
},
|
},
|
||||||
setting: function (){
|
setting: function (){
|
||||||
this.showSetting = true;
|
this.showSetting = true;
|
||||||
@ -185,6 +190,29 @@ module.exports = Vue.component('app-main', {
|
|||||||
|
|
||||||
context.expandCommand = trigger && trigger.contains(target);
|
context.expandCommand = trigger && trigger.contains(target);
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
|
ipc.on('emulator', function (event, type, project, data){
|
||||||
|
var runtime = window.AppRuntime[project.name];
|
||||||
|
|
||||||
|
if (runtime) {
|
||||||
|
switch (type) {
|
||||||
|
case 'data':
|
||||||
|
data += '';
|
||||||
|
break;
|
||||||
|
case 'error':
|
||||||
|
data = '执行出现错误: ' + data;
|
||||||
|
break;
|
||||||
|
case 'close':
|
||||||
|
data = '\u001b[32m命令执行完毕\u001b[39m\r\n';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
runtime.xterm.write(data + '');
|
||||||
|
scroll(runtime.xterm);
|
||||||
|
} else {
|
||||||
|
event.sender.send('emulator', project, 'stop');
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
ready: function (){
|
ready: function (){
|
||||||
createXTerm(this.project.name, this.$els.terminal);
|
createXTerm(this.project.name, this.$els.terminal);
|
||||||
|
@ -22,7 +22,7 @@ module.exports = function (Terminal){
|
|||||||
* @param end
|
* @param end
|
||||||
*/
|
*/
|
||||||
Terminal.prototype.refresh = function (start, end){
|
Terminal.prototype.refresh = function (start, end){
|
||||||
var parent = this.element.parentNode;
|
var parent = this.element ? this.element.parentNode : null;
|
||||||
var x, y, i, line, out, ch, width, data, attr, fgColor, bgColor, flags, row;
|
var x, y, i, line, out, ch, width, data, attr, fgColor, bgColor, flags, row;
|
||||||
|
|
||||||
if (parent && end - start >= this.rows / 2) {
|
if (parent && end - start >= this.rows / 2) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user