-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
354 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@import 'contextMenu'; | ||
@import 'keybindRecorder'; | ||
@import 'settings'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
const { resolve } = require('path'); | ||
const { readdirSync } = require('fs'); | ||
const { lstat } = require('fs').promises; | ||
|
||
const Theme = require('./theme'); | ||
|
||
module.exports = class StyleManager { | ||
constructor () { | ||
this.themesDir = resolve(__dirname, '..', '..', 'themes'); | ||
this.themes = new Map(); | ||
|
||
this.manifestKeys = [ 'name', 'version', 'description', 'author', 'license', 'theme' ]; | ||
} | ||
|
||
// Getters | ||
get (themeID) { | ||
return this.themes.get(themeID); | ||
} | ||
|
||
getThemes () { | ||
return [ ...this.themes.keys() ]; | ||
} | ||
|
||
isInstalled (theme) { | ||
return this.themes.has(theme); | ||
} | ||
|
||
isEnabled (theme) { | ||
return !powercord.settings.get('disabledThemes', []).includes(theme); | ||
} | ||
|
||
enable (themeID) { | ||
if (!this.get(themeID)) { | ||
throw new Error(`Tried to enable a non installed theme (${themeID})`); | ||
} | ||
|
||
powercord.settings.set( | ||
'disabledThemes', | ||
powercord.settings.get('disabledThemes', []).filter(p => p !== themeID) | ||
); | ||
|
||
this.themes.get(themeID).apply(); | ||
} | ||
|
||
disable (themeID) { | ||
const plugin = this.get(themeID); | ||
if (!plugin) { | ||
throw new Error(`Tried to disable a non installed theme (${themeID})`); | ||
} | ||
|
||
powercord.settings.set('disabledThemes', [ | ||
...powercord.settings.get('disabledThemes', []), | ||
themeID | ||
]); | ||
|
||
this.themes.get(themeID).remove(); | ||
} | ||
|
||
/* | ||
* @todo | ||
* async install (pluginID) { | ||
* await exec(`git clone https://github.com/powercord-org/${pluginID}`, this.pluginDir); | ||
* this.mount(pluginID); | ||
* } | ||
* | ||
* async uninstall (pluginID) { | ||
* if (pluginID.startsWith('pc-')) { | ||
* throw new Error(`You cannot uninstall an internal plugin. (Tried to uninstall ${pluginID})`); | ||
* } | ||
* | ||
* await this.unmount(pluginID); | ||
* await rmdirRf(resolve(this.pluginDir, pluginID)); | ||
* } | ||
*/ | ||
|
||
// Plugin CSS | ||
loadPluginCSS (themeID, file) { | ||
const theme = Theme.fromFile(themeID, file); | ||
this.themes.set(themeID, theme); | ||
theme.apply(); | ||
} | ||
|
||
// Start/Stop | ||
async loadThemes () { | ||
this.loadPluginCSS('powercord-core', resolve(__dirname, 'css', 'index.scss')); | ||
|
||
const files = readdirSync(this.themesDir); | ||
for (const filename of files) { | ||
if (filename === '.exists') { | ||
continue; | ||
} | ||
|
||
const themeID = filename.split('.').shift().toLowerCase(); | ||
const stat = await lstat(resolve(this.themesDir, filename)); | ||
let theme; | ||
|
||
try { | ||
if (stat.isFile()) { | ||
theme = Theme.fromFile(themeID, filename); | ||
} else { | ||
const manifest = require(resolve(this.themesDir, filename, 'powercord_manifest.json')); | ||
if (!this.manifestKeys.every(key => manifest.hasOwnProperty(key))) { | ||
console.error('%c[Powercord]', 'color: #257dd4', `Theme "${themeID}" doesn't have a valid manifest - Skipping`); | ||
continue; | ||
} | ||
|
||
theme = new Theme(themeID, { | ||
...manifest, | ||
theme: resolve(resolve(this.themesDir, filename, manifest.theme)) | ||
}); | ||
} | ||
} catch (e) { | ||
console.error('%c[Powercord]', 'color: #257dd4', `Theme "${themeID}" doesn't have a valid manifest or is not a valid file - Skipping`); | ||
continue; | ||
} | ||
|
||
this.themes.set(themeID, theme); | ||
|
||
if (!powercord.settings.get('disabledThemes', []).includes(themeID)) { | ||
theme.apply(); | ||
} | ||
} | ||
} | ||
|
||
unloadThemes () { | ||
[ ...this.themes.values() ].forEach(t => t.remove()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
const { createElement } = require('powercord/util'); | ||
const { resolve, dirname } = require('path'); | ||
const { readFile } = require('fs').promises; | ||
const { existsSync } = require('fs'); | ||
const watch = require('node-watch'); | ||
const { render } = require('sass'); | ||
|
||
const regex = /\.((s?c|le)ss|styl)$/; | ||
|
||
module.exports = class Theme { | ||
constructor (themeID, manifest) { | ||
// @todo: Validate more than actual theme. Not needed for now as plugins key is useless | ||
if (!regex.test(manifest.theme)) { | ||
throw new Error('Invalid theme file!'); | ||
} | ||
|
||
this.themeID = themeID; | ||
this.manifest = manifest; | ||
this.trackedFiles = []; | ||
} | ||
|
||
apply () { | ||
const element = document.head.querySelector(`#powercord-css-${this.themeID}`); | ||
if (!element) { | ||
document.head.appendChild( | ||
createElement('style', { id: `powercord-css-${this.themeID}` }) | ||
); | ||
} | ||
|
||
this.refresh(); | ||
} | ||
|
||
async refresh () { | ||
const element = document.head.querySelector(`#powercord-css-${this.themeID}`); | ||
if (!element) { | ||
return this.apply(); | ||
} | ||
|
||
const stylesheet = await this._compileStylesheet(); | ||
|
||
// Update CSS | ||
element.innerHTML = stylesheet.data; | ||
|
||
// Filter no longer used watchers | ||
this.trackedFiles = this.trackedFiles.filter(tf => { | ||
if (!stylesheet.includes.includes(tf.file)) { | ||
// noinspection JSPrimitiveTypeWrapperUsage | ||
stylesheet.includes = stylesheet.includes.filter(i => i !== tf.file); | ||
tf.watcher.close(); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
// Add new watchers | ||
stylesheet.includes.forEach(file => { | ||
const watcher = watch(file, this._handleUpdate.bind(this)); | ||
this.trackedFiles.push({ | ||
file, | ||
watcher | ||
}); | ||
}); | ||
} | ||
|
||
remove () { | ||
const element = document.head.querySelector(`#powercord-css-${this.themeID}`); | ||
if (element) { | ||
element.remove(); | ||
} | ||
} | ||
|
||
async _compileStylesheet () { | ||
let stylesheet = (await readFile(this.manifest.theme)).toString(); | ||
switch (this.manifest.theme.split('.').pop()) { | ||
case 'scss': | ||
stylesheet = await this._renderSCSS(stylesheet); | ||
break; | ||
case 'less': | ||
stylesheet = await this._renderLess(stylesheet); | ||
break; | ||
case 'styl': | ||
stylesheet = await this._renderStylus(stylesheet); | ||
break; | ||
default: | ||
stylesheet = { | ||
data: stylesheet, | ||
includes: [ this.manifest.theme ] | ||
}; | ||
} | ||
|
||
// @todo: Process the file and remove dynamic selectors | ||
return stylesheet; | ||
} | ||
|
||
_renderSCSS (scss) { | ||
return new Promise((res, rej) => { | ||
render({ | ||
data: scss, | ||
includePaths: [ dirname(this.manifest.theme) ], | ||
importer: (url, prev) => { | ||
url = url.replace('file:///', ''); | ||
if (existsSync(url)) { | ||
return { file: url }; | ||
} | ||
|
||
const prevFile = prev === 'stdin' ? this.manifest.theme : prev.replace(/https?:\/\/(?:[a-z]+\.)?discordapp\.com/i, ''); | ||
return { | ||
file: resolve(dirname(decodeURI(prevFile)), url).replace(/\\/g, '/') | ||
}; | ||
} | ||
}, (err, compiled) => { | ||
if (err) { | ||
return rej(err); | ||
} | ||
|
||
res({ | ||
data: compiled.css.toString(), | ||
includes: [ | ||
this.manifest.theme, | ||
...compiled.stats.includedFiles.map(f => decodeURI(f).replace(/\\/g, '/')) | ||
] | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
_renderLess (less) { | ||
// @todo | ||
return less; | ||
} | ||
|
||
_renderStylus (stylus) { | ||
// @todo | ||
return stylus; | ||
} | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
_handleUpdate (evt, _) { | ||
if (evt === 'update') { | ||
this.refresh(); | ||
} else if (evt === 'remove') { | ||
this.remove(); | ||
} | ||
} | ||
|
||
static fromFile (themeID, file) { | ||
return new Theme(themeID, { | ||
name: themeID, | ||
version: '1.0.0', | ||
description: 'No description provided', | ||
author: 'Unknown', | ||
license: 'Unknown', | ||
theme: file | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.