Skip to content

Commit d8e77ed

Browse files
committed
Added Packages#uninstall (#106)
1 parent ec4bbb2 commit d8e77ed

File tree

1 file changed

+51
-16
lines changed

1 file changed

+51
-16
lines changed

src/packages.js

+51-16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ import logger from './logger';
5555
* @typedef PackageMetadata
5656
*/
5757

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+
5873
/**
5974
* Package Manager
6075
*
@@ -337,36 +352,56 @@ export default class Packages {
337352
[...meta, ...configured].forEach(({name, args}) => this.launch(name, args || {}));
338353
}
339354

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+
340372
/**
341373
* Installs a package
342374
* @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]
346376
*/
347377
install(url, options = {}) {
348-
const body = {
378+
return this._apiRequest('install', {
349379
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+
}
355388

389+
/**
390+
* Creates a new API request
391+
* @param {string} endpoint
392+
* @param {object} body
393+
* @return {object} JSON
394+
*/
395+
_apiRequest(endpoint, body) {
356396
return this.core
357-
.request('/api/packages/install', {
397+
.request(`/api/packages/${endpoint}`, {
358398
method: 'post',
359399
headers: {
360400
'content-type': 'application/json'
361401
},
362402
body: JSON.stringify(body)
363403
})
364-
.then(response => response.json())
365-
.then((body) => {
366-
if (body.reload) {
367-
this.init();
368-
}
369-
});
404+
.then(response => response.json());
370405
}
371406

372407
/**

0 commit comments

Comments
 (0)