๐Ÿ“ฆ ionic-team / capacitor

๐Ÿ“„ doctor.ts ยท 77 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
77import { readJSON } from 'fs-extra';

import { doctorAndroid } from '../android/doctor';
import c from '../colors';
import { selectPlatforms } from '../common';
import type { Config } from '../definitions';
import { doctorIOS } from '../ios/doctor';
import { output } from '../log';
import { emoji as _e } from '../util/emoji';
import { resolveNode } from '../util/node';
import { getCommandOutput } from '../util/subprocess';

export async function doctorCommand(config: Config, selectedPlatformName: string): Promise<void> {
  output.write(`${_e('๐Ÿ’Š', '')}   ${c.strong('Capacitor Doctor')}  ${_e('๐Ÿ’Š', '')} \n\n`);

  await doctorCore(config);

  const platforms = await selectPlatforms(config, selectedPlatformName);
  await Promise.all(
    platforms.map((platformName) => {
      return doctor(config, platformName);
    }),
  );
}

export async function doctorCore(config: Config): Promise<void> {
  const [cliVersion, coreVersion, androidVersion, iosVersion] = await Promise.all([
    getCommandOutput('npm', ['info', '@capacitor/cli', 'version']),
    getCommandOutput('npm', ['info', '@capacitor/core', 'version']),
    getCommandOutput('npm', ['info', '@capacitor/android', 'version']),
    getCommandOutput('npm', ['info', '@capacitor/ios', 'version']),
  ]);

  output.write(
    `${c.strong('Latest Dependencies:')}\n\n` +
      `  @capacitor/cli: ${c.weak(cliVersion ?? 'unknown')}\n` +
      `  @capacitor/core: ${c.weak(coreVersion ?? 'unknown')}\n` +
      `  @capacitor/android: ${c.weak(androidVersion ?? 'unknown')}\n` +
      `  @capacitor/ios: ${c.weak(iosVersion ?? 'unknown')}\n\n` +
      `${c.strong('Installed Dependencies:')}\n\n`,
  );

  await printInstalledPackages(config);

  output.write('\n');
}

async function printInstalledPackages(config: Config) {
  const packageNames = ['@capacitor/cli', '@capacitor/core', '@capacitor/android', '@capacitor/ios'];
  await Promise.all(
    packageNames.map(async (packageName) => {
      const packagePath = resolveNode(config.app.rootDir, packageName, 'package.json');
      await printPackageVersion(packageName, packagePath);
    }),
  );
}

async function printPackageVersion(packageName: string, packagePath: string | null) {
  let version;
  if (packagePath) {
    version = (await readJSON(packagePath)).version;
  }
  output.write(`  ${packageName}: ${c.weak(version || 'not installed')}\n`);
}

export async function doctor(config: Config, platformName: string): Promise<void> {
  if (platformName === config.ios.name) {
    await doctorIOS(config);
  } else if (platformName === config.android.name) {
    await doctorAndroid(config);
  } else if (platformName === config.web.name) {
    return Promise.resolve();
  } else {
    throw `Platform ${platformName} is not valid.`;
  }
}