|
| 1 | +const { withDangerousMod } = require('@expo/config-plugins'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +/** |
| 6 | + * This config plugin copies files from the specified paths, like the assets |
| 7 | + * directory, to the Android drawable directory. |
| 8 | + */ |
| 9 | +function withAndroidDrawables(config, { drawableFiles }) { |
| 10 | + return withDangerousMod(config, [ |
| 11 | + 'android', |
| 12 | + (config) => { |
| 13 | + // Validate drawableFiles |
| 14 | + if (!Array.isArray(drawableFiles)) { |
| 15 | + throw new Error('drawableFiles must be an array of file paths'); |
| 16 | + } |
| 17 | + |
| 18 | + // Define the target drawable directory |
| 19 | + const drawableDir = path.join( |
| 20 | + config.modRequest.projectRoot, |
| 21 | + 'android/app/src/main/res/drawable', |
| 22 | + ); |
| 23 | + |
| 24 | + // Create the drawable directory if it doesn't exist |
| 25 | + if (!fs.existsSync(drawableDir)) { |
| 26 | + fs.mkdirSync(drawableDir, { recursive: true }); |
| 27 | + } |
| 28 | + |
| 29 | + // Copy each drawable file to the drawable directory |
| 30 | + drawableFiles.forEach((filePath) => { |
| 31 | + const sourcePath = path.resolve(config.modRequest.projectRoot, filePath); |
| 32 | + const fileName = path.basename(filePath); |
| 33 | + const destPath = path.join(drawableDir, fileName); |
| 34 | + |
| 35 | + if (!fs.existsSync(sourcePath)) { |
| 36 | + console.warn(`Warning: Drawable file not found: ${sourcePath}`); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Copy the file |
| 41 | + fs.copyFileSync(sourcePath, destPath); |
| 42 | + }); |
| 43 | + |
| 44 | + return config; |
| 45 | + }, |
| 46 | + ]); |
| 47 | +} |
| 48 | + |
| 49 | +module.exports = withAndroidDrawables; |
0 commit comments