|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +// This script copies assets from react-native-d3-chart to the React Native app |
| 7 | +// It runs automatically when the library is installed via npm/yarn |
| 8 | + |
| 9 | +function copyAssets() { |
| 10 | + try { |
| 11 | + // Check if we're in node_modules (installed as dependency) |
| 12 | + const isInNodeModules = __dirname.includes('node_modules'); |
| 13 | + |
| 14 | + if (!isInNodeModules && !process.env.FORCE_COPY) { |
| 15 | + // We're in development mode, skip copying unless forced |
| 16 | + console.log( |
| 17 | + 'react-native-d3-chart: Skipping asset copy in development mode (set FORCE_COPY=1 to override)' |
| 18 | + ); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Find the React Native app root |
| 23 | + let appRoot; |
| 24 | + if (isInNodeModules) { |
| 25 | + // In production: should be 2 levels up from node_modules/react-native-d3-chart |
| 26 | + appRoot = path.resolve(__dirname, '../../..'); |
| 27 | + } else { |
| 28 | + // In development: use the example directory as app root |
| 29 | + appRoot = path.resolve(__dirname, '../example'); |
| 30 | + } |
| 31 | + |
| 32 | + // Asset source locations in the library |
| 33 | + const androidAssetsSource = path.join( |
| 34 | + __dirname, |
| 35 | + '../android/src/main/assets' |
| 36 | + ); |
| 37 | + const iosAssetsSource = path.join(__dirname, '../ios/assets'); |
| 38 | + |
| 39 | + // Asset destination locations in the consuming app |
| 40 | + const androidAssetsTarget = path.join( |
| 41 | + appRoot, |
| 42 | + 'android/app/src/main/assets/react-native-d3-chart' |
| 43 | + ); |
| 44 | + |
| 45 | + // Copy Android assets |
| 46 | + if (fs.existsSync(androidAssetsSource)) { |
| 47 | + try { |
| 48 | + // Ensure target directory exists |
| 49 | + fs.mkdirSync(androidAssetsTarget, { recursive: true }); |
| 50 | + |
| 51 | + // Copy each file |
| 52 | + const files = fs.readdirSync(androidAssetsSource); |
| 53 | + files.forEach((file) => { |
| 54 | + const sourcePath = path.join(androidAssetsSource, file); |
| 55 | + const targetPath = path.join(androidAssetsTarget, file); |
| 56 | + |
| 57 | + // Only copy files (not directories) |
| 58 | + if (fs.lstatSync(sourcePath).isFile()) { |
| 59 | + fs.copyFileSync(sourcePath, targetPath); |
| 60 | + console.log( |
| 61 | + `react-native-d3-chart: Copied ${file} to Android assets` |
| 62 | + ); |
| 63 | + } |
| 64 | + }); |
| 65 | + } catch (error) { |
| 66 | + console.warn( |
| 67 | + 'react-native-d3-chart: Failed to copy Android assets:', |
| 68 | + error.message |
| 69 | + ); |
| 70 | + } |
| 71 | + } else { |
| 72 | + console.warn( |
| 73 | + 'react-native-d3-chart: Android assets not found at:', |
| 74 | + androidAssetsSource |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + // Copy iOS assets |
| 79 | + if (fs.existsSync(iosAssetsSource)) { |
| 80 | + try { |
| 81 | + const iosDir = path.join(appRoot, 'ios'); |
| 82 | + if (fs.existsSync(iosDir)) { |
| 83 | + // iOS assets should go to ios/assets directory |
| 84 | + const iosAssetsTargetDir = path.join(iosDir, 'assets'); |
| 85 | + |
| 86 | + try { |
| 87 | + // Ensure target directory exists |
| 88 | + fs.mkdirSync(iosAssetsTargetDir, { recursive: true }); |
| 89 | + |
| 90 | + // Copy each file |
| 91 | + const files = fs.readdirSync(iosAssetsSource); |
| 92 | + files.forEach((file) => { |
| 93 | + const sourcePath = path.join(iosAssetsSource, file); |
| 94 | + const targetPath = path.join(iosAssetsTargetDir, file); |
| 95 | + |
| 96 | + // Only copy files (not directories or .DS_Store) |
| 97 | + if ( |
| 98 | + fs.lstatSync(sourcePath).isFile() && |
| 99 | + !file.startsWith('.DS_Store') |
| 100 | + ) { |
| 101 | + fs.copyFileSync(sourcePath, targetPath); |
| 102 | + console.log( |
| 103 | + `react-native-d3-chart: Copied ${file} to iOS assets` |
| 104 | + ); |
| 105 | + } |
| 106 | + }); |
| 107 | + } catch (error) { |
| 108 | + console.warn( |
| 109 | + 'react-native-d3-chart: Failed to copy iOS assets:', |
| 110 | + error.message |
| 111 | + ); |
| 112 | + } |
| 113 | + } else { |
| 114 | + console.log( |
| 115 | + 'react-native-d3-chart: iOS directory not found, skipping iOS assets' |
| 116 | + ); |
| 117 | + } |
| 118 | + } catch (error) { |
| 119 | + console.warn( |
| 120 | + 'react-native-d3-chart: Failed to access iOS directory:', |
| 121 | + error.message |
| 122 | + ); |
| 123 | + } |
| 124 | + } else { |
| 125 | + console.warn( |
| 126 | + 'react-native-d3-chart: iOS assets not found at:', |
| 127 | + iosAssetsSource |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + console.log('react-native-d3-chart: Assets copied successfully'); |
| 132 | + } catch (error) { |
| 133 | + console.warn( |
| 134 | + 'react-native-d3-chart: Failed to copy assets:', |
| 135 | + error.message |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +copyAssets(); |
0 commit comments