update files

This commit is contained in:
nuintun 2015-11-26 15:16:32 +08:00
parent 0f6cc6bd35
commit a78ff09309
6 changed files with 171 additions and 6 deletions

View File

@ -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);
}
});
},

136
bin/emulator.js Normal file
View File

@ -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;
}
});
}
};

View File

@ -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();
});

View File

@ -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);

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

View File

@ -0,0 +1,7 @@
/**
* Created by nuintun on 2015/11/26.
*/
'use strict';