diff --git a/src/packages.js b/src/packages.js index 28292d0..bc96875 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 7652e4a..d418082 100644 --- a/src/providers/packages.js +++ b/src/providers/packages.js @@ -86,6 +86,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(); }