Skip to content

Commit 3bfcaab

Browse files
authored
fix(copy): only copy specified resources (#105)
1 parent 7d76022 commit 3bfcaab

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getConfigPath, read as readConfig, run as runConfig, write as writeConf
99
import { BaseError } from './error';
1010
import { NativeProjectConfig, copyToNativeProject } from './native';
1111
import { GeneratedResource, PLATFORMS, Platform, RunPlatformOptions, prettyPlatform, run as runPlatform } from './platform';
12-
import { Density, Orientation, ResolvedSource, SourceType } from './resources';
12+
import { Density, Orientation, ResolvedSource, ResourceType, SourceType } from './resources';
1313
import { tryFn } from './utils/fn';
1414

1515
const debug = Debug('cordova-res');
@@ -78,7 +78,9 @@ async function CordovaRes(options: CordovaRes.Options = {}): Promise<Result> {
7878
sources.push(...platformResult.sources);
7979

8080
if (copy && nativeProject) {
81-
await copyToNativeProject(platform, nativeProject, logstream, errstream);
81+
const shouldCopyIcons = resources.findIndex(res => res.type === ResourceType.ICON || res.type === ResourceType.ADAPTIVE_ICON) !== -1;
82+
const shouldCopySplash = resources.findIndex(res => res.type === ResourceType.SPLASH) !== -1;
83+
await copyToNativeProject(platform, nativeProject, shouldCopyIcons, shouldCopySplash, logstream, errstream);
8284
}
8385
}
8486
}

src/native.ts

+21-8
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const ANDROID_SPLASHES: readonly ProcessItem[] = [
169169
},
170170
];
171171

172-
async function copyImages(sourcePath: string, targetPath: string, images: readonly ProcessItem[]) {
172+
async function copyImages(sourcePath: string, targetPath: string, images: readonly ProcessItem[]): Promise<number> {
173173
await Promise.all(images.map(async item => {
174174
const source = path.join(sourcePath, item.source);
175175
const target = path.join(targetPath, item.target);
@@ -178,20 +178,33 @@ async function copyImages(sourcePath: string, targetPath: string, images: readon
178178

179179
await copy(source, target);
180180
}));
181+
182+
return images.length;
181183
}
182184

183-
export async function copyToNativeProject(platform: Platform, nativeProject: NativeProjectConfig, logstream: NodeJS.WritableStream | null, errstream: NodeJS.WritableStream | null) {
185+
export async function copyToNativeProject(platform: Platform, nativeProject: NativeProjectConfig, shouldCopyIcons: boolean, shouldCopySplash: boolean, logstream: NodeJS.WritableStream | null, errstream: NodeJS.WritableStream | null) {
186+
let count = 0;
187+
184188
if (platform === Platform.IOS) {
185189
const iosProjectDirectory = nativeProject.directory || 'ios';
186-
await copyImages(SOURCE_IOS_ICON, path.join(iosProjectDirectory, TARGET_IOS_ICON), IOS_ICONS);
187-
await copyImages(SOURCE_IOS_SPLASH, path.join(iosProjectDirectory, TARGET_IOS_SPLASH), IOS_SPLASHES);
188-
logstream?.write(util.format(`Copied %s resource items to %s`, IOS_ICONS.length + IOS_SPLASHES.length, prettyPlatform(platform)) + '\n');
190+
if (shouldCopyIcons) {
191+
count += await copyImages(SOURCE_IOS_ICON, path.join(iosProjectDirectory, TARGET_IOS_ICON), IOS_ICONS);
192+
}
193+
if (shouldCopySplash) {
194+
count += await copyImages(SOURCE_IOS_SPLASH, path.join(iosProjectDirectory, TARGET_IOS_SPLASH), IOS_SPLASHES);
195+
}
189196
} else if (platform === Platform.ANDROID) {
190197
const androidProjectDirectory = nativeProject.directory || 'android';
191-
await copyImages(SOURCE_ANDROID_ICON, path.join(androidProjectDirectory, TARGET_ANDROID_ICON), ANDROID_ICONS);
192-
await copyImages(SOURCE_ANDROID_SPLASH, path.join(androidProjectDirectory, TARGET_ANDROID_SPLASH), ANDROID_SPLASHES);
193-
logstream?.write(util.format(`Copied %s resource items to %s`, ANDROID_ICONS.length + ANDROID_SPLASHES.length, prettyPlatform(platform)) + '\n');
198+
if (shouldCopyIcons) {
199+
count += await copyImages(SOURCE_ANDROID_ICON, path.join(androidProjectDirectory, TARGET_ANDROID_ICON), ANDROID_ICONS);
200+
}
201+
if (shouldCopySplash) {
202+
count += await copyImages(SOURCE_ANDROID_SPLASH, path.join(androidProjectDirectory, TARGET_ANDROID_SPLASH), ANDROID_SPLASHES);
203+
}
194204
} else {
195205
errstream?.write(util.format('WARN:\tCopying to native projects is not supported for %s', prettyPlatform(platform)) + '\n');
206+
return;
196207
}
208+
209+
logstream?.write(util.format(`Copied %s resource items to %s`, count, prettyPlatform(platform)) + '\n');
197210
}

0 commit comments

Comments
 (0)