All files / src/cargoInterop metadata.ts

77.47% Statements 86/111
62.5% Branches 5/8
66.66% Functions 2/3
77.47% Lines 86/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1121x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x     4x 4x 4x 4x 1x 1x                                             1x 1x 4x 4x 4x   4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { exec } from 'child_process';
import { Observable, from, mergeMap } from 'rxjs';
import { Option, some, none } from 'fp-ts/Option';
 
const versionTest = /cargo\s(\d+\.\d+\.\d+).*/i;
 
type MetadataArgs = {
  cwd?: string,
  envConfig?: Record<string, string>
}
 
const cargoExists = ({cwd, envConfig = {}}: MetadataArgs = {}): Observable<boolean> => {
  const env = { ...process.env, ...envConfig };
  return new Observable((subscriber) => {
    exec('cargo --version', { cwd, env, windowsHide: true }, (error, stdout, stderr) => {
      if (error || stderr) {
        subscriber.next(false);
      } else if (stdout) {
        subscriber.next(versionTest.test(stdout));
      }
      subscriber.complete();
    });
  });
};
 
const cargoMetadata = ({cwd, envConfig = {}}: MetadataArgs = {}): Observable<Option<Metadata>> => {
  const env = { ...process.env, ...envConfig };
  return new Observable((subscriber) => {
    exec('cargo metadata --no-deps --format-version 1 -q', { cwd, env, windowsHide: true }, (error, stdout, stderr) => {
      if (error || stderr) {
        subscriber.next(none);
      } else if (stdout) {
        try {
          const metadata: Metadata = JSON.parse(stdout);
          if (metadata) {
            subscriber.next(some(metadata));
          } else {
            subscriber.next(none);
          }
        } catch {
          subscriber.next(none);
        }
      }

      subscriber.complete();
    });
  });
};
 
export const cargoSafeMetadata = ({cwd, envConfig = {}}: MetadataArgs = {}): Observable<Option<Metadata>> => {
  return cargoExists({ cwd, envConfig}).pipe(
    mergeMap((exists) => {
      if (exists) {
        return cargoMetadata({cwd, envConfig});
      } else {
        return from([none]);
      }
    })
  );
};
 
type Metadata = {
  packages: Package[];
  workspace_members: string[];
  workspace_default_members: string[];
  resolve: any;
  target_directory: string;
  version: number;
  workspace_root: string;
};
 
type Package = {
  name: string;
  version: string;
  id: string;
  license: string;
  license_file: string;
  description: string;
  source: string | null;
  dependencies: Dependency[];
  targets: Target[];
  features: Features;
  manifest_path: string;
};
 
export interface Dependency {
  name: string;
  source: string;
  req: string;
  kind: 'dev' | 'build' | null;
  rename: string | null;
  optional: boolean;
  uses_default_features: boolean;
  features: string[];
  target: string | null;
  path?: string;
  registry: string | null;
}
 
export interface Target {
  kind: string[];
  crate_types: string[];
  name: string;
  src_path: string;
  edition: string;
  doc: boolean;
  doctest: boolean;
  test: boolean;
}
 
export interface Features {}