From a78ff09309a50c5d67d00c0f11419a40dc945813 Mon Sep 17 00:00:00 2001 From: nuintun Date: Thu, 26 Nov 2015 15:16:32 +0800 Subject: [PATCH] update files --- bin/app-configure.js | 15 ++- bin/emulator.js | 136 ++++++++++++++++++++++++ main.js | 4 + static/js/app/index.js | 5 +- static/js/components/dialog/dialog.html | 10 ++ static/js/components/dialog/index.js | 7 ++ 6 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 bin/emulator.js create mode 100644 static/js/components/dialog/dialog.html create mode 100644 static/js/components/dialog/index.js diff --git a/bin/app-configure.js b/bin/app-configure.js index 8027616..62225dc 100644 --- a/bin/app-configure.js +++ b/bin/app-configure.js @@ -164,10 +164,13 @@ AppConfigure.prototype = { init: function (){ var context = this; - this.create(); - ipc.on('app-configure', function (event, command, configure){ switch (command) { + case 'ready': + context.create(function (){ + event.sender.send('app-configure', 'ready'); + }); + break; case 'import': context.import(function (configure){ this.showMessageBox('配置文件导入成功!', { type: 'info' }); @@ -185,7 +188,7 @@ AppConfigure.prototype = { this.showMessageBox('配置文件导出失败!'); }); break; - case 'refresh': + case 'read': context.read(function (configure){ event.sender.send('app-configure', 'refresh', configure); }, function (error){ @@ -204,16 +207,18 @@ AppConfigure.prototype = { }); }, - create: function (){ + create: function (done){ var context = this; fs.stat(CONFIGUREPATH, function (error, stats){ if (error || !stats.isFile()) { - context.save(DEFAULTCONFIGURE, null, function (){ + context.save(DEFAULTCONFIGURE, done, function (){ context.showMessageBox('配置文件创建失败,请用管理员模式运行重试!', function (){ context.window.close(); }); }); + } else { + done.call(this); } }); }, diff --git a/bin/emulator.js b/bin/emulator.js new file mode 100644 index 0000000..5773b4e --- /dev/null +++ b/bin/emulator.js @@ -0,0 +1,136 @@ +/** + * Created by nuintun on 2015/11/26. + */ + +'use strict'; + +var ipc = require('ipc-main'); + +var util = require('util'); +var EventEmitter = require('events'); +var spawn = require('child_process').spawn; + +/** + * Emulator + * @param name + * @param command + * @constructor + */ +function Emulator(name, command){ + this.name = name; + this.command = command; + + EventEmitter.call(this); +} + +// Inherit functions from `EventEmitter`'s prototype +util.inherits(Emulator, EventEmitter); + +Emulator.prototype = { + start: function (){ + var context = this; + + this.thread = this.exec(this.command) + .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 (){ + if (this.thread) { + this.thread.kill('SIGTERM'); + } + }, + exec: function (command /*, options, callback*/){ + var options = normalizeExecArgs.apply(null, arguments); + + // spawn + return spawn(options.file, options.args, { + cwd: options.cwd, + env: options.env, + gid: options.gid, + uid: options.uid, + windowsVerbatimArguments: !!options.windowsVerbatimArguments + }); + } +}; + +/** + * normalize exec args + * @param command + * @param options + * @returns {{cmd: *, file: *, args: *, options: *}} + */ +function normalizeExecArgs(command, options){ + var file, args; + + // Make a shallow copy before patching so we don't clobber the user's + // options object. + options = options || options; + + if (process.platform === 'win32') { + file = process.env.comspec || 'cmd.exe'; + args = ['/s', '/c', '"' + command + '"']; + options.windowsVerbatimArguments = true; + } else { + file = '/bin/sh'; + args = ['-c', command]; + } + + if (options.shell) { + file = options.shell; + } + + return { + cmd: command, + file: file, + args: args, + options: options + }; +} + +var cache = {}; + +module.exports = { + Emulator: Emulator, + start: function (){ + ipc.on('emulator', function (event, project, action){ + switch (action) { + case 'start': + if (!cache[project.name]) { + var emulator = new Emulator(project.command.name, project.command.value); + var send = function (type, data){ + event.sender.send(type, project, data); + }; + + emulator + .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; + } + break; + case 'stop': + if (cache[project.name]) { + cache[project.name].stop(); + + delete cache[project.name]; + } + break; + } + + }); + } +}; diff --git a/main.js b/main.js index 1de8517..3dfc1a7 100644 --- a/main.js +++ b/main.js @@ -16,6 +16,7 @@ var BrowserWindow = require('browser-window'); 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 // be closed automatically when the javascript object is GCed @@ -87,4 +88,7 @@ app.on('ready', function (){ // app configure new AppConfigure(mainWindow, mainTray); + + // emulator start + emulator.start(); }); diff --git a/static/js/app/index.js b/static/js/app/index.js index 7e7d59e..245dbfb 100644 --- a/static/js/app/index.js +++ b/static/js/app/index.js @@ -54,6 +54,9 @@ window.addEventListener('DOMContentLoaded', function (){ ipc.on('app-configure', function (event, command, configure){ switch (command) { + case 'ready': + ipc.send('app-configure', 'read'); + break; case 'refresh': if (app) { app.activeIndex = 0; @@ -69,5 +72,5 @@ window.addEventListener('DOMContentLoaded', function (){ } }); - ipc.send('app-configure', 'refresh'); + ipc.send('app-configure', 'ready'); }, false); diff --git a/static/js/components/dialog/dialog.html b/static/js/components/dialog/dialog.html new file mode 100644 index 0000000..62510fd --- /dev/null +++ b/static/js/components/dialog/dialog.html @@ -0,0 +1,10 @@ + + + + + Title + + + + + \ No newline at end of file diff --git a/static/js/components/dialog/index.js b/static/js/components/dialog/index.js new file mode 100644 index 0000000..96698dd --- /dev/null +++ b/static/js/components/dialog/index.js @@ -0,0 +1,7 @@ +/** + * Created by nuintun on 2015/11/26. + */ + +'use strict'; + +