From 2fed1fbf21c1f7e671133b9f178357bb7f8178c8 Mon Sep 17 00:00:00 2001 From: Anders Evenrud Date: Sat, 11 Apr 2020 00:31:01 +0200 Subject: [PATCH] Added user package uninstallation (#28) --- src/packages.js | 50 ++++++++++++++++++++++++++++++++++----- src/providers/packages.js | 9 +++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/packages.js b/src/packages.js index ac5ead1..eb8e353 100644 --- a/src/packages.js +++ b/src/packages.js @@ -167,8 +167,6 @@ class Packages { const userRoot = options.root; const target = await realpath(`${userRoot}/${name}`, user); - const root = await realpath(userRoot, user); - const manifest = await realpath(`${userRoot}/metadata.json`, user); if (await fs.exists(target)) { throw new Error('Target already exists'); @@ -192,18 +190,58 @@ class Packages { throw new Error('Invalid package'); } - // TODO: Check conflicts ? + await this.writeUserManifest(userRoot, user); - const filenames = await fg(root + '/*/metadata.json'); // FIXME: Windows! - const metadatas = await Promise.all(filenames.map(f => fs.readJson(f))); + return { + reload: !options.system + }; + } - await fs.writeJson(manifest, metadatas); + /** + * Uninstalls a package by name + * @param {string} name + * @param {InstallPackageOptions} options + * @param {object} user + */ + async uninstallPackage(name, options, user) { + const {realpath} = this.core.make('osjs/vfs'); + + if (!options.root) { + throw new Error('Missing package installation root path'); + } + + const userRoot = options.root; + const target = await realpath(`${userRoot}/${name}`, user); + + if (await fs.exists(target)) { + await fs.remove(target); + await this.writeUserManifest(userRoot, user); + } else { + throw new Error('Package not found in root directory'); + } return { reload: !options.system }; } + /** + * Writes user installed package manifest + * @param {string} userRoot + * @param {object} user + */ + async writeUserManifest(userRoot, user) { + const {realpath} = this.core.make('osjs/vfs'); + + // TODO: Check conflicts ? + const root = await realpath(userRoot, user); + const manifest = await realpath(`${userRoot}/metadata.json`, user); + const filenames = await fg(root + '/*/metadata.json'); // FIXME: Windows! + const metadatas = await Promise.all(filenames.map(f => fs.readJson(f))); + + await fs.writeJson(manifest, metadatas); + } + /** * Reads package manifests * @param {string[]} paths diff --git a/src/providers/packages.js b/src/providers/packages.js index 6845bec..2bf13a0 100644 --- a/src/providers/packages.js +++ b/src/providers/packages.js @@ -87,6 +87,15 @@ class PackageServiceProvider extends ServiceProvider { }); }); + routeAuthenticated('POST', '/api/packages/uninstall', (req, res) => { + this.packages.uninstallPackage(req.body.name, req.body.options, req.session.user) + .then(body => res.json(body)) + .catch((error) => { + console.error(error); + res.status(400).json({error: 'Package uninstallation failed'}); + }); + }); + return this.packages.init(); }