From e78021a5385c05fa018cc60b056db7c9c6a39c27 Mon Sep 17 00:00:00 2001 From: Maks-s <26678512+Maks-s@users.noreply.github.com> Date: Tue, 21 Aug 2018 02:45:54 +0200 Subject: [PATCH 1/3] Added delete method to plugins / themes --- client/src/modules/contentmanager.js | 30 ++++++++++++++++++++++++++++ client/src/modules/pluginmanager.js | 1 + client/src/modules/thememanager.js | 1 + common/modules/utils.js | 29 +++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) diff --git a/client/src/modules/contentmanager.js b/client/src/modules/contentmanager.js index 3a8aa4c6..2022fde9 100644 --- a/client/src/modules/contentmanager.js +++ b/client/src/modules/contentmanager.js @@ -256,6 +256,36 @@ export default class { } } + /** + * Delete content. + * @param {Content|String} content Content to delete + * @param {Boolean} force If true the content will be deleted even if an exception is thrown when disabling/unloading/deleting + */ + static async deleteContent(content, force) { + content = this.findContent(content); + if (!content) throw {message: `Could not find a ${this.contentType} from ${content}.`}; + + try { + await Modals.confirm(`Delete ${this.contentType} ?`, `Are you sure you want to delete ${content.info.name} ?`, 'Delete').promise; + } catch (err) { + return false; + } + + try { + const unload = this.unloadContent(content, force, false); + + if (!force) + await unload; + + await FileUtils.directoryExists(content.paths.contentPath); + FileUtils.deleteDirectory(content.paths.contentPath); + return true; + } catch (err) { + Logger.err(this.moduleName, err); + throw err; + } + } + /** * Unload content. * @param {Content|String} content Content to unload diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index c18c6aaa..bcc93e32 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -124,6 +124,7 @@ export default class extends ContentManager { return instance; } + static get deletePlugin() { return this.deleteContent } static get unloadPlugin() { return this.unloadContent } static get reloadPlugin() { return this.reloadContent } diff --git a/client/src/modules/thememanager.js b/client/src/modules/thememanager.js index 1a1b5811..3ed2b3f7 100644 --- a/client/src/modules/thememanager.js +++ b/client/src/modules/thememanager.js @@ -53,6 +53,7 @@ export default class ThemeManager extends ContentManager { } } + static get deleteTheme() { return this.deleteContent } static get unloadTheme() { return this.unloadContent } static async reloadTheme(theme) { theme = await this.reloadContent(theme); diff --git a/common/modules/utils.js b/common/modules/utils.js index fca6d957..a28b49f2 100644 --- a/common/modules/utils.js +++ b/common/modules/utils.js @@ -11,6 +11,7 @@ import fs from 'fs'; import _ from 'lodash'; import filetype from 'file-type'; +import { join as pathJoin } from 'path'; export class Utils { static overload(fn, cb) { @@ -493,4 +494,32 @@ export class FileUtils { if (!type) type = (await this.getFileType(buffer)).mime; return `data:${type};base64,${buffer.toString('base64')}`; } + + /** + * Delete a directory + * @param {String} path The directory's path + * @return {Promise} + */ + static async deleteDirectory(path) { + try { + await this.directoryExists(path); + const files = await this.listDirectory(path); + + for (const file of files) { + const pathToFile = pathJoin(path, file); + try { + await this.directoryExists(pathToFile); + await this.deleteDirectory(pathToFile); + } catch (err) { + fs.unlinkSync(pathToFile); + } + } + + fs.rmdirSync(path); + + return true; + } catch (err) { + throw err; + } + } } From 7c44b92bce227ed6d3813240673ac564252e2aa4 Mon Sep 17 00:00:00 2001 From: Maks <26678512+Maks-s@users.noreply.github.com> Date: Tue, 21 Aug 2018 09:57:09 +0200 Subject: [PATCH 2/3] Changed path import --- common/modules/utils.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/common/modules/utils.js b/common/modules/utils.js index a28b49f2..fc714398 100644 --- a/common/modules/utils.js +++ b/common/modules/utils.js @@ -11,7 +11,7 @@ import fs from 'fs'; import _ from 'lodash'; import filetype from 'file-type'; -import { join as pathJoin } from 'path'; +import join from 'path'; export class Utils { static overload(fn, cb) { @@ -500,13 +500,13 @@ export class FileUtils { * @param {String} path The directory's path * @return {Promise} */ - static async deleteDirectory(path) { + static async deleteDirectory(pathToDir) { try { - await this.directoryExists(path); - const files = await this.listDirectory(path); + await this.directoryExists(pathToDir); + const files = await this.listDirectory(pathToDir); for (const file of files) { - const pathToFile = pathJoin(path, file); + const pathToFile = path.join(pathToDir, file); try { await this.directoryExists(pathToFile); await this.deleteDirectory(pathToFile); From 10f45b6b4ce797fb094a14418a1b0e72a82a74c8 Mon Sep 17 00:00:00 2001 From: Maks <26678512+Maks-s@users.noreply.github.com> Date: Tue, 21 Aug 2018 10:09:38 +0200 Subject: [PATCH 3/3] Fixed stupid errors --- common/modules/utils.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/common/modules/utils.js b/common/modules/utils.js index fc714398..31573581 100644 --- a/common/modules/utils.js +++ b/common/modules/utils.js @@ -11,7 +11,7 @@ import fs from 'fs'; import _ from 'lodash'; import filetype from 'file-type'; -import join from 'path'; +import path from 'path'; export class Utils { static overload(fn, cb) { @@ -504,7 +504,7 @@ export class FileUtils { try { await this.directoryExists(pathToDir); const files = await this.listDirectory(pathToDir); - + for (const file of files) { const pathToFile = path.join(pathToDir, file); try { @@ -515,7 +515,7 @@ export class FileUtils { } } - fs.rmdirSync(path); + fs.rmdirSync(pathToDir); return true; } catch (err) {