-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestUtil.ts
More file actions
166 lines (133 loc) · 4.98 KB
/
testUtil.ts
File metadata and controls
166 lines (133 loc) · 4.98 KB
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
import { exec, type ExecOptions } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import archiver from "archiver";
type ProcessOutputOptions = ExecOptions & {
noLogCommand?: boolean;
noLogStdOut?: boolean;
noLogStdErr?: boolean;
};
type PackageMetadata = {
name: string;
version: string;
};
export class TestUtil {
public static readonly ANDROID_KEY_PLACEHOLDER = "CODE_PUSH_ANDROID_DEPLOYMENT_KEY";
public static readonly IOS_KEY_PLACEHOLDER = "CODE_PUSH_IOS_DEPLOYMENT_KEY";
public static readonly SERVER_URL_PLACEHOLDER = "CODE_PUSH_SERVER_URL";
public static readonly INDEX_JS_PLACEHOLDER = "CODE_PUSH_INDEX_JS_PATH";
public static readonly CODE_PUSH_APP_VERSION_PLACEHOLDER = "CODE_PUSH_APP_VERSION";
public static readonly CODE_PUSH_TEST_APP_NAME_PLACEHOLDER = "CODE_PUSH_TEST_APP_NAME";
public static readonly CODE_PUSH_APP_ID_PLACEHOLDER = "CODE_PUSH_TEST_APPLICATION_ID";
public static readonly PLUGIN_VERSION_PLACEHOLDER = "CODE_PUSH_PLUGIN_VERSION";
public static readMochaCommandLineOption(optionName: string, defaultValue?: string): string | undefined {
let optionValue: string | undefined;
for (let index = 0; index < process.argv.length; index += 1) {
if (process.argv[index] === optionName) {
optionValue = process.argv[index + 1];
break;
}
}
return optionValue ?? defaultValue;
}
public static readMochaCommandLineFlag(optionName: string): boolean {
return process.argv.includes(optionName);
}
public static getProcessOutput(command: string, options: ProcessOutputOptions = {}): Promise<string> {
const resolvedOptions: ProcessOutputOptions = {
maxBuffer: 1024 * 1024 * 500,
timeout: 10 * 60 * 1000,
...options,
};
if (!resolvedOptions.noLogCommand) {
console.log(`Running command: ${command}`);
}
return new Promise<string>((resolve, reject) => {
const execProcess = exec(command, resolvedOptions, (error, stdout) => {
if (error) {
if (!resolvedOptions.noLogStdErr) {
console.error(String(error));
}
reject(error);
return;
}
resolve(stdout.toString());
});
if (!resolvedOptions.noLogStdOut) {
execProcess.stdout?.pipe(process.stdout);
}
if (!resolvedOptions.noLogStdErr) {
execProcess.stderr?.pipe(process.stderr);
}
execProcess.on("error", (error) => {
if (!resolvedOptions.noLogStdErr) {
console.error(String(error));
}
reject(error);
});
});
}
public static getPluginName(): string {
return TestUtil.readPackageMetadata().name;
}
public static getPluginVersion(): string {
return TestUtil.readPackageMetadata().version;
}
public static replaceString(filePath: string, regex: string, replacement: string): void {
console.log(`replacing "${regex}" with "${replacement}" in ${filePath}`);
const source = fs.readFileSync(filePath, "utf8");
const output = source.replace(new RegExp(regex, "g"), replacement);
fs.writeFileSync(filePath, output, "utf8");
}
public static async copyFile(source: string, destination: string, overwrite: boolean): Promise<void> {
if (overwrite && fs.existsSync(destination)) {
fs.unlinkSync(destination);
}
fs.mkdirSync(path.dirname(destination), { recursive: true });
await fs.promises.copyFile(source, destination);
}
public static archiveFolder(
sourceFolder: string,
targetFolder: string,
archivePath: string,
isDiff: boolean,
): Promise<string> {
console.log(`Creating an update archive at: ${archivePath}`);
if (fs.existsSync(archivePath)) {
fs.unlinkSync(archivePath);
}
return new Promise<string>((resolve, reject) => {
const archive = archiver("zip", {});
const writeStream = fs.createWriteStream(archivePath);
writeStream.on("close", () => {
resolve(archivePath);
});
archive.on("error", (error) => {
reject(error);
});
if (isDiff) {
archive.append('{"deletedFiles":[]}', { name: "hotcodepush.json" });
}
archive.directory(sourceFolder, targetFolder);
archive.pipe(writeStream);
void archive.finalize();
});
}
public static resolveBooleanVariables(variable: string | undefined): boolean {
return variable?.toLowerCase() === "true";
}
private static readPackageMetadata(): PackageMetadata {
const packageFilePath = path.join(process.cwd(), "package.json");
if (!fs.existsSync(packageFilePath)) {
throw new Error(`package.json was not found in the current working directory: ${process.cwd()}`);
}
const packageFile = JSON.parse(fs.readFileSync(packageFilePath, "utf8")) as Partial<PackageMetadata>;
if (!packageFile.name || !packageFile.version) {
throw new Error(`package.json in ${process.cwd()} must include "name" and "version".`);
}
return {
name: packageFile.name,
version: packageFile.version,
};
}
}