Cleanup core
This commit is contained in:
parent
a4a130bfc6
commit
dc44af6968
140
core/src/main.js
140
core/src/main.js
|
@ -15,43 +15,40 @@ const { FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('
|
||||||
const { BrowserWindow, dialog } = require('electron');
|
const { BrowserWindow, dialog } = require('electron');
|
||||||
|
|
||||||
const tests = true;
|
const tests = true;
|
||||||
const _basePath = __dirname;
|
|
||||||
|
const _basePath = tests ? path.resolve(__dirname, '..', '..') : __dirname;
|
||||||
|
const _baseDataPath = tests ? path.resolve(_basePath, 'tests') : _basePath;
|
||||||
|
|
||||||
|
const sparkplug = path.resolve(__dirname, 'sparkplug.js');
|
||||||
|
|
||||||
const _clientScript = tests
|
const _clientScript = tests
|
||||||
? path.resolve(__dirname, '..', '..', 'client', 'dist', 'betterdiscord.client.js')
|
? path.resolve(_basePath, 'client', 'dist', 'betterdiscord.client.js')
|
||||||
: path.resolve(__dirname, 'betterdiscord.client.js');
|
: path.resolve(_basePath, 'betterdiscord.client.js');
|
||||||
const _dataPath = tests
|
|
||||||
? path.resolve(__dirname, '..', '..', 'tests', 'data')
|
|
||||||
: path.resolve(__dirname, 'data');
|
|
||||||
const _extPath = tests
|
|
||||||
? path.resolve(__dirname, '..', '..', 'tests', 'ext')
|
|
||||||
: path.resolve(__dirname, 'ext');
|
|
||||||
const _pluginPath = path.resolve(_extPath, 'plugins');
|
|
||||||
const _themePath = path.resolve(_extPath, 'themes');
|
|
||||||
const _modulePath = path.resolve(_extPath, 'modules');
|
|
||||||
const _cssEditorPath = tests
|
const _cssEditorPath = tests
|
||||||
? path.resolve(__dirname, '..', '..', 'csseditor', 'dist')
|
? path.resolve(__dirname, '..', '..', 'csseditor', 'dist')
|
||||||
: path.resolve(__dirname, 'csseditor');
|
: path.resolve(__dirname, 'csseditor');
|
||||||
|
|
||||||
|
const _dataPath = path.resolve(_baseDataPath, 'data');
|
||||||
|
const _extPath = path.resolve(_baseDataPath, 'ext');
|
||||||
|
const _pluginPath = path.resolve(_extPath, 'plugins');
|
||||||
|
const _themePath = path.resolve(_extPath, 'themes');
|
||||||
|
const _modulePath = path.resolve(_extPath, 'modules');
|
||||||
|
|
||||||
const paths = [
|
const paths = [
|
||||||
{ id: 'base', path: _basePath.replace(/\\/g, '/') },
|
{ id: 'base', path: _basePath },
|
||||||
{ id: 'cs', path: _clientScript.replace(/\\/g, '/') },
|
{ id: 'cs', path: _clientScript },
|
||||||
{ id: 'data', path: _dataPath.replace(/\\/g, '/') },
|
{ id: 'data', path: _dataPath },
|
||||||
{ id: 'ext', path: _extPath.replace(/\\/g, '/') },
|
{ id: 'ext', path: _extPath },
|
||||||
{ id: 'plugins', path: _pluginPath.replace(/\\/g, '/') },
|
{ id: 'plugins', path: _pluginPath },
|
||||||
{ id: 'themes', path: _themePath.replace(/\\/g, '/') },
|
{ id: 'themes', path: _themePath },
|
||||||
{ id: 'modules', path: _modulePath.replace(/\\/g, '/') },
|
{ id: 'modules', path: _modulePath },
|
||||||
{ id: 'csseditor', path: _cssEditorPath.replace(/\\/g, '/') }
|
{ id: 'csseditor', path: _cssEditorPath }
|
||||||
];
|
];
|
||||||
|
|
||||||
const sparkplug = path.resolve(__dirname, 'sparkplug.js').replace(/\\/g, '/');
|
|
||||||
|
|
||||||
const Common = {};
|
|
||||||
const globals = {
|
const globals = {
|
||||||
version: '2.0.0a',
|
version: '2.0.0a',
|
||||||
paths
|
paths
|
||||||
}
|
};
|
||||||
|
|
||||||
const dbInstance = new Database(paths.find(path => path.id === 'data').path);
|
|
||||||
|
|
||||||
class Comms {
|
class Comms {
|
||||||
|
|
||||||
|
@ -61,9 +58,7 @@ class Comms {
|
||||||
}
|
}
|
||||||
|
|
||||||
initListeners() {
|
initListeners() {
|
||||||
BDIpc.on('bd-getConfig', o => {
|
BDIpc.on('bd-getConfig', o => o.reply(this.bd.config.config));
|
||||||
o.reply(Common.Config.config);
|
|
||||||
});
|
|
||||||
|
|
||||||
BDIpc.on('bd-sendToDiscord', event => this.bd.windowUtils.send(event.args.channel, event.args.message));
|
BDIpc.on('bd-sendToDiscord', event => this.bd.windowUtils.send(event.args.channel, event.args.message));
|
||||||
|
|
||||||
|
@ -71,9 +66,6 @@ class Comms {
|
||||||
// BDIpc.on('bd-setScss', o => this.bd.csseditor.setSCSS(o.args.scss));
|
// BDIpc.on('bd-setScss', o => this.bd.csseditor.setSCSS(o.args.scss));
|
||||||
BDIpc.on('bd-sendToCssEditor', o => this.bd.csseditor.send(o.args.channel, o.args.data));
|
BDIpc.on('bd-sendToCssEditor', o => this.bd.csseditor.send(o.args.channel, o.args.data));
|
||||||
|
|
||||||
BDIpc.on('bd-readFile', this.readFile);
|
|
||||||
BDIpc.on('bd-readJson', o => this.readFile(o, true));
|
|
||||||
|
|
||||||
BDIpc.on('bd-native-open', o => {
|
BDIpc.on('bd-native-open', o => {
|
||||||
dialog.showOpenDialog(BrowserWindow.fromWebContents(o.ipcEvent.sender), o.args, filenames => {
|
dialog.showOpenDialog(BrowserWindow.fromWebContents(o.ipcEvent.sender), o.args, filenames => {
|
||||||
o.reply(filenames);
|
o.reply(filenames);
|
||||||
|
@ -88,36 +80,22 @@ class Comms {
|
||||||
}
|
}
|
||||||
|
|
||||||
sass.render(o.args, (err, result) => {
|
sass.render(o.args, (err, result) => {
|
||||||
if (err) {
|
if (err) return o.reply({ err });
|
||||||
o.reply({ err });
|
else o.reply(result);
|
||||||
return;
|
|
||||||
}
|
|
||||||
o.reply(result);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
BDIpc.on('bd-dba', o => {
|
BDIpc.on('bd-dba', o => {
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const ret = await dbInstance.exec(o.args);
|
o.reply(await this.bd.dbInstance.exec(o.args));
|
||||||
o.reply(ret);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
o.reply({err});
|
o.reply({ err });
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async readFile(o, json) {
|
|
||||||
const { path } = o.args;
|
|
||||||
try {
|
|
||||||
const readFile = json ? await FileUtils.readJsonFromFile(path) : await FileUtils.readFile(path);
|
|
||||||
o.reply(readFile);
|
|
||||||
} catch (err) {
|
|
||||||
o.reply(err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async send(channel, message) {
|
async send(channel, message) {
|
||||||
BDIpc.send(channel, message);
|
BDIpc.send(channel, message);
|
||||||
}
|
}
|
||||||
|
@ -135,23 +113,25 @@ class BetterDiscord {
|
||||||
|
|
||||||
this.injectScripts = this.injectScripts.bind(this);
|
this.injectScripts = this.injectScripts.bind(this);
|
||||||
this.ignite = this.ignite.bind(this);
|
this.ignite = this.ignite.bind(this);
|
||||||
Common.Config = new Config(globals);
|
|
||||||
|
this.config = new Config(args || globals);
|
||||||
|
this.dbInstance = new Database(this.config.paths.find(path => path.id === 'data').path);
|
||||||
this.comms = new Comms(this);
|
this.comms = new Comms(this);
|
||||||
|
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
const window = await this.waitForWindow();
|
await this.waitForWindowUtils();
|
||||||
this.windowUtils = new WindowUtils({ window });
|
|
||||||
|
|
||||||
await FileUtils.ensureDirectory(paths.find(path => path.id === 'ext').path);
|
await FileUtils.ensureDirectory(this.config.paths.find(path => path.id === 'ext').path);
|
||||||
|
|
||||||
this.csseditor = new CSSEditor(this, paths.find(path => path.id === 'csseditor').path);
|
this.csseditor = new CSSEditor(this, this.config.paths.find(path => path.id === 'csseditor').path);
|
||||||
|
|
||||||
this.windowUtils.events('did-get-response-details', () => this.ignite(this.windowUtils.window));
|
this.windowUtils.on('did-get-response-details', () => this.ignite());
|
||||||
this.windowUtils.events('did-finish-load', e => this.injectScripts(true));
|
this.windowUtils.on('did-finish-load', e => this.injectScripts(true));
|
||||||
|
|
||||||
this.windowUtils.events('did-navigate-in-page', (event, url, isMainFrame) => {
|
this.windowUtils.on('did-navigate-in-page', (event, url, isMainFrame) => {
|
||||||
this.windowUtils.send('did-navigate-in-page', { event, url, isMainFrame });
|
this.windowUtils.send('did-navigate-in-page', { event, url, isMainFrame });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -166,10 +146,8 @@ class BetterDiscord {
|
||||||
const defer = setInterval(() => {
|
const defer = setInterval(() => {
|
||||||
const windows = BrowserWindow.getAllWindows();
|
const windows = BrowserWindow.getAllWindows();
|
||||||
|
|
||||||
if (windows.length > 0) {
|
for (let window of windows) {
|
||||||
windows.forEach(window => {
|
BetterDiscord.ignite(window);
|
||||||
self.ignite(window);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (windows.length === 1 && windows[0].webContents.getURL().includes('discordapp.com')) {
|
if (windows.length === 1 && windows[0].webContents.getURL().includes('discordapp.com')) {
|
||||||
|
@ -180,23 +158,45 @@ class BetterDiscord {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ignite(window) {
|
async waitForWindowUtils() {
|
||||||
//Hook things that Discord removes from global. These will be removed again in the client script
|
if (this.windowUtils) return this.windowUtils;
|
||||||
window.webContents.executeJavaScript(`require("${sparkplug}");`);
|
const window = await this.waitForWindow();
|
||||||
|
return this.windowUtils = new WindowUtils({ window });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get window() {
|
||||||
|
return this.windowUtils ? this.windowUtils.window : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks things that Discord removes from global. These will be removed again in the client script.
|
||||||
|
*/
|
||||||
|
ignite() {
|
||||||
|
return BetterDiscord.ignite(this.window);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hooks things that Discord removes from global. These will be removed again in the client script.
|
||||||
|
* @param {BrowserWindow} window The window to inject the sparkplug script into
|
||||||
|
*/
|
||||||
|
static ignite(window) {
|
||||||
|
return WindowUtils.injectScript(window, sparkplug);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Injects the client script into the main window.
|
||||||
|
* @param {Boolean} reload Whether the main window was reloaded
|
||||||
|
*/
|
||||||
async injectScripts(reload = false) {
|
async injectScripts(reload = false) {
|
||||||
console.log(`RELOAD? ${reload}`);
|
console.log(`RELOAD? ${reload}`);
|
||||||
if (!tests) {
|
if (!tests) {
|
||||||
const files = await FileUtils.listDirectory(paths.find(path => path.id === 'base').path);
|
const files = await FileUtils.listDirectory(this.config.paths.find(path => path.id === 'base').path);
|
||||||
const latestCs = FileUtils.resolveLatest(files, file => file.endsWith('.js') && file.startsWith('client.'), file => file.replace('client.', '').replace('.js', ''), 'client.', '.js');
|
const latestCs = FileUtils.resolveLatest(files, file => file.endsWith('.js') && file.startsWith('client.'), file => file.replace('client.', '').replace('.js', ''), 'client.', '.js');
|
||||||
paths.find(path => path.id === 'cs').path = path.resolve(paths.find(path => path.id === 'base').path, latestCs).replace(/\\/g, '/');
|
this.config.paths.find(path => path.id === 'cs').path = path.resolve(this.config.paths.find(path => path.id === 'base').path, latestCs);
|
||||||
}
|
}
|
||||||
this.windowUtils.injectScript(paths.find(path => path.id === 'cs').path);
|
return this.windowUtils.injectScript(this.config.paths.find(path => path.id === 'cs').path);
|
||||||
}
|
}
|
||||||
|
|
||||||
get fileUtils() { return FileUtils; }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -5,14 +5,15 @@
|
||||||
* https://github.com/JsSucks - https://betterdiscord.net
|
* https://github.com/JsSucks - https://betterdiscord.net
|
||||||
*
|
*
|
||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const { Module } = require('./modulebase');
|
const { Module } = require('./modulebase');
|
||||||
|
|
||||||
const { ipcMain } = require('electron');
|
const { ipcMain } = require('electron');
|
||||||
|
|
||||||
class BDIpcEvent extends Module {
|
class BDIpcEvent extends Module {
|
||||||
|
|
||||||
constructor(event, args) {
|
constructor(event, args) {
|
||||||
super(args);
|
super(args);
|
||||||
this.ipcEvent = event;
|
this.ipcEvent = event;
|
||||||
|
@ -34,10 +35,9 @@ class BDIpcEvent extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
class BDIpc {
|
class BDIpc {
|
||||||
|
|
||||||
static on(channel, cb) {
|
static on(channel, cb) {
|
||||||
ipcMain.on(channel, (event, args) => cb(new BDIpcEvent(event, args)));
|
ipcMain.on(channel, (event, args) => cb(new BDIpcEvent(event, args)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { BDIpc };
|
module.exports = { BDIpc };
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
* https://github.com/JsSucks - https://betterdiscord.net
|
* https://github.com/JsSucks - https://betterdiscord.net
|
||||||
*
|
*
|
||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const { Module } = require('./modulebase');
|
const { Module } = require('./modulebase');
|
||||||
|
@ -22,11 +22,11 @@ class Config extends Module {
|
||||||
|
|
||||||
get config() {
|
get config() {
|
||||||
return {
|
return {
|
||||||
'version': this.version,
|
version: this.version,
|
||||||
'paths': this.paths
|
paths: this.paths
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { Config };
|
module.exports = { Config };
|
||||||
|
|
|
@ -22,6 +22,10 @@ class CSSEditor extends Module {
|
||||||
this.bd = bd;
|
this.bd = bd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens an editor and replies to an IPC event.
|
||||||
|
* @param {BDIpcEvent} event
|
||||||
|
*/
|
||||||
openEditor(o) {
|
openEditor(o) {
|
||||||
if (this.editor) {
|
if (this.editor) {
|
||||||
if (this.editor.isFocused()) return;
|
if (this.editor.isFocused()) return;
|
||||||
|
@ -32,12 +36,7 @@ class CSSEditor extends Module {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = this.options;
|
const options = Object.assign({}, this.options, o.message);
|
||||||
for (let option in o.args) {
|
|
||||||
if (o.args.hasOwnProperty(option)) {
|
|
||||||
options[option] = o.args[option];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.editor = new BrowserWindow(options);
|
this.editor = new BrowserWindow(options);
|
||||||
this.editor.loadURL('about:blank');
|
this.editor.loadURL('about:blank');
|
||||||
|
@ -59,20 +58,34 @@ class CSSEditor extends Module {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the SCSS in the editor.
|
||||||
|
*/
|
||||||
setSCSS(scss) {
|
setSCSS(scss) {
|
||||||
this.send('set-scss', scss);
|
this.send('set-scss', scss);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends data to the editor.
|
||||||
|
* @param {String} channel
|
||||||
|
* @param {Any} data
|
||||||
|
*/
|
||||||
send(channel, data) {
|
send(channel, data) {
|
||||||
if (!this.editor) return;
|
if (!this.editor) return;
|
||||||
this.editor.webContents.send(channel, data);
|
this.editor.webContents.send(channel, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the CSS editor's always on top flag.
|
||||||
|
*/
|
||||||
set alwaysOnTop(state) {
|
set alwaysOnTop(state) {
|
||||||
if (!this.editor) return;
|
if (!this.editor) return;
|
||||||
this.editor.setAlwaysOnTop(state);
|
this.editor.setAlwaysOnTop(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default options to pass to BrowserWindow.
|
||||||
|
*/
|
||||||
get options() {
|
get options() {
|
||||||
return {
|
return {
|
||||||
width: 800,
|
width: 800,
|
||||||
|
@ -81,7 +94,7 @@ class CSSEditor extends Module {
|
||||||
frame: false
|
frame: false
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { CSSEditor };
|
module.exports = { CSSEditor };
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
const Datastore = require('nedb');
|
const Datastore = require('nedb');
|
||||||
|
|
||||||
class Database {
|
class Database {
|
||||||
|
|
||||||
constructor(dbPath) {
|
constructor(dbPath) {
|
||||||
this.exec = this.exec.bind(this);
|
this.exec = this.exec.bind(this);
|
||||||
|
|
|
@ -5,20 +5,19 @@
|
||||||
* https://github.com/JsSucks - https://betterdiscord.net
|
* https://github.com/JsSucks - https://betterdiscord.net
|
||||||
*
|
*
|
||||||
* This source code is licensed under the MIT license found in the
|
* This source code is licensed under the MIT license found in the
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
Base Module that every non-static module should extend
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Module that every non-static module should extend.
|
||||||
|
*/
|
||||||
class Module {
|
class Module {
|
||||||
|
|
||||||
constructor(args) {
|
constructor(args) {
|
||||||
this.__ = {
|
this.__ = {
|
||||||
state: args,
|
state: args,
|
||||||
args
|
args
|
||||||
}
|
};
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,4 +32,4 @@ class Module {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { Module };
|
module.exports = { Module };
|
||||||
|
|
|
@ -10,43 +10,35 @@
|
||||||
|
|
||||||
// TODO Use common
|
// TODO Use common
|
||||||
|
|
||||||
const
|
const path = require('path');
|
||||||
path = require('path'),
|
const fs = require('fs');
|
||||||
fs = require('fs');
|
|
||||||
|
|
||||||
const { Module } = require('./modulebase');
|
const { Module } = require('./modulebase');
|
||||||
|
|
||||||
class Utils {
|
class Utils {
|
||||||
|
|
||||||
static async tryParseJson(jsonString) {
|
static async tryParseJson(jsonString) {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(jsonString);
|
return JSON.parse(jsonString);
|
||||||
}catch(err) {
|
} catch (err) {
|
||||||
throw ({
|
throw ({
|
||||||
'message': 'Failed to parse json',
|
message: 'Failed to parse json',
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static get timestamp() {
|
|
||||||
return 'Timestamp';
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class FileUtils {
|
class FileUtils {
|
||||||
|
|
||||||
static async fileExists(path) {
|
static async fileExists(path) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.stat(path, (err, stats) => {
|
fs.stat(path, (err, stats) => {
|
||||||
if(err) return reject({
|
if(err) return reject({
|
||||||
'message': `No such file or directory: ${err.path}`,
|
message: `No such file or directory: ${err.path}`,
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!stats.isFile()) return reject({
|
if(!stats.isFile()) return reject({
|
||||||
'message': `Not a file: ${path}`,
|
message: `Not a file: ${path}`,
|
||||||
stats
|
stats
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -59,12 +51,12 @@ class FileUtils {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.stat(path, (err, stats) => {
|
fs.stat(path, (err, stats) => {
|
||||||
if(err) return reject({
|
if(err) return reject({
|
||||||
'message': `Directory does not exist: ${path}`,
|
message: `Directory does not exist: ${path}`,
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
|
|
||||||
if(!stats.isDirectory()) return reject({
|
if(!stats.isDirectory()) return reject({
|
||||||
'message': `Not a directory: ${path}`,
|
message: `Not a directory: ${path}`,
|
||||||
stats
|
stats
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -76,14 +68,14 @@ class FileUtils {
|
||||||
static async readFile(path) {
|
static async readFile(path) {
|
||||||
try {
|
try {
|
||||||
await this.fileExists(path);
|
await this.fileExists(path);
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
throw(err);
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.readFile(path, 'utf-8', (err, data) => {
|
fs.readFile(path, 'utf-8', (err, data) => {
|
||||||
if(err) reject({
|
if(err) return reject({
|
||||||
'message': `Could not read file: ${path}`,
|
message: `Could not read file: ${path}`,
|
||||||
err
|
err
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -97,14 +89,13 @@ class FileUtils {
|
||||||
try {
|
try {
|
||||||
readFile = await this.readFile(path);
|
readFile = await this.readFile(path);
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
throw(err);
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsed = await Utils.tryParseJson(readFile);
|
return await Utils.tryParseJson(readFile);
|
||||||
return parsed;
|
} catch (err) {
|
||||||
} catch(err) {
|
throw Object.assign(err, { path });
|
||||||
throw(Object.assign(err, { path }));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,8 +104,8 @@ class FileUtils {
|
||||||
await this.directoryExists(path);
|
await this.directoryExists(path);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.readdir(path, (err, files) => {
|
fs.readdir(path, (err, files) => {
|
||||||
if (err) return reject(err);
|
if (err) reject(err);
|
||||||
resolve(files);
|
else resolve(files);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -145,11 +136,8 @@ class FileUtils {
|
||||||
static async createDirectory(path) {
|
static async createDirectory(path) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.mkdir(path, err => {
|
fs.mkdir(path, err => {
|
||||||
if (err) {
|
if (err) reject(err);
|
||||||
if (err.code === 'EEXIST') return resolve();
|
else resolve();
|
||||||
else return reject(err);
|
|
||||||
}
|
|
||||||
resolve();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -170,7 +158,6 @@ class FileUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
class WindowUtils extends Module {
|
class WindowUtils extends Module {
|
||||||
|
|
||||||
bindings() {
|
bindings() {
|
||||||
this.openDevTools = this.openDevTools.bind(this);
|
this.openDevTools = this.openDevTools.bind(this);
|
||||||
this.executeJavascript = this.executeJavascript.bind(this);
|
this.executeJavascript = this.executeJavascript.bind(this);
|
||||||
|
@ -194,16 +181,20 @@ class WindowUtils extends Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
injectScript(fpath, variable) {
|
injectScript(fpath, variable) {
|
||||||
console.log(`Injecting: ${fpath}`);
|
return WindowUtils.injectScript(this.window, fpath, variable);
|
||||||
|
}
|
||||||
|
|
||||||
|
static injectScript(window, fpath, variable) {
|
||||||
|
// console.log(`Injecting: ${fpath}`);
|
||||||
|
|
||||||
const escaped_path = fpath.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
const escaped_path = fpath.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
||||||
const escaped_variable = variable ? variable.replace(/\\/g, '\\\\').replace(/"/g, '\\"') : null;
|
const escaped_variable = variable ? variable.replace(/\\/g, '\\\\').replace(/"/g, '\\"') : null;
|
||||||
|
|
||||||
if (variable) this.executeJavascript(`window["${escaped_variable}"] = require("${escaped_path}");`);
|
if (variable) window.executeJavaScript(`window["${escaped_variable}"] = require("${escaped_path}");`);
|
||||||
else this.executeJavascript(`require("${escaped_path}");`);
|
else window.executeJavaScript(`require("${escaped_path}");`);
|
||||||
}
|
}
|
||||||
|
|
||||||
events(event, callback) {
|
on(event, callback) {
|
||||||
this.webContents.on(event, callback);
|
this.webContents.on(event, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +202,6 @@ class WindowUtils extends Module {
|
||||||
channel = channel.startsWith('bd-') ? channel : `bd-${channel}`;
|
channel = channel.startsWith('bd-') ? channel : `bd-${channel}`;
|
||||||
this.webContents.send(channel, message);
|
this.webContents.send(channel, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -33,18 +33,18 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import '../../node_modules/codemirror/addon/scroll/simplescrollbars.js';
|
|
||||||
import '../../node_modules/codemirror/mode/css/css.js';
|
|
||||||
import '../../node_modules/codemirror/addon/hint/css-hint.js';
|
|
||||||
import '../../node_modules/codemirror/addon/search/search.js';
|
|
||||||
import '../../node_modules/codemirror/addon/search/searchcursor.js';
|
|
||||||
import '../../node_modules/codemirror/addon/search/jump-to-line.js';
|
|
||||||
import '../../node_modules/codemirror/addon/dialog/dialog.js';
|
|
||||||
import '../../node_modules/codemirror/addon/hint/show-hint.js';
|
|
||||||
|
|
||||||
import BDIpc from './BDIpc';
|
import BDIpc from './BDIpc';
|
||||||
|
|
||||||
const { remote } = window.require('electron');
|
import { remote } from 'electron';
|
||||||
|
|
||||||
|
import 'codemirror/addon/scroll/simplescrollbars.js';
|
||||||
|
import 'codemirror/mode/css/css.js';
|
||||||
|
import 'codemirror/addon/hint/css-hint.js';
|
||||||
|
import 'codemirror/addon/search/search.js';
|
||||||
|
import 'codemirror/addon/search/searchcursor.js';
|
||||||
|
import 'codemirror/addon/search/jump-to-line.js';
|
||||||
|
import 'codemirror/addon/dialog/dialog.js';
|
||||||
|
import 'codemirror/addon/hint/show-hint.js';
|
||||||
|
|
||||||
const ExcludedIntelliSenseTriggerKeys = {
|
const ExcludedIntelliSenseTriggerKeys = {
|
||||||
'8': 'backspace',
|
'8': 'backspace',
|
||||||
|
|
Loading…
Reference in New Issue