|
| 1 | +import { copy } from '@ionic/utils-fs'; |
| 2 | +import Debug from 'debug'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +import { Platform } from './platform'; |
| 6 | + |
| 7 | +export interface NativeProject { |
| 8 | + enabled?: boolean; |
| 9 | + androidProjectDirectory?: string; |
| 10 | + iosProjectDirectory?: string; |
| 11 | +} |
| 12 | +interface ProcessItem { |
| 13 | + source: string; |
| 14 | + target: string; |
| 15 | +} |
| 16 | + |
| 17 | +const debug = Debug('cordova-res:native'); |
| 18 | + |
| 19 | +const SOURCE_IOS_ICON = 'resources/ios/icon/'; |
| 20 | +const SOURCE_IOS_SPLASH = 'resources/ios/splash/'; |
| 21 | + |
| 22 | +const TARGET_IOS_ICON = '/App/App/Assets.xcassets/AppIcon.appiconset/'; |
| 23 | +const TARGET_IOS_SPLASH = '/App/App/Assets.xcassets/Splash.imageset/'; |
| 24 | + |
| 25 | +const SOURCE_ANDROID_ICON = 'resources/android/icon/'; |
| 26 | +const SOURCE_ANDROID_SPLASH = 'resources/android/splash/'; |
| 27 | + |
| 28 | +const TARGET_ANDROID_ICON = '/app/src/main/res/'; |
| 29 | +const TARGET_ANDROID_SPLASH = '/app/src/main/res/'; |
| 30 | + |
| 31 | +const IOS_ICONS: readonly ProcessItem[] = [ |
| 32 | + { source: 'icon-20.png', target: '[email protected]' }, |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + { source: 'icon-29.png', target: '[email protected]' }, |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | + { source: 'icon-40.png', target: '[email protected]' }, |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + { source: 'icon-76.png', target: '[email protected]' }, |
| 47 | + |
| 48 | + |
| 49 | + { source: 'icon-1024.png', target: '[email protected]' }, |
| 50 | +]; |
| 51 | +const IOS_SPLASHES: readonly ProcessItem[] = [ |
| 52 | + { source: 'Default-Portrait@~ipadpro.png', target: 'splash-2732x2732.png' }, |
| 53 | + { source: 'Default-Portrait@~ipadpro.png', target: 'splash-2732x2732-1.png' }, |
| 54 | + { source: 'Default-Portrait@~ipadpro.png', target: 'splash-2732x2732-2.png' }, |
| 55 | +]; |
| 56 | + |
| 57 | +const ANDROID_ICONS: readonly ProcessItem[] = [ |
| 58 | + { source: 'drawable-ldpi-icon.png', target: 'drawable-hdpi-icon.png' }, |
| 59 | + { source: 'drawable-mdpi-icon.png', target: 'mipmap-mdpi/ic_launcher.png' }, |
| 60 | + { |
| 61 | + source: 'drawable-mdpi-icon.png', |
| 62 | + target: 'mipmap-mdpi/ic_launcher_round.png', |
| 63 | + }, |
| 64 | + { |
| 65 | + source: 'drawable-mdpi-icon.png', |
| 66 | + target: 'mipmap-mdpi/ic_launcher_foreground.png', |
| 67 | + }, |
| 68 | + { source: 'drawable-hdpi-icon.png', target: 'mipmap-hdpi/ic_launcher.png' }, |
| 69 | + { |
| 70 | + source: 'drawable-hdpi-icon.png', |
| 71 | + target: 'mipmap-hdpi/ic_launcher_round.png', |
| 72 | + }, |
| 73 | + { |
| 74 | + source: 'drawable-hdpi-icon.png', |
| 75 | + target: 'mipmap-hdpi/ic_launcher_foreground.png', |
| 76 | + }, |
| 77 | + { source: 'drawable-xhdpi-icon.png', target: 'mipmap-xhdpi/ic_launcher.png' }, |
| 78 | + { |
| 79 | + source: 'drawable-xhdpi-icon.png', |
| 80 | + target: 'mipmap-xhdpi/ic_launcher_round.png', |
| 81 | + }, |
| 82 | + { |
| 83 | + source: 'drawable-xhdpi-icon.png', |
| 84 | + target: 'mipmap-xhdpi/ic_launcher_foreground.png', |
| 85 | + }, |
| 86 | + { |
| 87 | + source: 'drawable-xxhdpi-icon.png', |
| 88 | + target: 'mipmap-xxhdpi/ic_launcher.png', |
| 89 | + }, |
| 90 | + { |
| 91 | + source: 'drawable-xxhdpi-icon.png', |
| 92 | + target: 'mipmap-xxhdpi/ic_launcher_round.png', |
| 93 | + }, |
| 94 | + { |
| 95 | + source: 'drawable-xxhdpi-icon.png', |
| 96 | + target: 'mipmap-xxhdpi/ic_launcher_foreground.png', |
| 97 | + }, |
| 98 | + { |
| 99 | + source: 'drawable-xxxhdpi-icon.png', |
| 100 | + target: 'mipmap-xxxhdpi/ic_launcher.png', |
| 101 | + }, |
| 102 | + { |
| 103 | + source: 'drawable-xxxhdpi-icon.png', |
| 104 | + target: 'mipmap-xxxhdpi/ic_launcher_round.png', |
| 105 | + }, |
| 106 | + { |
| 107 | + source: 'drawable-xxxhdpi-icon.png', |
| 108 | + target: 'mipmap-xxxhdpi/ic_launcher_foreground.png', |
| 109 | + }, |
| 110 | +]; |
| 111 | +const ANDROID_SPLASHES: readonly ProcessItem[] = [ |
| 112 | + { source: 'drawable-land-mdpi-screen.png', target: 'drawable/splash.png' }, |
| 113 | + { |
| 114 | + source: 'drawable-land-mdpi-screen.png', |
| 115 | + target: 'drawable-land-mdpi/splash.png', |
| 116 | + }, |
| 117 | + { |
| 118 | + source: 'drawable-land-hdpi-screen.png', |
| 119 | + target: 'drawable-land-hdpi/splash.png', |
| 120 | + }, |
| 121 | + { |
| 122 | + source: 'drawable-land-xhdpi-screen.png', |
| 123 | + target: 'drawable-land-xhdpi/splash.png', |
| 124 | + }, |
| 125 | + { |
| 126 | + source: 'drawable-land-xxhdpi-screen.png', |
| 127 | + target: 'drawable-land-xxhdpi/splash.png', |
| 128 | + }, |
| 129 | + { |
| 130 | + source: 'drawable-land-xxxhdpi-screen.png', |
| 131 | + target: 'drawable-land-xxxhdpi/splash.png', |
| 132 | + }, |
| 133 | + { |
| 134 | + source: 'drawable-port-mdpi-screen.png', |
| 135 | + target: 'drawable-port-mdpi/splash.png', |
| 136 | + }, |
| 137 | + { |
| 138 | + source: 'drawable-port-hdpi-screen.png', |
| 139 | + target: 'drawable-port-hdpi/splash.png', |
| 140 | + }, |
| 141 | + { |
| 142 | + source: 'drawable-port-xhdpi-screen.png', |
| 143 | + target: 'drawable-port-xhdpi/splash.png', |
| 144 | + }, |
| 145 | + { |
| 146 | + source: 'drawable-port-xxhdpi-screen.png', |
| 147 | + target: 'drawable-port-xxhdpi/splash.png', |
| 148 | + }, |
| 149 | + { |
| 150 | + source: 'drawable-port-xxxhdpi-screen.png', |
| 151 | + target: 'drawable-port-xxxhdpi/splash.png', |
| 152 | + }, |
| 153 | +]; |
| 154 | + |
| 155 | +async function copyImages(sourcePath: string, targetPath: string, images: readonly ProcessItem[]) { |
| 156 | + await Promise.all(images.map(async item => { |
| 157 | + const source = path.join(sourcePath, item.source); |
| 158 | + const target = path.join(targetPath, item.target); |
| 159 | + await copy(source, target); |
| 160 | + debug('Copied resource item from %s to %s', source, target); |
| 161 | + })); |
| 162 | +} |
| 163 | + |
| 164 | +export async function copyToNativeProject(platform: Platform, nativeProject: NativeProject, logstream: NodeJS.WritableStream) { |
| 165 | + if (platform === Platform.IOS) { |
| 166 | + const iosProjectDirectory = nativeProject.iosProjectDirectory || 'ios'; |
| 167 | + await copyImages(SOURCE_IOS_ICON, path.join(iosProjectDirectory, TARGET_IOS_ICON), IOS_ICONS); |
| 168 | + await copyImages(SOURCE_IOS_SPLASH, path.join(iosProjectDirectory, TARGET_IOS_SPLASH), IOS_SPLASHES); |
| 169 | + logstream.write(`Copied ${IOS_ICONS.length + IOS_SPLASHES.length} resource items to iOS project`); |
| 170 | + } else if (platform === Platform.ANDROID) { |
| 171 | + const androidProjectDirectory = nativeProject.androidProjectDirectory || 'android'; |
| 172 | + await copyImages(SOURCE_ANDROID_ICON, path.join(androidProjectDirectory, TARGET_ANDROID_ICON), ANDROID_ICONS); |
| 173 | + await copyImages(SOURCE_ANDROID_SPLASH, path.join(androidProjectDirectory, TARGET_ANDROID_SPLASH), ANDROID_SPLASHES); |
| 174 | + logstream.write(`Copied ${ANDROID_ICONS.length + ANDROID_SPLASHES.length} resource items to Android project`); |
| 175 | + } |
| 176 | +} |
0 commit comments