Skip to content

Commit

Permalink
fix(import): import android drawables
Browse files Browse the repository at this point in the history
close #14
  • Loading branch information
Murat committed Jun 1, 2024
1 parent 9b5b40d commit 1e51bd3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe('importAndroidLaunchIcon', () => {
'/oldProject/android/app/src/main/res/mipmap-any/ic_launcher.png',
'old image data'
);
mockFs.writeFileSync(
'/oldProject/android/app/src/main/res/drawable-any/ic_notification.png',
'old image data'
);
mockFs.writeFileSync(
path.join(
getProjectPath(),
Expand Down
38 changes: 32 additions & 6 deletions src/utils/upgrade/android/importAndroidLaunchIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function importAndroidLaunchIcon(
[projectPath, 'android/app/src/main/res/mipmap-*/*'].join('/'),
{ nodir: true }
);
const drawables = globSync(
[projectPath, 'android/app/src/main/res/drawable*/*'].join('/'),
{ nodir: true }
);
// get launcher icon and launcher round icon name from AndroidManifest.xml
const manifestPath = path.join(
projectPath,
Expand All @@ -28,9 +32,10 @@ export function importAndroidLaunchIcon(
if (!icon) return null;
return {
id: 'androidLaunchIcon',
title: 'Android Launch Icon',
title: 'Android Icons',
value: icon,
apply: () => setAndroidLaunchIcon(projectPath, mipmaps, icon, roundIcon),
apply: () =>
setAndroidLaunchIcon(projectPath, mipmaps, drawables, icon, roundIcon),
};
} catch (e) {
return null;
Expand All @@ -40,6 +45,7 @@ export function importAndroidLaunchIcon(
async function setAndroidLaunchIcon(
oldProjectPath: string,
mipmaps: string[],
drawables: string[],
icon: string,
roundIcon: string | undefined
) {
Expand All @@ -60,10 +66,7 @@ async function setAndroidLaunchIcon(
path.join(oldProjectPath, 'android'),
mipmap
);
const destination = path.join(
path.join(getProjectPath(), 'android'),
relativePath
);
const destination = path.join(getProjectPath(), 'android', relativePath);

// ensure dir exists
await new Promise(r =>
Expand All @@ -75,6 +78,29 @@ async function setAndroidLaunchIcon(
}
logMessage('copied mipmaps from old project');

// copy new drawables
for (const drawable of drawables) {
// get path after android

const relativePath = path.relative(
path.join(oldProjectPath, 'android'),
drawable
);
const destination = path.join(getProjectPath(), 'android', relativePath);

// do not overwrite if exists
if (fs.existsSync(destination)) continue;

// ensure dir exists
await new Promise(r =>
fs.mkdir(path.dirname(destination), { recursive: true }, r)
);

// copy file
await new Promise(r => fs.copyFile(drawable, destination, r));
}
logMessage('copied drawables from old project');

// replace icon and round icon attributes in AndroidManifest.xml
const manifestPath = path.join(
getProjectPath(),
Expand Down

0 comments on commit 1e51bd3

Please sign in to comment.