diff --git a/bin/emulator.js b/bin/emulator.js index 41c9402..408c0cd 100644 --- a/bin/emulator.js +++ b/bin/emulator.js @@ -69,4 +69,40 @@ function normalizeExecArgs(command, options){ }; } -module.exports = Emulator; +// thread +process.on('message', function (project){ + var stream; + + var emulator = new Emulator({ + cwd: project.path, + command: project.command.value + }); + + stream = emulator.start(); + + stream.stdout.on('data', function (data){ + process.send({ + event: 'data', + project: project, + data: data.toString() + }); + }); + + stream.stderr.on('data', 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() + }); + }); +}); diff --git a/bin/thread.js b/bin/thread.js index 85063ef..c94a600 100644 --- a/bin/thread.js +++ b/bin/thread.js @@ -4,24 +4,24 @@ 'use strict'; -var cluster = require('cluster'); - +var path = require('path'); var ipc = require('ipc-main'); +var fork = require('child_process').fork; // cache -var workers = {}; +var threads = {}; /** - * killWorker + * killThread * @param name */ -function killWorker(name){ - var worker = workers[name]; +function killThread(name){ + var thread = threads[name]; - if (worker && !worker.isDead()) { - worker.kill('SIGTERM'); + if (thread && thread.connected) { + thread.kill('SIGTERM'); - delete workers[name]; + delete threads[name]; } } @@ -29,11 +29,11 @@ module.exports = { start: function (){ ipc.on('emulator', function (event, project, action){ var key = project.name + '-' + project.command.name; - var worker = workers[key]; + var thread = threads[key]; switch (action) { case 'start': - if (!worker || worker.isDead()) { + if (!thread || !thread.connected) { var env = {}; Object.keys(process.env).forEach(function (key){ @@ -44,29 +44,31 @@ module.exports = { env[item.name] = item.value; }); - worker = cluster.fork(env); + thread = fork(path.join(__dirname, 'emulator'), { + env: env + }); - worker.on('message', function (message){ + thread.on('message', function (message){ event.sender.send('emulator', message.event, message.project, message.data); }); - worker.on('error', function (){ - killWorker(project.name); + thread.on('error', function (){ + killThread(project.name); }); delete project.env; - worker.send(project); + thread.send(project); - workers[project.name] = worker; + threads[project.name] = thread; } else { delete project.env; - worker.send(project); + thread.send(project); } break; case 'stop': - killWorker(project.name); + killThread(project.name); break; } }); diff --git a/main.js b/main.js index 276ee1c..f3af99f 100644 --- a/main.js +++ b/main.js @@ -4,134 +4,91 @@ 'use strict'; -var cluster = require('cluster'); +// node module +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 thread = require('./bin/thread'); -if (cluster.isMaster) { - // node module - 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 thread = require('./bin/thread'); +// keep a global reference of the window object, if you don't, the window will +// be closed automatically when the javascript object is GCed +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'); - // keep a global reference of the window object, if you don't, the window will - // be closed automatically when the javascript object is GCed - 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'); +// quit when all windows are closed +app.on('window-all-closed', function (){ + app.quit(); +}); - // quit when all windows are closed - app.on('window-all-closed', function (){ - app.quit(); - }); +// this method will be called when atom-shell has done everything +// initialization and ready for creating browser windows +app.on('ready', function (){ + // create the tray window + mainTray = new Tray(ICON); - // this method will be called when atom-shell has done everything - // initialization and ready for creating browser windows - app.on('ready', function (){ - // 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(); - } + 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 - 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(); + // 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 }); -} else { - var Emulator = require('./bin/emulator'); - process.on('message', function (project){ - var stream; + // 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(); - var emulator = new Emulator({ - cwd: project.path, - command: project.command.value - }); - - stream = emulator.start(); - - stream.stdout.on('data', function (data){ - process.send({ - event: 'data', - project: project, - data: data.toString() - }); - }); - - stream.stderr.on('data', 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() - }); - }); + mainTray = null; + mainWindow = null; }); -} + + // window control + windowControl(ICON, mainWindow, mainTray); + + // open directory + openDirectory(mainWindow); + + // app configure + new AppConfigure(mainWindow, mainTray); + + // emulator start + thread.start(); +}); \ No newline at end of file diff --git a/static/css/index.css b/static/css/index.css index b0270d6..380a410 100644 --- a/static/css/index.css +++ b/static/css/index.css @@ -494,7 +494,3 @@ header [class*=" icon-"] { height: calc(100% - 32px); background: url(../images/no-data.png) center no-repeat; } -.ui-terminal-cursor { - background: #fff; - color: #181818; -}