/
PackageFilterer.ts
48 lines (40 loc) · 1.91 KB
/
PackageFilterer.ts
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { PlatformInformation } from "../platform";
import * as util from '../common';
import { PackageError } from "./PackageError";
import { AbsolutePathPackage } from "./AbsolutePathPackage";
const { filterAsync } = require('node-filter-async');
export async function filterPackages(packages: AbsolutePathPackage[], platformInfo: PlatformInformation): Promise<AbsolutePathPackage[]> {
let platformPackages = filterPlatformPackages(packages, platformInfo);
return filterAlreadyInstalledPackages(platformPackages);
}
function filterPlatformPackages(packages: AbsolutePathPackage[], platformInfo: PlatformInformation) {
if (packages) {
return packages.filter(pkg => {
if (pkg.architectures && pkg.architectures.indexOf(platformInfo.architecture) === -1) {
return false;
}
if (pkg.platforms && pkg.platforms.indexOf(platformInfo.platform) === -1) {
return false;
}
return true;
});
}
else {
throw new PackageError("Package manifest does not exist.");
}
}
async function filterAlreadyInstalledPackages(packages: AbsolutePathPackage[]): Promise<AbsolutePathPackage[]> {
return filterAsync(packages, async (pkg: AbsolutePathPackage) => {
//If the file is present at the install test path then filter it
let testPath = pkg.installTestPath;
if (!testPath) {
//if there is no testPath specified then we will not filter it
return true;
}
return !(await util.fileExists(testPath.value));
});
}