My setup:
"amazon-cognito-identity-js": "^6.3.13",
"aws-amplify": "^5.3.18",
"aws-amplify-react-native": "^7.0.8",
"expo": "~51.0.11",
"react-native": "0.74.2",
I have a React Native with Expo 51 project and I’m trying to “generate” my aws-exports.js
file dynamically during my builds.
I’ve also created some EAS Secrets with my aws-exports.js
file encoded in BASE64.
In order to have more flexibility in my builds I’ve overwriten the default eas-build-pre-install
hook, so I could run some custom scripts in order to generate my file.
This is working as spected, the aws-exports.js
file is created and handleEncodedFile
is executed
"eas-build-pre-install": "touch ./src/aws-exports.js && node ./src/scripts/handleEncodedFile.scripts.js",
And here is my handleEncodedFile
script:
const fs = require("node:fs");
function createAwsExports() {
// defaults ENV VARs from EAS
const buildProfile = process.env.EAS_BUILD_PROFILE;
const workingDir = process.env.EAS_BUILD_WORKINGDIR;
try {
console.info("Using EAS Build Profile: ", buildProfile);
console.info("Running from DIR: ", workingDir);
let handleDecodedSecret = null;
switch (buildProfile) {
case "development":
const expoDevSecret = process.env.AWS_EXPORTS_DEV;
handleDecodedSecret = Buffer.from(expoDevSecret, "base64").toString();
break;
case "production":
const expoPrdSecret = process.env.AWS_EXPORTS_PRD;
handleDecodedSecret = Buffer.from(expoPrdSecret, "base64").toString();
break;
default:
throw new Error("Unknown build profile: ", buildProfile);
}
if (!handleDecodedSecret) {
throw new Error(
'"handleDecodedSecret" is NULL. This should never happen'
);
}
// returns TRUE
console.log(
'Does "src/aws-exports.js" exists? ',
fs.existsSync(`${workingDir}/src/aws-exports.js`)
);
// Execution stops here
fs.writeFile(`${workingDir}/src/aws-exports.js`, handleDecodedSecret);
} catch (error) {
throw new Error('Error while creating "aws-exports.js": ', error);
}
}
createAwsExports();
And this is the error I’m getting from Expo Dashboard (not very helpful tbh)
Script 'eas-build-pre-install' is present in package.json, running it...
Fri, 07 Jun 2024 02:04:58 GMT
> <my-app>@1.0.0 eas-build-pre-install
> touch ./src/aws-exports.js && node ./src/scripts/handleEncodedFile.scripts.js
Using EAS Build Profile: development
Running from DIR: /home/expo/workingdir/build
Does "src/aws-exports.js" exists? true
/home/expo/workingdir/build/src/scripts/handleEncodedFile.scripts.js:46
throw new Error('Error while creating "aws-exports.js": ', error);
^
Error: Error while creating "aws-exports.js":
at createAwsExports (/home/expo/workingdir/build/src/scripts/handleEncodedFile.scripts.js:46:11)
at Object.<anonymous> (/home/expo/workingdir/build/src/scripts/handleEncodedFile.scripts.js:50:1)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
at node:internal/main/run_main_module:23:47
Node.js v18.18.0
npm run eas-build-pre-install exited with non-zero code: 1
The scripts above should:
- Create an empty
aws-exports.js
in the expo workingdir/home/expo/workingdir/build/src
- Validate which eas profile the build is running on and use the respective EAS Secret
In previous projects I’m using the following script in my package.json
// I always need to alter manually from AWS_EXPORTS_PRD to AWS_EXPORTS_DEV depending on the build
"eas-build-pre-install": "echo $AWS_EXPORTS_PRD | base64 -d -i > ./src/aws-exports.js"
It doesn’t break, but also doesn’t work. For some reason the only way I’ve got it working was removing it from the .gitignore
, which I’m sick of doing.
Thansk for the help in advance!