-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathbuild-artefacts-service.ts
159 lines (140 loc) · 4.44 KB
/
build-artefacts-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
import * as path from "path";
import { IBuildArtefactsService } 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 BuildArtefactsService implements IBuildArtefactsService {
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 'tns 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 copyLatestAppPackage(
targetPath: string,
platformData: IPlatformData,
buildOutputOptions: IBuildOutputOptions
): void {
targetPath = path.resolve(targetPath);
const outputPath =
buildOutputOptions.outputPath ||
platformData.getBuildOutputPath(buildOutputOptions);
const applicationPackage = this.getLatestApplicationPackage(
outputPath,
platformData.getValidBuildOutputData(buildOutputOptions)
);
const packageFile = applicationPackage.packageName;
this.$fs.ensureDirectoryExists(path.dirname(targetPath));
if (
this.$fs.exists(targetPath) &&
this.$fs.getFsStats(targetPath).isDirectory()
) {
const sourceFileName = path.basename(packageFile);
this.$logger.trace(
`Specified target path: '${targetPath}' is directory. Same filename will be used: '${sourceFileName}'.`
);
targetPath = path.join(targetPath, sourceFileName);
}
this.$fs.copyFile(packageFile, targetPath);
this.$logger.info(`Copied file '${packageFile}' to '${targetPath}'.`);
}
private getLatestApplicationPackage(
buildOutputPath: string,
validBuildOutputData: IValidBuildOutputData
): 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("buildArtefactsService", BuildArtefactsService);