๐Ÿ“ฆ ionic-team / capacitor

๐Ÿ“„ update.ts ยท 78 lines
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
78import { updateAndroid } from '../android/update';
import c from '../colors';
import {
  check,
  checkPackage,
  resolvePlatform,
  runHooks,
  runPlatformHook,
  runTask,
  selectPlatforms,
  isValidPlatform,
} from '../common';
import type { CheckFunction } from '../common';
import type { Config } from '../definitions';
import { fatal, isFatal } from '../errors';
import { checkBundler, checkCocoaPods } from '../ios/common';
import { updateIOS } 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 && !(await isValidPlatform(selectedPlatformName))) {
    const platformDir = resolvePlatform(config, selectedPlatformName);
    if (platformDir) {
      await runPlatformHook(config, selectedPlatformName, platformDir, 'capacitor:update');
    } else {
      logger.error(`Platform ${c.input(selectedPlatformName)} not found.`);
    }
  } else {
    const then = +new Date();
    const platforms = await selectPlatforms(config, selectedPlatformName);
    try {
      await check([() => 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: any) {
      if (!isFatal(e)) {
        fatal(e.stack ?? e);
      }

      throw e;
    }
  }
}

export function updateChecks(config: Config, platforms: string[]): CheckFunction[] {
  const checks: CheckFunction[] = [];
  for (const platformName of platforms) {
    if (platformName === config.ios.name) {
      checks.push(() => checkBundler(config) || checkCocoaPods(config));
    } else if (platformName === config.android.name) {
      continue;
    } else if (platformName === config.web.name) {
      continue;
    } else {
      throw `Platform ${platformName} is not valid.`;
    }
  }
  return checks;
}

export async function update(config: Config, platformName: string, deployment: boolean): Promise<void> {
  await runTask(c.success(c.strong(`update ${platformName}`)), async () => {
    await runHooks(config, platformName, config.app.rootDir, 'capacitor:update:before');

    if (platformName === config.ios.name) {
      await updateIOS(config, deployment);
    } else if (platformName === config.android.name) {
      await updateAndroid(config);
    }

    await runHooks(config, platformName, config.app.rootDir, 'capacitor:update:after');
  });
}