Add environment variables using Belt CLI - #78
Conversation
0eb08b9 to
19e8dc0
Compare
|
I don't think having the dotenv package is a requirement for adding environmental variables to an expo project. One can load env vars in an expo project without another external dependency as long as the env vars are consistent with what is specified on Expo docs. |
19e8dc0 to
4761adb
Compare
Nice catch. I will tweak this PR.
Setting up env variables is something that happens in most mobile projects. As a CLI, this makes it quicker with one command including setting up test environments. Since we don't have a roadmap for Belt features, I felt this would provide some value. Should we get together and come up with a roadmap of Belt features as I'm keen to add better features providing value? |
| await fs.copy(path.join(projectDir, '.env.example'), envPath); | ||
| } | ||
|
|
||
| await addToGitignore('.env'); |
There was a problem hiding this comment.
We might need to check if the entry already exists in the .ignore file to prevent duplicates.
| ): Promise<boolean> { | ||
| if (!(await fs.pathExists(filePath))) return false; | ||
| const contents = (await fs.readFile(filePath)).toString(); | ||
| const updated = contents.replace(search, replacement); |
There was a problem hiding this comment.
I believe .replace will only replace the first occurrence, would .replaceAll be more suitable here?
| @@ -4,3 +4,9 @@ export default async function commit(message: string) { | |||
| await exec('git add .'); | |||
| await exec(`git commit -m "${message}"`); | |||
There was a problem hiding this comment.
I realized this could open a door for injection, for now it is alright because the message being passed is hardcoded, but we might need to rethink thins.
codeofdiego
left a comment
There was a problem hiding this comment.
Looks good, I just had some minor notes.
In this PR, we add a new
belt add envCLI command that sets up environment variable management in an Expo project using theEXPO_PUBLIC_mechanism.What the command does
dotenvas a dev dependency..env.example,.env.test,jest.setup.env.js, andsrc/config/index.ts..envfrom.env.exampleif it doesn't already exist.src/util/api/api.tsto replace the hardcoded GitHub API URL withEXPO_PUBLIC_API_BASE_URL.jest.config.jsto loadjest.setup.env.jsviasetupFiles.Refactoring existing notifications code
Reusable utilities were extracted as part of this work and applied to both env and notifications:
src/util/patchFile.ts- generic read/search/replace/write function to patch existing project files.src/util/confirmToProceed.ts- interactive intro prompt shared across commands.handleCommitErrormoved to be used in multiple commands.Both env and notifications command now have: