Merge pull request #183 from JsSucks/utils-load-latest
Utils load latest
This commit is contained in:
commit
048abaeeed
|
@ -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",
|
||||
|
|
|
@ -15,6 +15,7 @@ const { FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('
|
|||
const { BrowserWindow, dialog } = require('electron');
|
||||
|
||||
const tests = true;
|
||||
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,8 @@ class BetterDiscord {
|
|||
const window = await this.waitForWindow();
|
||||
this.windowUtils = new WindowUtils({ window });
|
||||
|
||||
await FileUtils.ensureDirectory(paths.find(path => path.id === 'ext').path);
|
||||
|
||||
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 +185,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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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(), bindings()));
|
||||
});
|
||||
|
|
|
@ -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 lint && npm run build && gulp release"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue