diff --git a/packages/community-cli-plugin/src/commands/bundle/assetPathUtils.js b/packages/community-cli-plugin/src/commands/bundle/assetPathUtils.js index 0207bb7951a..84261469a32 100644 --- a/packages/community-cli-plugin/src/commands/bundle/assetPathUtils.js +++ b/packages/community-cli-plugin/src/commands/bundle/assetPathUtils.js @@ -40,7 +40,7 @@ function getAndroidAssetSuffix(scale: number): string { } // See https://developer.android.com/guide/topics/resources/drawable-resource.html -const drawableFileTypes = new Set([ +const drawableFileTypes: Set = new Set([ 'gif', 'jpeg', 'jpg', @@ -86,6 +86,7 @@ function getBasePath(asset: PackagerAsset): string { } export default { + drawableFileTypes, getAndroidAssetSuffix, getAndroidResourceFolderName, getResourceIdentifier, diff --git a/packages/community-cli-plugin/src/commands/bundle/createKeepFileAsync.js b/packages/community-cli-plugin/src/commands/bundle/createKeepFileAsync.js new file mode 100644 index 00000000000..dd347b6b319 --- /dev/null +++ b/packages/community-cli-plugin/src/commands/bundle/createKeepFileAsync.js @@ -0,0 +1,40 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +import type {AssetData} from 'metro/src/Assets'; + +import assetPathUtils from './assetPathUtils'; +import fs from 'fs'; +import path from 'path'; + +async function createKeepFileAsync( + assets: $ReadOnlyArray, + outputDirectory: string, +): Promise { + if (!assets.length) { + return; + } + const assetsList = []; + for (const asset of assets) { + const prefix = assetPathUtils.drawableFileTypes.has(asset.type) + ? 'drawable' + : 'raw'; + assetsList.push( + `@${prefix}/${assetPathUtils.getResourceIdentifier(asset)}`, + ); + } + const keepPath = path.join(outputDirectory, 'raw/keep.xml'); + const content = `\n`; + await fs.promises.mkdir(path.dirname(keepPath), {recursive: true}); + await fs.promises.writeFile(keepPath, content); +} + +export default createKeepFileAsync; diff --git a/packages/community-cli-plugin/src/commands/bundle/saveAssets.js b/packages/community-cli-plugin/src/commands/bundle/saveAssets.js index cb40997c322..9f168d7777c 100644 --- a/packages/community-cli-plugin/src/commands/bundle/saveAssets.js +++ b/packages/community-cli-plugin/src/commands/bundle/saveAssets.js @@ -17,6 +17,7 @@ import { isCatalogAsset, writeImageSet, } from './assetCatalogIOS'; +import createKeepFileAsync from './createKeepFileAsync'; import filterPlatformAssetScales from './filterPlatformAssetScales'; import getAssetDestPathAndroid from './getAssetDestPathAndroid'; import getAssetDestPathIOS from './getAssetDestPathIOS'; @@ -88,6 +89,9 @@ async function saveAssets( } else { assets.forEach(addAssetToCopy); } + if (platform === 'android') { + await createKeepFileAsync(assets, assetsDest); + } return copyAll(filesToCopy); }