UNPKG

5.21 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.10.0
2(function() {
3 var Browser, BrowserWindow, Locker, Watcher, app, chokidar, cson, exec, fork, fs, fse, ipc, mainWindow,
4 bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
5
6 require('crash-reporter').start();
7
8 fse = require("fs-extra");
9
10 chokidar = require("chokidar");
11
12 cson = require("cson");
13
14 fs = require("fs");
15
16 exec = require("child_process").exec;
17
18 fork = require("child_process").fork;
19
20 app = require('app');
21
22 BrowserWindow = require('browser-window');
23
24 ipc = require('electron').ipcMain;
25
26 mainWindow = null;
27
28 Locker = (function() {
29 function Locker() {
30 this.lock = bind(this.lock, this);
31 this.check = bind(this.check, this);
32 this.unlock = bind(this.unlock, this);
33 }
34
35 Locker.prototype.path = ".lock";
36
37 Locker.prototype.unlock = function() {
38 return fse.removeSync(this.path);
39 };
40
41 Locker.prototype.check = function() {
42 if (fs.existsSync(this.path)) {
43 console.log("exists " + this.path);
44 return app.quit();
45 }
46 };
47
48 Locker.prototype.lock = function() {
49 return fse.ensureFileSync(this.path);
50 };
51
52 return Locker;
53
54 })();
55
56 Watcher = (function() {
57 function Watcher() {
58 this.start = bind(this.start, this);
59 this.restart = bind(this.restart, this);
60 this.firstReadObj = {};
61 }
62
63 Watcher.prototype.restart = function() {
64 var cmd;
65 cmd = fse.readJsonSync("package.json").scripts.electron;
66 exec(cmd);
67 return setTimeout(app.quit, 0);
68 };
69
70 Watcher.prototype.start = function() {
71 return chokidar.watch(["./browser/.build/"]).on("change", (function(_this) {
72 return function(path) {
73 if (!_this.firstReadObj[path]) {
74 return _this.firstReadObj[path] = true;
75 }
76 if (!(path.match(/\.js$/) || _this.restartFlg)) {
77 return;
78 }
79 _this.restartFlg = true;
80 Locker.prototype.unlock();
81 return _this.restart();
82 };
83 })(this));
84 };
85
86 return Watcher;
87
88 })();
89
90 module.exports = Browser = (function() {
91 function Browser() {
92 this.sendMsg = bind(this.sendMsg, this);
93 this.ipcEvent = bind(this.ipcEvent, this);
94 this.start = bind(this.start, this);
95 this.init = bind(this.init, this);
96 }
97
98 Browser.prototype.configCson = ".config.cson";
99
100 Browser.prototype.init = function() {
101 var error;
102 try {
103 this.config = cson.load(this.configCson);
104 } catch (error) {
105 this.config = cson.load(this.configCson.slice(1));
106 }
107 this.option = this.config["browser-window"];
108 return this;
109 };
110
111 Browser.prototype.start = function(url) {
112 this.url = url;
113 Locker.prototype.check();
114 Locker.prototype.lock();
115 this.watcher = new Watcher();
116 this.watcher.start();
117 this.ipcEvent();
118 app.on('window-all-closed', (function(_this) {
119 return function() {
120 if (process.platform !== 'darwin') {
121 return app.quit();
122 }
123 };
124 })(this));
125 return app.on('ready', (function(_this) {
126 return function() {
127 var option;
128 option = JSON.parse(JSON.stringify(_this.option));
129 option["web-preferences"].preload = "" + (process.cwd()) + _this.option["web-preferences"].preload;
130 mainWindow = new BrowserWindow(option);
131 mainWindow.setAlwaysOnTop(true);
132 if (!_this.url.match(/^(http|\/\/)/)) {
133 _this.url = "file://" + (process.cwd()) + _this.url;
134 }
135 mainWindow.loadUrl(_this.url);
136 mainWindow.openDevTools();
137 mainWindow.webContents.on("did-finish-load", function() {
138 if (!_this.option["always-on-top"]) {
139 return mainWindow != null ? mainWindow.setAlwaysOnTop(false) : void 0;
140 }
141 });
142 mainWindow.on('closed', function() {
143 return mainWindow = null;
144 });
145 mainWindow.on("close", function(e) {
146 var csonString, wh, xy;
147 Locker.prototype.unlock();
148 if (!mainWindow.getPosition) {
149 return;
150 }
151 xy = mainWindow.getPosition();
152 wh = mainWindow.getSize();
153 _this.option.x = xy[0];
154 _this.option.y = xy[1];
155 _this.option.width = wh[0];
156 _this.option.height = wh[1];
157 csonString = cson.createCSONString(_this.config, {
158 indent: " "
159 });
160 fs.writeFileSync(_this.configCson, csonString);
161 return mainWindow = null;
162 });
163 return _this.startCompiler();
164 };
165 })(this));
166 };
167
168 Browser.prototype.ipcEvent = function() {
169 return ipc.on("restart", this.watcher.restart);
170 };
171
172 Browser.prototype.startCompiler = function() {
173 return exec(fse.readJsonSync("package.json").scripts.watchAll).stdout.on("data", this.sendMsg);
174 };
175
176 Browser.prototype.sendMsg = function(msg) {
177 return mainWindow != null ? mainWindow.webContents.send("browser send msg", msg) : void 0;
178 };
179
180 return Browser;
181
182 })();
183
184}).call(this);