Skip to content

Commit

Permalink
Added Packages#uninstall (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Apr 11, 2020
1 parent ec4bbb2 commit d8e77ed
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions src/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ import logger from './logger';
* @typedef PackageMetadata
*/

/**
* @property {string} root Installation root (except on system)
* @property {boolean} [system] Install on system
* @property {object} [headers] Forward HTTP headers
* @typedef PackageInstallationOption
*/

/**
* @return {PackageInstallationOption}
*/
const createPackageInstallationOptions = options => Object.assign({}, {
root: 'home:/.packages',
system: false
}, options);

/**
* Package Manager
*
Expand Down Expand Up @@ -337,36 +352,56 @@ export default class Packages {
[...meta, ...configured].forEach(({name, args}) => this.launch(name, args || {}));
}

/**
* Uninstalls a package
* @param {string} name Package name
* @param {PackageInstallationOption} [options]
*/
uninstall(name, options = {}) {
return this._apiRequest('uninstall', {
name,
options: createPackageInstallationOptions(options)
})
.then((body) => {
if (body.reload) {
this.init();
}
});
}

/**
* Installs a package
* @param {string} url URL to package
* @param {options} [options]
* @param {boolean} [options.system] Install as system package
* @param {string} [options.root] Root installation path
* @param {PackageInstallationOption} [options]
*/
install(url, options = {}) {
const body = {
return this._apiRequest('install', {
url,
options: Object.assign({}, {
root: 'home:/.packages',
system: false
}, options)
};
options: createPackageInstallationOptions(options)
})
.then((body) => {
if (body.reload) {
this.init();
}
});
}

/**
* Creates a new API request
* @param {string} endpoint
* @param {object} body
* @return {object} JSON
*/
_apiRequest(endpoint, body) {
return this.core
.request('/api/packages/install', {
.request(`/api/packages/${endpoint}`, {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(body)
})
.then(response => response.json())
.then((body) => {
if (body.reload) {
this.init();
}
});
.then(response => response.json());
}

/**
Expand Down

0 comments on commit d8e77ed

Please sign in to comment.