update files

This commit is contained in:
nuintun 2015-12-03 10:19:09 +08:00
parent d5c9496df7
commit f4d4451eeb
4 changed files with 204 additions and 143 deletions

View File

@ -4,8 +4,6 @@
'use strict'; 'use strict';
var ipc = require('ipc-main');
var spawn = require('child_process').spawn; var spawn = require('child_process').spawn;
/** /**
@ -71,64 +69,5 @@ function normalizeExecArgs(command, options){
}; };
} }
var emulators = {}; module.exports = Emulator;
module.exports = {
Emulator: Emulator,
start: function (){
ipc.on('emulator', function (event, project, action){
var key = project.name + '-' + project.command.name;
var emulator = emulators[key];
switch (action) {
case 'start':
if (!emulator) {
var stream;
var env = {};
Object.keys(process.env).forEach(function (key){
env[key] = process.env[key];
});
project.env.forEach(function (item){
env[item.name] = item.value;
});
emulator = new Emulator({
env: env,
cwd: project.path,
command: project.command.value
});
stream = emulator.start();
stream.stdout.on('data', function (data){
event.sender.send('emulator', 'data', project, data);
});
stream.stderr.on('error', function (error){
event.sender.send('emulator', 'error', project, error);
emulator.stop();
delete emulators[key];
});
stream.on('close', function (signal){
event.sender.send('emulator', 'close', project, signal);
delete emulators[key];
});
emulators[key] = emulator;
}
break;
case 'stop':
if (emulator) {
emulator.stop();
}
break;
}
});
}
};

70
bin/thread.js Normal file
View File

@ -0,0 +1,70 @@
/**
* Created by nuintun on 2015/12/3.
*/
'use strict';
var cluster = require('cluster');
var ipc = require('ipc-main');
// cache
var workers = {};
/**
* killWorker
* @param name
*/
function killWorker(name){
var worker = workers[name];
if (worker && !worker.isDead()) {
worker.kill('SIGTERM');
delete workers[name];
}
}
module.exports = {
start: function (){
ipc.on('emulator', function (event, project, action){
var key = project.name + '-' + project.command.name;
var worker = workers[key];
switch (action) {
case 'start':
if (!worker) {
var env = {};
Object.keys(process.env).forEach(function (key){
env[key] = process.env[key];
});
project.env.forEach(function (item){
env[item.name] = item.value;
});
worker = cluster.fork(env);
worker.on('message', function (message){
event.sender.send('emulator', message.event, message.project, message.data);
});
worker.on('error', function (){
killWorker(project.name);
});
delete project.env;
worker.send(project);
workers[project.name] = worker;
}
break;
case 'stop':
killWorker(project.name);
break;
}
});
}
};

197
main.js
View File

