export function getInstalledPackages()

in torchlive-cli/src/android/AndroidSDK.ts [105:161]


export function getInstalledPackages(): Package[] {
  // An option on unix would be to use:
  //
  // `sdkmanager --list | sed -e '/Available Packages/q'`
  //
  // However, this would most likely not work for all potentially supported
  // platform. Therefore, we are going to substring the output from `--list`.
  const listOutput = execaCommandSync(getSDKManagerPath(), ['--list']);
  const splitListOutput = listOutput.substring(
    0,
    listOutput.indexOf('Available Packages'),
  );

  const packages: Package[] = [];

  const regex = /^\s\s(\S+)\s*\|\s([\d.]+)\s+\|\s(.*)\s+\|\s(.+)\s*$/gm;

  let m: RegExpExecArray;
  while ((m = regex.exec(splitListOutput)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
      regex.lastIndex++;
    }

    let packagePath: string;
    let packageVersion: string;
    let packageDescription: string;
    let packageLocation: string;

    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
      switch (groupIndex) {
        case 1:
          packagePath = match;
          break;
        case 2:
          packageVersion = match;
          break;
        case 3:
          packageDescription = match;
          break;
        case 4:
          packageLocation = match;
          break;
      }
    });

    packages.push({
      path: packagePath,
      version: packageVersion,
      description: packageDescription,
      location: packageLocation,
    });
  }

  return packages;
}