From 9f24d0d1ce46ca24ed29d66823a16e8d40571beb Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 20 Mar 2018 07:45:11 -0300 Subject: [PATCH 1/5] Fix package ver and make sure we always load the latest client script --- client/package.json | 2 +- core/src/main.js | 19 ++++++++++-- core/src/modules/utils.js | 62 +++++++++++++++++++++++++++++++++++++++ gulpfile.js | 9 +++++- package.json | 2 +- 5 files changed, 89 insertions(+), 5 deletions(-) diff --git a/client/package.json b/client/package.json index fde8f251..6e536f09 100644 --- a/client/package.json +++ b/client/package.json @@ -2,7 +2,7 @@ "name": "bdclient", "description": "BetterDiscord client package", "author": "Jiiks", - "version": "2.0.0.b", + "version": "2.0.0b", "homepage": "https://betterdiscord.net", "license": "MIT", "main": "index.js", diff --git a/core/src/main.js b/core/src/main.js index 91cfb7c7..b9a61886 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -14,7 +14,8 @@ const sass = require('node-sass'); const { FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('./modules'); const { BrowserWindow, dialog } = require('electron'); -const tests = true; +const tests = false; +const _basePath = __dirname; const _clientScript = tests ? path.resolve(__dirname, '..', '..', 'client', 'dist', 'betterdiscord.client.js') : path.resolve(__dirname, 'betterdiscord.client.js'); @@ -32,6 +33,7 @@ const _cssEditorPath = tests : path.resolve(__dirname, 'csseditor'); const paths = [ + { id: 'base', path: _basePath.replace(/\\/g, '/') }, { id: 'cs', path: _clientScript.replace(/\\/g, '/') }, { id: 'data', path: _dataPath.replace(/\\/g, '/') }, { id: 'ext', path: _extPath.replace(/\\/g, '/') }, @@ -142,6 +144,14 @@ class BetterDiscord { const window = await this.waitForWindow(); this.windowUtils = new WindowUtils({ window }); + await FileUtils.ensureDirectory(paths.find(path => path.id === 'ext').path); + + if (!tests) { + const files = await FileUtils.listDirectory(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'); + paths.find(path => path.id === 'cs').path = path.resolve(paths.find(path => path.id === 'base').path, latestCs).replace(/\\/g, '/'); + } + this.csseditor = new CSSEditor(this, paths.find(path => path.id === 'csseditor').path); this.windowUtils.events('did-get-response-details', () => this.ignite(this.windowUtils.window)); @@ -181,8 +191,13 @@ class BetterDiscord { window.webContents.executeJavaScript(`require("${sparkplug}");`); } - injectScripts(reload = false) { + async injectScripts(reload = false) { console.log(`RELOAD? ${reload}`); + if (!tests) { + const files = await FileUtils.listDirectory(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'); + paths.find(path => path.id === 'cs').path = path.resolve(paths.find(path => path.id === 'base').path, latestCs).replace(/\\/g, '/'); + } this.windowUtils.injectScript(paths.find(path => path.id === 'cs').path); } diff --git a/core/src/modules/utils.js b/core/src/modules/utils.js index a525322b..3a31dbb0 100644 --- a/core/src/modules/utils.js +++ b/core/src/modules/utils.js @@ -8,6 +8,8 @@ * LICENSE file in the root directory of this source tree. */ +// TODO Use common + const path = require('path'), fs = require('fs'); @@ -105,6 +107,66 @@ class FileUtils { throw(Object.assign(err, { path })); } } + + static async listDirectory(path) { + try { + await this.directoryExists(path); + return new Promise((resolve, reject) => { + fs.readdir(path, (err, files) => { + if (err) return reject(err); + resolve(files); + }); + }); + } catch (err) { + throw err; + } + } + + static filterFiles(files, filter, map) { + if (!map) return files.filter(filter); + return files.filter(filter).map(map); + } + + static resolveLatest(files, filter, map, prefix, suffix) { + let latest = null; + for (const file of this.filterFiles(files, filter, map)) { + const [major, minor, revision] = file.split('.'); + if (!major || !minor || !revision) continue; + if (!latest) { + latest = file; + continue; + } + + latest = file > latest ? file : latest; + } + return (prefix && suffix) ? `${prefix}${latest}${suffix}` : latest; + } + + static async createDirectory(path) { + return new Promise((resolve, reject) => { + fs.mkdir(path, err => { + if (err) { + if (err.code === 'EEXIST') return resolve(); + else return reject(err); + } + resolve(); + }); + }); + } + + static async ensureDirectory(path) { + try { + await this.directoryExists(path); + return true; + } catch (err) { + try { + await this.createDirectory(path); + return true; + } catch (err) { + throw err; + } + } + } } class WindowUtils extends Module { diff --git a/gulpfile.js b/gulpfile.js index 1eadf46d..31677bd3 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -39,6 +39,13 @@ const core3 = function() { return fs.writeFileSync('./release/index.js', `module.exports = require('./core.${corepkg.version}.js');`); } +const sparkplug = function() { + return pump([ + gulp.src('./core/dist/sparkplug.js'), + gulp.dest('./release') + ]); +} + const cssEditor = function() { return pump([ gulp.src('./csseditor/dist/**/*'), @@ -58,5 +65,5 @@ const bindings = function() { } gulp.task('release', function () { - del(['./release/**/*']).then(() => merge(client(), core(), core2(), core3(), cssEditor(), deps())); + del(['./release/**/*']).then(() => merge(client(), core(), core2(), core3(), sparkplug(), cssEditor(), deps())); }); diff --git a/package.json b/package.json index 1ccfb0f5..6bd66edd 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,6 @@ "lint": "eslint -f unix client/src core/src csseditor/src", "test": "npm run build && npm run lint", "build_node-sass": "node scripts/build-node-sass.js", - "release": "build && gulp release" + "release": "npm run build && gulp release" } } From c60411c901092211b512e3e4d2b7eff5610c07fd Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 20 Mar 2018 07:46:34 -0300 Subject: [PATCH 2/5] Don't have to resolve it twice --- core/src/main.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/src/main.js b/core/src/main.js index b9a61886..562fe0b7 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -146,12 +146,6 @@ class BetterDiscord { await FileUtils.ensureDirectory(paths.find(path => path.id === 'ext').path); - if (!tests) { - const files = await FileUtils.listDirectory(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'); - paths.find(path => path.id === 'cs').path = path.resolve(paths.find(path => path.id === 'base').path, latestCs).replace(/\\/g, '/'); - } - this.csseditor = new CSSEditor(this, paths.find(path => path.id === 'csseditor').path); this.windowUtils.events('did-get-response-details', () => this.ignite(this.windowUtils.window)); From 4ce32c415e18ef4c6fff22b291d18395a106797c Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 20 Mar 2018 07:47:03 -0300 Subject: [PATCH 3/5] copy prebuilt bindings --- gulpfile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gulpfile.js b/gulpfile.js index 31677bd3..35166675 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -65,5 +65,5 @@ const bindings = function() { } gulp.task('release', function () { - del(['./release/**/*']).then(() => merge(client(), core(), core2(), core3(), sparkplug(), cssEditor(), deps())); + del(['./release/**/*']).then(() => merge(client(), core(), core2(), core3(), sparkplug(), cssEditor(), deps(), bindings())); }); From ee87cbc25c90876732c0669c841639c013de77fb Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 20 Mar 2018 07:48:03 -0300 Subject: [PATCH 4/5] lint before building release --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6bd66edd..8ad374b7 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,6 @@ "lint": "eslint -f unix client/src core/src csseditor/src", "test": "npm run build && npm run lint", "build_node-sass": "node scripts/build-node-sass.js", - "release": "npm run build && gulp release" + "release": "npm run lint && npm run build && gulp release" } } From f3fa3c2ae2621c520cfecc426dfbc88d6ab7e3fd Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 20 Mar 2018 07:49:19 -0300 Subject: [PATCH 5/5] Tests should be true other than for building a release --- core/src/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main.js b/core/src/main.js index 562fe0b7..c9dc6fa4 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -14,7 +14,7 @@ const sass = require('node-sass'); const { FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('./modules'); const { BrowserWindow, dialog } = require('electron'); -const tests = false; +const tests = true; const _basePath = __dirname; const _clientScript = tests ? path.resolve(__dirname, '..', '..', 'client', 'dist', 'betterdiscord.client.js')