All files / src/android Drawable.xml.ts

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8

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                        2x     6x                                                                                 6x 6x                     6x 6x 6x 6x    
import path from 'path';
import { Element } from 'xml-js';
 
import { ResizeMode } from '../constants';
import {
  mergeXmlElements,
  readXmlFile,
  writeXmlFile,
  ExpectedElementsType,
  ExpectedElementType,
} from '../xml-manipulation';
 
const DRAWABLE_XML_FILE_PATH = `./res/drawable/splashscreen.xml`;
 
function configureDrawable(xml: Element, resizeMode: ResizeMode): Element {
  const expected: ExpectedElementsType = {
    elements: [
      {
        idx: 0,
        comment: `\n  This file was created by '@expo/configure-splash-screen' and some of it's content shouldn't be modified by hand\n`,
      },
      {
        name: 'layer-list',
        attributes: {
          'xmlns:android': 'http://schemas.android.com/apk/res/android',
        },
        elements: {
          newValue: ([
            {
              name: 'item',
              attributes: {
                'android:drawable': '@color/splashscreen_background',
              },
            },
          ] as ExpectedElementType[]).concat(
            resizeMode !== ResizeMode.NATIVE
              ? []
              : [
                  {
                    name: 'item',
                    elements: [
                      {
                        name: 'bitmap',
                        attributes: {
                          'android:gravity': 'center',
                          'android:src': '@drawable/splashscreen_image',
                        },
                      },
                    ],
                  },
                ]
          ),
        },
      },
    ],
  };
  const result = mergeXmlElements(xml, expected);
  return result;
}
 
/**
 * @param androidMainPath Path to the main directory containing code and resources in Android project. In general that would be `android/app/src/main`.
 * @param resizeMode
 */
export default async function configureDrawableXml(
  androidMainPath: string,
  resizeMode: ResizeMode
) {
  const filePath = path.resolve(androidMainPath, DRAWABLE_XML_FILE_PATH);
  const xmlContent = await readXmlFile(filePath);
  const configuredXmlContent = configureDrawable(xmlContent, resizeMode);
  await writeXmlFile(filePath, configuredXmlContent);
}