Created
July 4, 2024 20:11
-
-
Save oidoug/90881ec099d7bdd41069c1973976fcba to your computer and use it in GitHub Desktop.
Expo Config Plugin for bundling Realm DB file to iOS and Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable @typescript-eslint/no-var-requires */ | |
const path = require('path'); | |
const fs = require('fs'); | |
const { | |
withXcodeProject, | |
IOSConfig, | |
withDangerousMod, | |
} = require('expo/config-plugins'); | |
function withRealmAssetIos(config, fileArray) { | |
return withXcodeProject(config, async (config) => { | |
if (!Array.isArray(fileArray)) { | |
fileArray = [fileArray]; | |
} | |
for (const file of fileArray) { | |
config.modResults = await setFileInXcodeProjectAsync({ | |
file, | |
projectName: config.modRequest.projectName, | |
project: config.modResults, | |
}); | |
} | |
return config; | |
}); | |
} | |
async function setFileInXcodeProjectAsync({ file, projectName, project }) { | |
// Resource path relative to Xcode project root | |
const filepath = path.join('../', file); | |
if (!project.hasFile(filepath)) { | |
// eslint-disable-next-line no-console | |
console.log(`✔ [Realm-iOS] Adding ${filepath} as Resource`); | |
IOSConfig.XcodeUtils.addResourceFileToGroup({ | |
filepath, | |
groupName: projectName, | |
project, | |
isBuildFile: true, | |
}); | |
} | |
return project; | |
} | |
const androidFolderPath = 'app/src/main/assets'; | |
const withRealmAssetAndroid = (expoConfig, fileArray) => | |
withDangerousMod(expoConfig, [ | |
'android', | |
(modConfig) => { | |
if (modConfig.modRequest.platform === 'android') { | |
const androidAssetsPath = path.join( | |
modConfig.modRequest.platformProjectRoot, | |
androidFolderPath | |
); | |
if (!Array.isArray(fileArray)) { | |
fileArray = [fileArray]; | |
} | |
fileArray.forEach((file) => { | |
const filepath = path.join(androidAssetsPath, path.basename(file)); | |
// eslint-disable-next-line no-console | |
console.log(`✔ [Realm-Android] Adding ${file} to assets folder`); | |
if (!fs.existsSync(path.dirname(filepath))) { | |
fs.mkdirSync(path.dirname(filepath), { recursive: true }); | |
} | |
fs.copyFileSync(file, filepath); | |
}); | |
} | |
return modConfig; | |
}, | |
]); | |
const withRealmAsset = (config, props) => { | |
config = withRealmAssetIos(config, props); | |
config = withRealmAssetAndroid(config, props); | |
return config; | |
}; | |
module.exports = withRealmAsset; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment