-
Notifications
You must be signed in to change notification settings - Fork 1k
/
update.ts
91 lines (87 loc) · 2.52 KB
/
update.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
import { updateAndroid } from '../android/update';
import c from '../colors';
import type { CheckFunction } from '../common';
import {
check,
checkPackage,
logFatal,
resolvePlatform,
runPlatformHook,
runTask,
} from '../common';
import type { Config } from '../config';
import { updateIOS, updateIOSChecks } from '../ios/update';
import { logger } from '../log';
import { allSerial } from '../util/promise';
export async function updateCommand(
config: Config,
selectedPlatformName: string,
deployment: boolean,
): Promise<void> {
if (selectedPlatformName && !config.isValidPlatform(selectedPlatformName)) {
const platformDir = resolvePlatform(config, selectedPlatformName);
if (platformDir) {
await runPlatformHook(platformDir, 'capacitor:update');
} else {
logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
}
} else {
const then = +new Date();
const platforms = config.selectPlatforms(selectedPlatformName);
if (platforms.length === 0) {
logger.info(
`There are no platforms to update yet.\n` +
`Add platforms with ${c.input('npx cap add')}.`,
);
return;
}
try {
await check(config, [checkPackage, ...updateChecks(config, platforms)]);
await allSerial(
platforms.map(platformName => async () =>
await update(config, platformName, deployment),
),
);
const now = +new Date();
const diff = (now - then) / 1000;
logger.info(`Update finished in ${diff}s`);
} catch (e) {
logFatal(e.stack ?? e);
}
}
}
export function updateChecks(
config: Config,
platforms: string[],
): CheckFunction[] {
const checks: CheckFunction[] = [];
for (const platformName of platforms) {
if (platformName === config.ios.name) {
checks.push(...updateIOSChecks);
} else if (platformName === config.android.name) {
return [];
} else if (platformName === config.web.name) {
return [];
} else {
throw `Platform ${platformName} is not valid.`;
}
}
return checks;
}
export async function update(
config: Config,
platformName: string,
deployment: boolean,
): Promise<void> {
try {
await runTask(c.success(c.strong(`update ${platformName}`)), async () => {
if (platformName === config.ios.name) {
await updateIOS(config, deployment);
} else if (platformName === config.android.name) {
await updateAndroid(config);
}
});
} catch (e) {
logger.error(`Error running update:\n` + (e.stack ?? e));
}
}