-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
259 lines (235 loc) · 6.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const config = require("./config.json");
const app = require("electron").app;
const electron = require("electron");
const Menu = electron.Menu;
const BrowserWindow = require("electron").BrowserWindow;
const path = require("path");
const isDev = require("electron-is-dev");
const io = require("socket.io");
const server = io.listen(config.replSocketPort);
const util = require('util');
const childProcess = require("child_process");
const exec = util.promisify(childProcess.exec);
const fs = require("fs");
const appendFile = require("fs").appendFile;
const OscServer = require("node-osc").Server;
let mainWindow = null;
console.log("load config:", config);
const targetFile = path.join(
__dirname,
"/" + config.yodakaPath + "/" + config.spagoOptions.targetFile
);
const bundleFile = path.join(
__dirname,
"/" + "./" + config.yodakaPath + "/" + config.spagoOptions.bundlePath
);
const mainFile = path.join(
__dirname,
"/" + "./" + config.yodakaPath + "/" + config.spagoOptions.mainPath + ".purs"
);
try {
fs.statSync(targetFile);
} catch (err) {
if (err.code === "ENOENT") {
exec("touch " + targetFile, (error, stdout, stderr) => {
if (error) {
socket.emit("responseError", "File Preraring Error.");
}
});
}
}
// const doSpawn = () => {
// psci = childProcess.spawn(
// config.psciCommand,
// [
// config.psciOptions[0],
// "--",
// config.psciOptions[1],
// "--port " + config.psciPort
// ],
// {
// cwd: config.yodakaPath,
// shell: true
// }
// );
// };
console.log("Yodaka dir", path.join(process.cwd(), "/" + config.yodakaPath));
const bundleCompileFile = async code => {
await prapareFile();
const indented = indentation(code);
const addingCode = indented + "\n";
appendFile(mainFile, addingCode, err => {
if (err) throw err;
console.log("Writen Main Correctly");
});
};
const prapareFile = async () => {
return new Promise (resolve => {
exec("cat " + bundleFile + " >> " + mainFile, (error, stdout, stderr) => {
if (error) {
console.log("Write File Error", error);
}
resolve();
});
});
};
const cleanMain = async () => {
new Promise (resolve => {
exec('echo " " > ' + mainFile, (error, stdout, stderr) => {
if (error) {
console.log("Write File Error", error);
return;
}
resolve();
});
});
};
const doCompile = (socket, code) => {
return new Promise((resolve, reject) => {
bundleCompileFile(code);
const opts = {
cwd: path.join(process.cwd(), "/" + config.yodakaPath)
};
const command = config.spagoCommand;
exec(command, opts, (error, stdout, stderr) => {
if (error) {
console.error(`Compile error:: ${error}`);
socket.emit("responseError", "Compile Error with :: " + error);
return reject(error);
}
resolve();
});
});
};
const indentation = text => {
let text_ = text.split("\n");
let splited = [" \n"].concat(text_);
const final = splited.reduce((p, c, i) => {
if (i == 0) {
return p.concat(c + "\n");
} else {
return p.concat(" " + c + "\n");
}
});
return final;
};
/**
* socket io
*/
server.on("connection", socket => {
console.log("client connected");
/**
* interaction with repl stdio
* TODO: Want add repl process like: https://github.com/tidalcycles/atom-tidalcycles/blob/master/lib/repl.js#L54
*
* We do spago build here. We want to eveluate code in psci directly in the future.
* Now we can't evaluate multi-line code from outside of our console App.
* see: https://github.com/purescript/purescript/issues/934
*/
socket.on("repl", async code => {
await cleanMain();
console.log("build psci > ", code);
doCompile(socket, code)
.then(() => {
// psci.stdin.write(msg + "\n");
const resultCode = fs.readFileSync(targetFile, { encodeing: "utf8" });
socket.emit("response", resultCode);
})
.catch(e => {
console.log("Failed compiling :: ", e);
});
});
// psci.stdout.setEncoding("utf-8");
// psci.stdout.on("data", data => {
// console.log("on DATA", data);
// if (data.indexOf("PSCi, ") >= 0) {
// socket.emit("replLoaded");
// }
// socket.emit("response", data);
// });
// psci.stderr.setEncoding("utf-8");
// psci.stderr.on("data", data => {
// console.log(data);
// socket.emit("response", data);
// });
/**
* interaction with editor of render process
*/
console.log("sthart watching Main.purs");
/**]
* osc listen
*/
const oscServer = new OscServer(config.osc.port, config.osc.host);
oscServer.on("message", function(msg) {
// console.log("on osc msg:", msg);
socket.emit(msg[0], msg[1]);
});
});
const exportWeb = () => {
exec("npm run export", (error, stdout, stderr) => {
if (error) {
socket.emit("Error", "Web Exporting Error.");
}
console.log("Exported..");
if (process.platform==='darwin') { exec("open ./dist") }
});
}
/**
* menu settings
*/
const initWindowMenu = () => {
const template = [
{
label: 'Menu',
submenu: [
{
label: 'Quit',
click () { app.quit(); }
}
]
},
{
label: 'File',
submenu: [
{
label: 'Export',
click () { exportWeb() }
}
]
}
]
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
app.on("window-all-closed", function() {
if (process.platform != "darwin") {
app.quit();
}
});
app.on("ready", function() {
mainWindow = new BrowserWindow({
width: config.editor.windowWidth,
height: config.editor.windowHeight,
webPreferences: {
webSecurity: false,
nodeIntegration: true
}
// titleBarStyle: "hidden"/
});
initWindowMenu();
app.toggleMenubar = function() {
mainWindow.setMenuBarVisibility(!mainWindow.isMenuBarVisible());
};
app.toggleFullscreen = function() {
mainWindow.setFullScreen(!mainWindow.isFullScreen());
};
mainWindow.loadURL(
isDev
? "http://localhost:" + config.kusabiPort
: `file://${path.join(__dirname, "/dist/index.html")}`
);
mainWindow.on("closed", function() {
mainWindow = null;
app.quit();
});
});