@ -4,91 +4,134 @@
'use strict'; 'use strict';
// node module var cluster = require('cluster');
var path = require('path');
// module to control application life
var app = require('app');
var Menu = require('menu');
var Tray = require('tray');
// module to create native browser window
var BrowserWindow = require('browser-window');
// custom module
var windowControl = require('./bin/window-control');
var openDirectory = require('./bin/open-directory');
var AppConfigure = require('./bin/app-configure');
var emulator = require('./bin/emulator');
// keep a global reference of the window object, if you don't, the window will if (cluster.isMaster) {
// be closed automatically when the javascript object is GCed // node module
var mainTray = null; var path = require('path');
var mainWindow = null; // module to control application life
// const var var app = require('app');
const APPNAME = '命令管理器'; var Menu = require('menu');
const ICON = path.join(__dirname, './app.ico'); var Tray = require('tray');
const INDEX = 'file:///' + path.join(__dirname, 'index.html'); // module to create native browser window
var BrowserWindow = require('browser-window');
// custom module
var windowControl = require('./bin/window-control');
var openDirectory = require('./bin/open-directory');
var AppConfigure = require('./bin/app-configure');
var thread = require('./bin/thread');
// quit when all windows are closed // keep a global reference of the window object, if you don't, the window will
app.on('window-all-closed', function (){ // be closed automatically when the javascript object is GCed
app.quit(); var mainTray = null;
}); var mainWindow = null;
// const var
const APPNAME = '命令管理器';
const ICON = path.join(__dirname, './app.ico');
const INDEX = 'file:///' + path.join(__dirname, 'index.html');
// this method will be called when atom-shell has done everything // quit when all windows are closed
// initialization and ready for creating browser windows app.on('window-all-closed', function (){
app.on('ready', function (){ app.quit();
// create the tray window
mainTray = new Tray(ICON);
mainTray.setToolTip(APPNAME);
mainTray.setContextMenu(Menu.buildFromTemplate([
{
label: '显示窗口',
click: function (){
mainWindow.show();
}
},
{
label: '退出程序',
click: function (){
app.quit();
}
}
]));
// create the browser window
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
icon: ICON,
'min-width': 1024,
'min-height': 768,
title: APPNAME,
frame: false,
center: true,
'use-content-size': true
}); });
// and load the index.html of the app // this method will be called when atom-shell has done everything
mainWindow.loadURL(INDEX); // initialization and ready for creating browser windows
// emitted when the window is closed app.on('ready', function (){
mainWindow.on('closed', function (){ // create the tray window
// dereference the window object, usually you would store windows mainTray = new Tray(ICON);
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element
mainTray.destroy();
mainTray = null; mainTray.setToolTip(APPNAME);
mainWindow = null; mainTray.setContextMenu(Menu.buildFromTemplate([
{
label: '显示窗口',
click: function (){
mainWindow.show();
}
},
{
label: '退出程序',
click: function (){
app.quit();
}
}
]));
// create the browser window
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
icon: ICON,
'min-width': 1024,
'min-height': 768,
title: APPNAME,
frame: false,
center: true,
'use-content-size': true
});
// and load the index.html of the app
mainWindow.loadURL(INDEX);
// emitted when the window is closed
mainWindow.on('closed', function (){
// dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element
mainTray.destroy();
mainTray = null;
mainWindow = null;
});
// window control
windowControl(ICON, mainWindow, mainTray);
// open directory
openDirectory(mainWindow);
// app configure
new AppConfigure(mainWindow, mainTray);
// emulator start
thread.start();
}); });
} else {
var Emulator = require('./bin/emulator');
// window control process.on('message', function (project){
windowControl(ICON, mainWindow, mainTray); var stream;
// open directory var emulator = new Emulator({
openDirectory(mainWindow); cwd: project.path,
command: project.command.value
});
// app configure stream = emulator.start();
new AppConfigure(mainWindow, mainTray);
// emulator start stream.stdout.on('data', function (data){
emulator.start(); process.send({
}); event: 'data',
project: project,
data: data.toString()
});
});
stream.stderr.on('error', function (error){
emulator.stop();
process.send({
event: 'error',
project: project,
data: error.toString()
});
});
stream.on('close', function (signal){
emulator.stop();
process.send({
event: 'close',
project: project,
data: signal.toString()
});
});
});
}

View File

@ -143,10 +143,19 @@ module.exports = Vue.component('app-main', {
}, },
methods: { methods: {
exec: function (name, command){ exec: function (name, command){
var project = this.project;
var runtime = AppRuntime[project.name];
runtime.worker.port.postMessage({
action: 'write',
name: project.name,
data: '\u001b[32m执行命令\u001b[0m: \u001b[35m' + name + '\u001b[0m\r\n'
});
ipc.send('emulator', { ipc.send('emulator', {
name: this.project.name, name: project.name,
path: this.project.path, path: project.path,
env: util.normalize(this.project.env), env: util.normalize(project.env),
command: { command: {
name: name, name: name,
value: command value: command
@ -202,7 +211,7 @@ module.exports = Vue.component('app-main', {
data = data.toString(); data = data.toString();
break; break;
case 'error': case 'error':
data = '\u001b[31m发生错误 \u001b[0m' + data.toString(); data = '\u001b[31m发生错误: \u001b[0m' + data.toString();
break; break;
case 'close': case 'close':
data = '\u001b[32m命令执行完成\u001b[0m'; data = '\u001b[32m命令执行完成\u001b[0m';