-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
54 lines (48 loc) · 1.41 KB
/
update.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow
import {debug as debugModule} from "debug";
import list from "./list";
import install from "./install";
import * as config from "../config";
import {getRepositoryPackage} from "../utils/GitHubUtils";
// For debug purpose only:
const log: Function = debugModule("qilin:update");
/**
* Fetches new versions directly from GitHub for a package list.
*
* @param {Object} packages
* @return {Promise<Array<Object>>}
* @async
*/
export function getNewVersions(packages: Object): Promise<Array<Object>> {
return Promise.all(
Object.keys(packages).map((id) => (
getRepositoryPackage(id, config)
))
);
}
/**
* Asynchronously checks if locally installed packages under a certain namespace
* are up-to-date. If no, they are downloaded again.
*
* @example
* qpm.update("plugins").then(…);
* qpm.update("themes").then(…);
* qpm.update().then(…);
*
* @param {?string} namespace
* @return {Promise<*>}
* @async
*/
export default async function(namespace?: string): Promise<*> {
const download = [];
const packages = await list(namespace);
const external = await getNewVersions(packages);
external.forEach((e) => {
if (e.version !== packages[e.repository].version) {
// eslint-disable-next-line
log(`Updating ${e.repository}: ${packages[e.repository].version} => ${e.version}`);
download.push(install(e.repository));
}
});
return Promise.all(download);
}