All files / src/android Drawables.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 2/2
100% Lines 11/11

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      2x   2x                                                     2x 2x                     5x   30x 6x         5x 3x 1x   3x            
import fs from 'fs-extra';
import path from 'path';
 
const SPLASH_SCREEN_FILENAME = 'splashscreen_image.png';
 
const DRAWABLES_CONFIGS = {
  default: {
    path: `./res/drawable/${SPLASH_SCREEN_FILENAME}`,
    dimensionsMultiplier: 1,
  },
  mdpi: {
    path: `./res/drawable-mdpi/${SPLASH_SCREEN_FILENAME}`,
    dimensionsMultiplier: 1,
  },
  hdpi: {
    path: `./res/drawable-hdpi/${SPLASH_SCREEN_FILENAME}`,
    dimensionsMultiplier: 1.5,
  },
  xhdpi: {
    path: `./res/drawable-xhdpi/${SPLASH_SCREEN_FILENAME}`,
    dimensionsMultiplier: 2,
  },
  xxhdpi: {
    path: `./res/drawable-xxhdpi/${SPLASH_SCREEN_FILENAME}`,
    dimensionsMultiplier: 3,
  },
  xxxhdpi: {
    path: `./res/drawable-xxxhdpi/${SPLASH_SCREEN_FILENAME}`,
    dimensionsMultiplier: 4,
  },
};
 
const DRAWABLE_DIR_PATH = './res/drawable';
const SPLASH_SCREEN_DRAWABLE_PATH = `./res/drawable/${SPLASH_SCREEN_FILENAME}`;
 
/**
 * Deletes all previous splash_screen_images and copies new one to desired drawable directory.
 * If path isn't provided then no new image is placed in drawable directories.
 * @see https://developer.android.com/training/multiscreen/screendensities
 */
export default async function configureDrawables(
  androidMainPath: string,
  splashScreenImagePath?: string
) {
  await Promise.all(
    Object.values(DRAWABLES_CONFIGS).map(async ({ path: drawbalePath }) => {
      if (await fs.pathExists(path.resolve(androidMainPath, drawbalePath))) {
        await fs.remove(path.resolve(androidMainPath, drawbalePath));
      }
    })
  );
 
  if (splashScreenImagePath) {
    if (!(await fs.pathExists(path.resolve(androidMainPath, DRAWABLE_DIR_PATH)))) {
      await fs.mkdir(path.resolve(androidMainPath, DRAWABLE_DIR_PATH));
    }
    await fs.copyFile(
      splashScreenImagePath,
      path.resolve(androidMainPath, SPLASH_SCREEN_DRAWABLE_PATH)
    );
  }
}