Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/cli-link-assets/src/__tests__/linkAssets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,59 @@ describe('linkAssets', () => {
),
).toMatchSnapshot();
});
it('should link a font asset whose extension is upper-cased', async () => {
writeFiles(DIR, {
...baseProjectKotlin,
'assets/shared/fonts/Montserrat-Regular.TTF':
fixtureFiles.montserratRegularFont,
});

await linkAssets([], configMock as CLIConfig);

// Android: it is linked as a font resource, not as a custom asset.
expect(readMontserratXMLFontFile()).toContain('@font/montserrat_regular');
expect(
fs.existsSync(
path.resolve(
DIR,
'android/app/src/main/res/font/montserrat_regular.ttf',
),
),
).toBe(true);
expect(
fs.existsSync(
path.resolve(
DIR,
'android/app/src/main/assets/custom/Montserrat-Regular.TTF',
),
),
).toBe(false);
expect(readMainApplicationKotlinFile()).toContain(
'addCustomFont(this, "Montserrat", R.font.montserrat)',
);

// iOS: it is registered in `UIAppFonts`, otherwise the font ships in the
// bundle but cannot be used at runtime.
expect(readInfoPlistFile()).toContain('Montserrat-Regular.TTF');
});

it('should link an image asset whose extension is upper-cased', async () => {
writeFiles(DIR, {
...baseProjectKotlin,
'assets/shared/Upper Image.PNG': fixtureFiles.imagePng,
});

await linkAssets([], configMock as CLIConfig);

expect(
fs.existsSync(
path.resolve(DIR, 'android/app/src/main/res/drawable/upper_image.png'),
),
).toBe(true);
expect(
fs.existsSync(
path.resolve(DIR, 'android/app/src/main/assets/custom/Upper Image.PNG'),
),
).toBe(false);
});
});
15 changes: 14 additions & 1 deletion packages/cli-link-assets/src/fileTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import path from 'path';

const fontTypes = ['otf', 'ttf'] as const;

const imageTypes = ['png', 'jpg', 'gif'] as const;

const audioTypes = ['mp3'] as const;

export {fontTypes, imageTypes, audioTypes};
/**
* Returns the extension of an asset, without the leading dot and lower-cased.
*
* The known asset types above are all spelled in lower case, while the
* extension on disk can be cased in any way (`Lato-Regular.TTF`), so it has to
* be normalized before it is matched against them.
*/
function getAssetExtension(filePath: string) {
return path.extname(filePath).substring(1).toLowerCase();
}

export {fontTypes, imageTypes, audioTypes, getAssetExtension};
5 changes: 3 additions & 2 deletions packages/cli-link-assets/src/tools/linkPlatform/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {CLIError, logger} from '@react-native-community/cli-tools';
import fs from 'fs';
import path from 'path';
import {getAssetExtension} from '../../fileTypes';
import sha1File from '../../sha1File';
import {CleanAssets} from '../cleanAssets/types';
import {CopyAssets} from '../copyAssets/types';
Expand Down Expand Up @@ -173,15 +174,15 @@ function linkPlatform({
.map((fileExt): FileFilter => {
return {
name: fileExt,
filter: (asset) => path.extname(asset.path) === `.${fileExt}`,
filter: (asset) => getAssetExtension(asset.path) === fileExt,
options: linkOptionsPerExt[fileExt as Extension],
};
})
.concat({
name: 'custom',
filter: (asset) =>
Object.keys(linkOptionsPerExt).indexOf(
path.extname(asset.path).substring(1),
getAssetExtension(asset.path),
) === -1,
options: otherLinkOptions,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'path';
import {AssetPathAndSHA1} from '..';
import {fontTypes} from '../../../fileTypes';
import {fontTypes, getAssetExtension} from '../../../fileTypes';
import {Platform} from '../../linkPlatform';

function migration2(
Expand All @@ -12,7 +11,7 @@ function migration2(
shouldRelinkAndroidFonts:
platform === 'android' &&
fontTypes.includes(
path.extname(asset.path).substring(1) as (typeof fontTypes)[number],
getAssetExtension(asset.path) as (typeof fontTypes)[number],
),
}));
}
Expand Down
Loading