Skip to content

Commit 935df5b

Browse files
committed
Added user package uninstallation (#28)
1 parent dd06b04 commit 935df5b

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/packages.js

+44-6
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ class Packages {
167167

168168
const userRoot = options.root;
169169
const target = await realpath(`${userRoot}/${name}`, user);
170-
const root = await realpath(userRoot, user);
171-
const manifest = await realpath(`${userRoot}/metadata.json`, user);
172170

173171
if (await fs.exists(target)) {
174172
throw new Error('Target already exists');
@@ -192,18 +190,58 @@ class Packages {
192190
throw new Error('Invalid package');
193191
}
194192

195-
// TODO: Check conflicts ?
193+
await this.writeUserManifest(userRoot, user);
196194

197-
const filenames = await fg(root + '/*/metadata.json'); // FIXME: Windows!
198-
const metadatas = await Promise.all(filenames.map(f => fs.readJson(f)));
195+
return {
196+
reload: !options.system
197+
};
198+
}
199199

200-
await fs.writeJson(manifest, metadatas);
200+
/**
201+
* Uninstalls a package by name
202+
* @param {string} name
203+
* @param {InstallPackageOptions} options
204+
* @param {object} user
205+
*/
206+
async uninstallPackage(name, options, user) {
207+
const {realpath} = this.core.make('osjs/vfs');
208+
209+
if (!options.root) {
210+
throw new Error('Missing package installation root path');
211+
}
212+
213+
const userRoot = options.root;
214+
const target = await realpath(`${userRoot}/${name}`, user);
215+
216+
if (await fs.exists(target)) {
217+
await fs.remove(target);
218+
await this.writeUserManifest(userRoot, user);
219+
} else {
220+
throw new Error('Package not found in root directory');
221+
}
201222

202223
return {
203224
reload: !options.system
204225
};
205226
}
206227

228+
/**
229+
* Writes user installed package manifest
230+
* @param {string} userRoot
231+
* @param {object} user
232+
*/
233+
async writeUserManifest(userRoot, user) {
234+
const {realpath} = this.core.make('osjs/vfs');
235+
236+
// TODO: Check conflicts ?
237+
const root = await realpath(userRoot, user);
238+
const manifest = await realpath(`${userRoot}/metadata.json`, user);
239+
const filenames = await fg(root + '/*/metadata.json'); // FIXME: Windows!
240+
const metadatas = await Promise.all(filenames.map(f => fs.readJson(f)));
241+
242+
await fs.writeJson(manifest, metadatas);
243+
}
244+
207245
/**
208246
* Reads package manifests
209247
* @param {string[]} paths

src/providers/packages.js

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ class PackageServiceProvider extends ServiceProvider {
8787
});
8888
});
8989

90+
routeAuthenticated('POST', '/api/packages/uninstall', (req, res) => {
91+
this.packages.uninstallPackage(req.body.name, req.body.options, req.session.user)
92+
.then(body => res.json(body))
93+
.catch((error) => {
94+
console.error(error);
95+
res.status(400).json({error: 'Package uninstallation failed'});
96+
});
97+
});
98+
9099
return this.packages.init();
91100
}
92101

0 commit comments

Comments
 (0)