add new paths and enable snippet import
This commit is contained in:
parent
145d61fe5e
commit
7e10de32cb
|
@ -207,10 +207,13 @@ export class BetterDiscord {
|
||||||
|
|
||||||
async ensureDirectories() {
|
async ensureDirectories() {
|
||||||
await FileUtils.ensureDirectory(this.config.getPath('ext'));
|
await FileUtils.ensureDirectory(this.config.getPath('ext'));
|
||||||
|
await FileUtils.ensureDirectory(this.config.getPath('user'));
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
FileUtils.ensureDirectory(this.config.getPath('plugins')),
|
FileUtils.ensureDirectory(this.config.getPath('plugins')),
|
||||||
FileUtils.ensureDirectory(this.config.getPath('themes')),
|
FileUtils.ensureDirectory(this.config.getPath('themes')),
|
||||||
FileUtils.ensureDirectory(this.config.getPath('modules'))
|
FileUtils.ensureDirectory(this.config.getPath('modules')),
|
||||||
|
FileUtils.ensureDirectory(this.config.getPath('userfiles')),
|
||||||
|
FileUtils.ensureDirectory(this.config.getPath('snippets'))
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,17 +256,23 @@ export class BetterDiscord {
|
||||||
* Add extra paths to config
|
* Add extra paths to config
|
||||||
*/
|
*/
|
||||||
extraPaths() {
|
extraPaths() {
|
||||||
const baseDataPath = path.resolve(this.config.getPath('data'), '..');
|
const base = path.resolve(this.config.getPath('data'), '..');
|
||||||
const extPath = path.resolve(baseDataPath, 'ext');
|
const ext = path.resolve(base, 'ext');
|
||||||
const pluginPath = path.resolve(extPath, 'plugins');
|
const plugins = path.resolve(ext, 'plugins');
|
||||||
const themePath = path.resolve(extPath, 'themes');
|
const themes = path.resolve(ext, 'themes');
|
||||||
const modulePath = path.resolve(extPath, 'modules');
|
const modules = path.resolve(ext, 'modules');
|
||||||
|
const user = path.resolve(this.config.getPath('data'), 'user');
|
||||||
|
const userfiles = path.resolve(user, 'files');
|
||||||
|
const snippets = path.resolve(user, 'snippets');
|
||||||
|
|
||||||
this.config.addPath('base', baseDataPath);
|
this.config.addPath('base', base);
|
||||||
this.config.addPath('ext', extPath);
|
this.config.addPath('ext', ext);
|
||||||
this.config.addPath('plugins', pluginPath);
|
this.config.addPath('plugins', plugins);
|
||||||
this.config.addPath('themes', themePath);
|
this.config.addPath('themes', themes);
|
||||||
this.config.addPath('modules', modulePath);
|
this.config.addPath('modules', modules);
|
||||||
|
this.config.addPath('user', user);
|
||||||
|
this.config.addPath('userfiles', userfiles);
|
||||||
|
this.config.addPath('snippets', snippets);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -35,39 +35,59 @@ export default class Editor extends Module {
|
||||||
});
|
});
|
||||||
|
|
||||||
BDIpc.on('editor-getFiles', async (event) => {
|
BDIpc.on('editor-getFiles', async (event) => {
|
||||||
const exclude = ['db.json', 'emotes.json', 'storage', 'user.settings.json', 'window.json'];
|
try {
|
||||||
const listFiles = await FileUtils.listDirectory(this.bd.config.getPath('data'));
|
const files = await FileUtils.listDirectory(this.bd.config.getPath('userfiles'));
|
||||||
const files = listFiles.filter(file => !exclude.includes(file));
|
|
||||||
|
|
||||||
const constructFiles = files.map(async file => {
|
const constructFiles = files.map(async file => {
|
||||||
const content = await FileUtils.readFile(path.resolve(this.bd.config.getPath('data'), file));
|
const content = await FileUtils.readFile(path.resolve(this.bd.config.getPath('userfiles'), file));
|
||||||
return { type: 'file', name: file, saved: true, mode: this.resolveMode(file), content, savedContent: content, hoisted: file === 'user.scss' };
|
return { type: 'file', name: file, saved: true, mode: this.resolveMode(file), content, savedContent: content, hoisted: file === 'user.scss' };
|
||||||
});
|
});
|
||||||
|
|
||||||
const awaitFiles = await Promise.all(constructFiles);
|
const awaitFiles = await Promise.all(constructFiles);
|
||||||
|
|
||||||
event.reply(awaitFiles);
|
event.reply(awaitFiles);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
event.reject({ err });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
BDIpc.on('editor-getSnippets', async (event) => {
|
BDIpc.on('editor-getSnippets', async (event) => {
|
||||||
event.reply([
|
try {
|
||||||
{ type: 'snippet', name: 'test.js', content: '', savedContent: '', mode: 'javascript', saved: true }
|
const files = await FileUtils.listDirectory(this.bd.config.getPath('snippets'));
|
||||||
]);
|
|
||||||
|
const constructFiles = files.map(async file => {
|
||||||
|
const content = await FileUtils.readFile(path.resolve(this.bd.config.getPath('snippets'), file));
|
||||||
|
return { type: 'snippet', name: file, saved: true, mode: this.resolveMode(file), content, savedContent: content };
|
||||||
|
});
|
||||||
|
|
||||||
|
const awaitFiles = await Promise.all(constructFiles);
|
||||||
|
|
||||||
|
event.reply(awaitFiles);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
event.reject({ err });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
BDIpc.on('editor-saveFile', async (event, file) => {
|
BDIpc.on('editor-saveFile', async (event, file) => {
|
||||||
try {
|
try {
|
||||||
await FileUtils.writeFile(path.resolve(this.bd.config.getPath('data'), file.name), file.content);
|
await FileUtils.writeFile(path.resolve(this.bd.config.getPath('userfiles'), file.name), file.content);
|
||||||
event.reply('ok');
|
event.reply('ok');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
event.reply({ err });
|
console.log(err);
|
||||||
|
event.reject({ err });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
BDIpc.on('editor-saveSnippet', async (event, snippet) => {
|
BDIpc.on('editor-saveSnippet', async (event, snippet) => {
|
||||||
// TODO not sure how to store snippets yet
|
try {
|
||||||
console.log(snippet);
|
await FileUtils.writeFile(path.resolve(this.bd.config.getPath('snippets'), snippet.name), snippet.content);
|
||||||
event.reply('ok');
|
event.reply('ok');
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
event.reject({ err });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
BDIpc.on('editor-injectStyle', async (event, { id, style, mode }) => {
|
BDIpc.on('editor-injectStyle', async (event, { id, style, mode }) => {
|
||||||
|
@ -77,11 +97,27 @@ export default class Editor extends Module {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
style = style.split('\n').map(line => {
|
style = await Promise.all(style.split('\n').map(async(line) => {
|
||||||
if (!line.startsWith('@import')) return line;
|
if (!line.startsWith('@import')) return line;
|
||||||
const filename = line.split(' ')[1].replace(/'|"|;/g, '');
|
const filename = line.split(' ')[1].replace(/'|"|;/g, '');
|
||||||
return `@import '${path.resolve(this.bd.config.getPath('data'), filename).replace(/\\/g, '/')}';`;
|
let filePath = path.resolve(this.bd.config.getPath('userfiles'), filename).replace(/\\/g, '/');
|
||||||
}).join('\n');
|
|
||||||
|
try {
|
||||||
|
await FileUtils.fileExists(filePath);
|
||||||
|
} catch (err) {
|
||||||
|
filePath = path.resolve(this.bd.config.getPath('snippets'), filename).replace(/\\/g, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await FileUtils.fileExists(filePath);
|
||||||
|
} catch (err) {
|
||||||
|
return `/*${filename}*/`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `@import '${filePath}';`;
|
||||||
|
}));
|
||||||
|
|
||||||
|
style = style.join('\n');
|
||||||
|
|
||||||
sass.render({ data: style }, (err, result) => {
|
sass.render({ data: style }, (err, result) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
Loading…
Reference in New Issue