-
Notifications
You must be signed in to change notification settings - Fork 264
/
cordova-build-utils.ts
331 lines (319 loc) · 8.56 KB
/
cordova-build-utils.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import { spawnSync } from "child_process";
import {
existsSync,
mkdirSync,
copySync,
rmSync,
readFileSync,
writeFileSync,
} from "fs-extra";
import { join } from "path";
import { readdirSync } from "fs";
import File from "@saltcorn/data/models/file";
import type User from "@saltcorn/data/models/user";
import { parseStringPromise, Builder } from "xml2js";
import { removeNonWordChars } from "@saltcorn/data/utils";
/**
* copy saltcorn-mobile-app as a template to buildDir
* @param buildDir directory where the app will be build
* @param templateDir directory of the template code that will be copied to 'buildDir'
*/
export function prepareBuildDir(buildDir: string, templateDir: string) {
if (existsSync(buildDir)) rmSync(buildDir, { force: true, recursive: true });
copySync(templateDir, buildDir);
rmSync(`${buildDir}/node_modules`, { recursive: true, force: true });
const result = spawnSync("npm", ["install", "--legacy-peer-deps"], {
cwd: buildDir,
});
console.log(result.output.toString());
}
/**
* parse the config.xml file and replace the id and name parameters
* on error the defaults will be used
* @param buildDir directory where the app will be build
* @param appName
*/
export async function setAppName(buildDir: string, appName: string) {
try {
const configXml = join(buildDir, "config.xml");
const content = readFileSync(configXml);
const parsed = await parseStringPromise(content);
parsed.widget.$.id = `${removeNonWordChars(appName)}.mobile.app`;
parsed.widget.name[0] = appName;
const xmlBuilder = new Builder();
const newCfg = xmlBuilder.buildObject(parsed);
writeFileSync(configXml, newCfg);
} catch (error: any) {
console.log(
`Unable to set the appName to '${appName}': ${
error.message ? error.message : "Unknown error"
}`
);
}
}
/**
* parse the config.xml file and replace the version parameter
* on error the defaults will be used
* @param buildDir directory where the app will be build
* @param appVersion
*/
export async function setAppVersion(buildDir: string, appVersion: string) {
try {
const configXml = join(buildDir, "config.xml");
const content = readFileSync(configXml);
const parsed = await parseStringPromise(content);
parsed.widget.$.version = appVersion;
const xmlBuilder = new Builder();
const newCfg = xmlBuilder.buildObject(parsed);
writeFileSync(configXml, newCfg);
} catch (error: any) {
console.log(
`Unable to set the appVersion to '${appVersion}': ${
error.message ? error.message : "Unknown error"
}`
);
}
}
/**
* copy a png file into the build dir and use it as launcher icon
* @param buildDir
* @param appIcon path to appIcon file
*/
export async function prepareAppIcon(buildDir: string, appIcon: string) {
try {
copySync(appIcon, join(buildDir, "res", "icon", "android", "icon.png"), {
overwrite: true,
});
} catch (error: any) {
console.log(
`Unable to set the app icon '${appIcon}': ${
error.message ? error.message : "Unknown error"
}`
);
}
}
/**
*
* @param buildDir directory where the app will be build
* @returns
*/
export function buildApkInContainer(buildDir: string) {
const result = spawnSync(
"docker",
[
"run",
"--network",
"host",
"-v",
`${buildDir}:/saltcorn-mobile-app`,
"saltcorn/cordova-builder",
],
{ cwd: "." }
);
if (result.output) console.log(result.output.toString());
else if (result.error) console.log(result.error.toString());
else console.log("docker finished without output");
return result.status;
}
/**
* build '.apk / .ipa' files with cordova (only android is tested)
* @param flags
* @returns
*/
export function buildApp(
buildDir: string,
platforms: string[],
useDocker?: boolean,
buildForEmulator?: boolean,
appleTeamId?: string
) {
if (!useDocker) {
addPlugins(buildDir);
return callBuild(buildDir, platforms, buildForEmulator, appleTeamId);
} else {
let code = buildApkInContainer(buildDir);
if (code === 0 && platforms.indexOf("ios") > -1)
code = callBuild(buildDir, ["ios"], buildForEmulator, appleTeamId);
return code;
}
}
/**
* call cordova plugin add ...
* it loads the dependencies from npm, docker usese cached folders
* @param buildDir
*/
function addPlugins(buildDir: string) {
let result = spawnSync(
"npm",
["run", "add-plugin", "--", "cordova-sqlite-ext"],
{
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
}
);
console.log(result.output.toString());
result = spawnSync(
"npm",
["run", "add-plugin", "--", "cordova-plugin-file@7.0.0"],
{
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
}
);
console.log(result.output.toString());
result = spawnSync(
"npm",
["run", "add-plugin", "--", "cordova-plugin-inappbrowser"],
{
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
}
);
console.log(result.output.toString());
result = spawnSync(
"npm",
["run", "add-plugin", "--", "cordova-plugin-network-information"],
{
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
}
);
console.log(result.output.toString());
result = spawnSync(
"npm",
["run", "add-plugin", "--", "cordova-plugin-geolocation"],
{
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
}
);
console.log(result.output.toString());
result = spawnSync(
"npm",
["run", "add-plugin", "--", "cordova-plugin-camera"],
{
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
}
);
console.log(result.output.toString());
}
/**
*
* @param buildDir directory where the app will be build
* @param platforms
*/
export function addPlatforms(buildDir: string, platforms: string[]) {
const result = spawnSync("npm", ["run", "add-platform", "--", ...platforms], {
cwd: buildDir,
env: {
...process.env,
NODE_ENV: "development",
},
});
console.log(result.output.toString());
}
/**
*
* @param buildDir directory where the app will be build
* @param platforms
* @returns
*/
export function callBuild(
buildDir: string,
platforms: string[],
buildForEmulator?: boolean,
appleTeamId?: string
) {
addPlatforms(buildDir, platforms);
let buildParams = [...platforms];
if (!buildForEmulator) {
buildParams.push(
"--device",
`--developmentTeam=${appleTeamId}`,
"--codeSignIdentity=iPhone Developer",
"--automaticProvisioning=true"
);
}
const result = spawnSync("npm", ["run", "build-app", "--", ...buildParams], {
cwd: buildDir,
});
console.log(result.output.toString());
return result.status;
}
/**
* find first file with specific ending
* @param directory directory to search
* @param ending wantet ending
*/
function fileWithEnding(directory: string, ending: string): string | null {
if (!existsSync(directory)) return null;
for (const file of readdirSync(directory)) {
if (file.endsWith(ending)) return file;
}
return null;
}
function safeEnding(file: string, ending: string): string {
if (!file.endsWith(ending)) return `${file}${ending}`;
return file;
}
/**
* copy .apk / .ipa files to 'copyDir' if they exist
* @param buildDir directory where the app was build
* @param copyDir directory where the resulting app file will be copied to
* @param user the user specified by the userEmail (-c) parameter
* @param appName
*/
export async function tryCopyAppFiles(
buildDir: string,
copyDir: string,
user: User,
appName?: string
) {
if (!existsSync(copyDir)) {
mkdirSync(copyDir);
}
// android .apk file
const apkBuildDir = join(
buildDir,
"platforms",
"android",
"app",
"build",
"outputs",
"apk",
"debug"
);
const apkFile = fileWithEnding(apkBuildDir, ".apk");
if (apkFile) {
const dstFile = appName ? safeEnding(appName, ".apk") : "app-debug.apk";
copySync(join(apkBuildDir, apkFile), join(copyDir, dstFile));
await File.set_xattr_of_existing_file(dstFile, copyDir, user);
}
// iOS .ipa file
const ipaBuildDir = join(buildDir, "platforms", "ios", "build", "device");
const ipaFile = fileWithEnding(ipaBuildDir, ".ipa");
if (ipaFile) {
const dstFile = appName ? safeEnding(appName, ".ipa") : "app-debug.ipa";
copySync(join(ipaBuildDir, ipaFile), join(copyDir, dstFile));
await File.set_xattr_of_existing_file(dstFile, copyDir, user);
}
}