-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathbuild-artifacts-service.ts
171 lines (152 loc) · 4.78 KB
/
build-artifacts-service.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import * as path from "path";
import { IBuildArtifactsService } from "../definitions/build";
import {
IPlatformData,
IBuildOutputOptions,
IValidBuildOutputData,
} from "../definitions/platform";
import { IApplicationPackage } from "../declarations";
import { IErrors, IFileSystem } from "../common/declarations";
import { injector } from "../common/yok";
import * as _ from "lodash";
export class BuildArtifactsService implements IBuildArtifactsService {
constructor(
private $errors: IErrors,
private $fs: IFileSystem,
private $logger: ILogger
) {}
public async getLatestAppPackagePath(
platformData: IPlatformData,
buildOutputOptions: IBuildOutputOptions
): Promise<string> {
const outputPath =
buildOutputOptions.outputPath ||
platformData.getBuildOutputPath(buildOutputOptions);
const applicationPackage = this.getLatestApplicationPackage(
outputPath,
platformData.getValidBuildOutputData(buildOutputOptions)
);
const packageFile = applicationPackage.packageName;
if (!packageFile || !this.$fs.exists(packageFile)) {
this.$errors.fail(
`Unable to find built application. Try 'ns build ${platformData.platformNameLowerCase}'.`
);
}
return packageFile;
}
public getAllAppPackages(
buildOutputPath: string,
validBuildOutputData: IValidBuildOutputData
): IApplicationPackage[] {
const rootFiles = this.$fs
.readDirectory(buildOutputPath)
.map((filename) => path.join(buildOutputPath, filename));
let result = this.getApplicationPackagesCore(
rootFiles,
validBuildOutputData.packageNames
);
if (result) {
return result;
}
const candidates = this.$fs.enumerateFilesInDirectorySync(buildOutputPath);
result = this.getApplicationPackagesCore(
candidates,
validBuildOutputData.packageNames
);
if (result) {
return result;
}
if (validBuildOutputData.regexes && validBuildOutputData.regexes.length) {
const packages = candidates.filter((filepath) =>
_.some(validBuildOutputData.regexes, (regex) =>
regex.test(path.basename(filepath))
)
);
return this.createApplicationPackages(packages);
}
return [];
}
public copyAppPackages(
targetPath: string,
platformData: IPlatformData,
buildOutputOptions: IBuildOutputOptions
): void {
targetPath = path.resolve(targetPath);
const outputPath =
buildOutputOptions.outputPath ||
platformData.getBuildOutputPath(buildOutputOptions);
const applicationPackages = this.getAllAppPackages(
outputPath,
platformData.getValidBuildOutputData(buildOutputOptions)
);
this.$fs.ensureDirectoryExists(path.dirname(targetPath));
let filterRegex: RegExp;
let targetIsDirectory = false;
if (
this.$fs.exists(targetPath) &&
this.$fs.getFsStats(targetPath).isDirectory()
) {
targetIsDirectory = true;
} else if (targetPath.match(/\.(ipa|aab|apk)/)){
if (applicationPackages.length > 1){
filterRegex = new RegExp('universal');
this.$logger.trace(
`Multiple packages were built but only the universal one will be copied if existing'.`
);
}
} else {
targetIsDirectory = true;
}
applicationPackages.forEach(pack => {
const targetFilePath = targetIsDirectory ? path.join(targetPath, path.basename(pack.packageName)) : targetPath;
if (!filterRegex || filterRegex.test(pack.packageName)) {
this.$fs.copyFile(pack.packageName, targetFilePath);
this.$logger.info(`Copied file '${pack.packageName}' to '${targetFilePath}'.`);
}
});
}
private getLatestApplicationPackage(
buildOutputPath: string,
validBuildOutputData: IValidBuildOutputData,
abis?: string[]
): IApplicationPackage {
let packages = this.getAllAppPackages(
buildOutputPath,
validBuildOutputData
);
const packageExtName = path.extname(validBuildOutputData.packageNames[0]);
if (packages.length === 0) {
this.$errors.fail(
`No ${packageExtName} found in ${buildOutputPath} directory.`
);
}
if (packages.length > 1) {
this.$logger.warn(
`More than one ${packageExtName} found in ${buildOutputPath} directory. Using the last one produced from build.`
);
}
packages = _.sortBy(packages, (pkg) => pkg.time).reverse(); // We need to reverse because sortBy always sorts in ascending order
return packages[0];
}
private getApplicationPackagesCore(
candidates: string[],
validPackageNames: string[]
): IApplicationPackage[] {
const packages = candidates.filter((filePath) =>
_.includes(validPackageNames, path.basename(filePath))
);
if (packages.length > 0) {
return this.createApplicationPackages(packages);
}
return null;
}
private createApplicationPackages(packages: string[]): IApplicationPackage[] {
return packages.map((packageName) => {
return {
packageName,
time: this.$fs.getFsStats(packageName).mtime,
};
});
}
}
injector.register("buildArtifactsService", BuildArtifactsService);