@@ -55,6 +55,21 @@ import logger from './logger';
55
55
* @typedef PackageMetadata
56
56
*/
57
57
58
+ /**
59
+ * @property {string } root Installation root (except on system)
60
+ * @property {boolean } [system] Install on system
61
+ * @property {object } [headers] Forward HTTP headers
62
+ * @typedef PackageInstallationOption
63
+ */
64
+
65
+ /**
66
+ * @return {PackageInstallationOption }
67
+ */
68
+ const createPackageInstallationOptions = options => Object . assign ( { } , {
69
+ root : 'home:/.packages' ,
70
+ system : false
71
+ } , options ) ;
72
+
58
73
/**
59
74
* Package Manager
60
75
*
@@ -337,36 +352,56 @@ export default class Packages {
337
352
[ ...meta , ...configured ] . forEach ( ( { name, args} ) => this . launch ( name , args || { } ) ) ;
338
353
}
339
354
355
+ /**
356
+ * Uninstalls a package
357
+ * @param {string } name Package name
358
+ * @param {PackageInstallationOption } [options]
359
+ */
360
+ uninstall ( name , options = { } ) {
361
+ return this . _apiRequest ( 'uninstall' , {
362
+ name,
363
+ options : createPackageInstallationOptions ( options )
364
+ } )
365
+ . then ( ( body ) => {
366
+ if ( body . reload ) {
367
+ this . init ( ) ;
368
+ }
369
+ } ) ;
370
+ }
371
+
340
372
/**
341
373
* Installs a package
342
374
* @param {string } url URL to package
343
- * @param {options } [options]
344
- * @param {boolean } [options.system] Install as system package
345
- * @param {string } [options.root] Root installation path
375
+ * @param {PackageInstallationOption } [options]
346
376
*/
347
377
install ( url , options = { } ) {
348
- const body = {
378
+ return this . _apiRequest ( 'install' , {
349
379
url,
350
- options : Object . assign ( { } , {
351
- root : 'home:/.packages' ,
352
- system : false
353
- } , options )
354
- } ;
380
+ options : createPackageInstallationOptions ( options )
381
+ } )
382
+ . then ( ( body ) => {
383
+ if ( body . reload ) {
384
+ this . init ( ) ;
385
+ }
386
+ } ) ;
387
+ }
355
388
389
+ /**
390
+ * Creates a new API request
391
+ * @param {string } endpoint
392
+ * @param {object } body
393
+ * @return {object } JSON
394
+ */
395
+ _apiRequest ( endpoint , body ) {
356
396
return this . core
357
- . request ( ' /api/packages/install' , {
397
+ . request ( ` /api/packages/${ endpoint } ` , {
358
398
method : 'post' ,
359
399
headers : {
360
400
'content-type' : 'application/json'
361
401
} ,
362
402
body : JSON . stringify ( body )
363
403
} )
364
- . then ( response => response . json ( ) )
365
- . then ( ( body ) => {
366
- if ( body . reload ) {
367
- this . init ( ) ;
368
- }
369
- } ) ;
404
+ . then ( response => response . json ( ) ) ;
370
405
}
371
406
372
407
/**
0 commit comments