All files / src/withPushNotifications withAndroidPushNotifications.utils.ts

100% Statements 12/12
100% Branches 14/14
100% Functions 3/3
100% Lines 12/12

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                              4x 3x               1x                                             5x   4x       4x               1x                 4x       4x     4x       2x     2x    
interface GradleDependency {
  classpath: string;
  version?: string;
}
 
/**
 * Add a dependency to the project build.gradle file.
 * @param buildGradle - The build.gradle file
 * @param options - The options
 * @returns The updated build.gradle file
 */
export function addProjectDependency(
  buildGradle: string,
  options: GradleDependency
) {
  if (!buildGradle.includes(options?.classpath)) {
    return buildGradle.replace(
      /dependencies\s?{/,
      `dependencies {
        classpath('${options?.classpath}${
          options?.version ? `:${options?.version}` : ''
        }')`
    );
  } else {
    return buildGradle;
  }
}
 
interface AppGradleDependency extends GradleDependency {
  /**
   * The string to add to the dependencies block.
   *
   * If this is not provided, ${classpath}:${version} will be used.
   */
  implementation?: string;
}
 
/**
 * Add a dependency to the app build.gradle file.
 * @param buildGradle - The build.gradle file
 * @param options - The options
 * @returns The updated build.gradle file
 */
export function addAppDependency(
  buildGradle: string,
  options: AppGradleDependency
) {
  if (!buildGradle.includes(options?.classpath)) {
    const implementationString =
      options?.implementation ??
      `'${options?.classpath}${
        options?.version ? `:${options?.version}` : ''
      }'`;
    return buildGradle.replace(
      /dependencies\s?{/,
      // NOTE: awkard spacing is intentional -- it ensure correct alignment in
      // the output build.gradle file
      `dependencies {
    implementation ${implementationString}`
    );
  } else {
    return buildGradle;
  }
}
 
/**
 * Add the apply plugin line to the app build.gradle file if it doesn't exist.
 */
export function addApplyPlugin(appBuildGradle: string, pluginName: string) {
  // Check for `apply plugin: 'com.google.gms.google-services'`
  const applyPluginPattern = new RegExp(
    `apply\\s+plugin:\\s+['"]${pluginName}['"]`
  );
  // Check for `plugins { id 'com.google.gms.google-services' }`
  const pluginIdPattern = new RegExp(`id\\s+['"]${pluginName}['"]`);
 
  // Make sure the project does not have the plugin already
  if (
    !appBuildGradle.match(applyPluginPattern) &&
    !appBuildGradle.match(pluginIdPattern)
  ) {
    return appBuildGradle + `\napply plugin: '${pluginName}'`;
  }
 
  return appBuildGradle;
}