Skip to content

Commit

Permalink
Added user package uninstallation (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Apr 11, 2020
1 parent 24b3547 commit 9d53a34
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/providers/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 9d53a34

Please sign in to comment.