From 6fa177c7454ed23bc3b16709f2c8cbdaebcfc260 Mon Sep 17 00:00:00 2001 From: ErikSin <67773827+ErikSin@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:18:35 -0800 Subject: [PATCH 1/6] chore: created script to extract env into js --- bitrise.yml | 5 +++++ scripts/generate_env.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 scripts/generate_env.js diff --git a/bitrise.yml b/bitrise.yml index 28ac9966e..d98284814 100644 --- a/bitrise.yml +++ b/bitrise.yml @@ -323,6 +323,11 @@ workflows: inputs: - npm_version: "" - command: run build:translations + - npm@1.1: + title: Build environment variables + inputs: + - npm_version: "" + - command: run build:env - script@1.2.0: title: Set version name & code inputs: diff --git a/scripts/generate_env.js b/scripts/generate_env.js new file mode 100644 index 000000000..320d96bdd --- /dev/null +++ b/scripts/generate_env.js @@ -0,0 +1,34 @@ +const fs = require("fs"); +const path = require("path"); + +// Path to your .env file +const envPath = path.resolve(__dirname, "../.env"); + +// Read the contents of the .env file +const envFileContent = fs.readFileSync(envPath, "utf8"); + +// Split the file contents into lines +const lines = envFileContent.split("\n"); + +// Create an object to hold the environment variables +const envVariables = {}; + +// Iterate over each line and set environment variables +lines.forEach(line => { + // Skip empty lines and comments + if (line.trim() !== "" && !line.startsWith("#")) { + // Split the line into key-value pairs + const [key, value] = line.split("="); + // Set the environment variable in the object + envVariables[key.trim()] = value.trim(); + } +}); + +// Convert the object to a string representation +const envString = JSON.stringify(envVariables, null, 4); + +// Write the environment variables to a JavaScript file +const outputPath = path.resolve(__dirname, "../env.js"); +fs.writeFileSync(outputPath, `export default ${envString};`, "utf8"); + +console.log("Environment variables injected into env.js"); From 5bc40fb6a8821472265a463cd96994e272d15cd1 Mon Sep 17 00:00:00 2001 From: ErikSin <67773827+ErikSin@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:19:44 -0800 Subject: [PATCH 2/6] chore updated gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 45edb0a3a..1b13068c9 100644 --- a/.gitignore +++ b/.gitignore @@ -78,6 +78,7 @@ buck-out/ # Private env variables .env .bitrise.secrets.yml +env.js # nodejs-mobile assets generated by build script # TODO: somehow modify gradle build scripts so that these can be stored in From b8d39b08401963c8e2d6ef2127e583e78fc62bb3 Mon Sep 17 00:00:00 2001 From: ErikSin <67773827+ErikSin@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:20:42 -0800 Subject: [PATCH 3/6] chore: updated where mapbox token was being accessed --- src/config.json | 3 --- src/frontend/hooks/useMapAvailability.ts | 4 ++-- src/frontend/sharedComponents/Map/MapView.js | 4 ++-- 3 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 src/config.json diff --git a/src/config.json b/src/config.json deleted file mode 100644 index 7afaaf204..000000000 --- a/src/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "mapboxAccessToken": "pk.eyJ1IjoiZ21hY2xlbm5hbiIsImEiOiJSaWVtd2lRIn0.ASYMZE2HhwkAw4Vt7SavEg" -} diff --git a/src/frontend/hooks/useMapAvailability.ts b/src/frontend/hooks/useMapAvailability.ts index 5d33cafb2..68478ae3d 100644 --- a/src/frontend/hooks/useMapAvailability.ts +++ b/src/frontend/hooks/useMapAvailability.ts @@ -4,12 +4,12 @@ import MapboxGL from "@react-native-mapbox-gl/maps"; import api from "../api"; import { normalizeStyleURL } from "../lib/mapbox"; -import config from "../../config.json"; +import env from "../../../env"; /** URL used for map style when no custom map and user is online */ export const onlineStyleURL = normalizeStyleURL( MapboxGL.StyleURL.Outdoors + "?" + Date.now(), - config.mapboxAccessToken + env.MAPBOX_ACCESS_TOKEN ); export type MapAvailability = "unknown" | "available" | "unavailable"; diff --git a/src/frontend/sharedComponents/Map/MapView.js b/src/frontend/sharedComponents/Map/MapView.js index b89818aaa..dc34a524f 100644 --- a/src/frontend/sharedComponents/Map/MapView.js +++ b/src/frontend/sharedComponents/Map/MapView.js @@ -13,9 +13,9 @@ import type { LocationContextType } from "../../context/LocationContext"; import type { ObservationsMap } from "../../context/ObservationsContext"; import { useIsFullyFocused } from "../../hooks/useIsFullyFocused"; import bugsnag from "../../lib/logger"; -import config from "../../../config.json"; import { OfflineMapLayers } from "../OfflineMapLayers"; import { UserLocation } from "./UserLocation"; +import env from "../../../../env"; // This is the default zoom used when the map first loads, and also the zoom // that the map will zoom to if the user clicks the "Locate" button and the @@ -38,7 +38,7 @@ Logger.setLogCallback(log => { ); }); -MapboxGL.setAccessToken(config.mapboxAccessToken); +MapboxGL.setAccessToken(env.MAPBOX_ACCESS_TOKEN); // Forces Mapbox to always be in connected state, rather than reading system // connectivity state MapboxGL.setConnected(true); From 5c95632c05be9d400bf644fa7eea4186650f74f3 Mon Sep 17 00:00:00 2001 From: ErikSin <67773827+ErikSin@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:21:04 -0800 Subject: [PATCH 4/6] chore:added env script to package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 36ec3b4c1..f279b6430 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "android-no-backend-rebuild": "react-native run-android --variant=appDebug --appIdSuffix=debug", "android-storybook": "react-native run-android --variant=storybookDebug --appIdSuffix=storybook.debug", "android-icca": "RN_SRC_EXT=icca react-native run-android --variant=iccaDebug --appIdSuffix=icca.debug", + "build:env": "node ./scripts/build-env.js", "build:backend": "./scripts/build-backend.sh", "build:intl-polyfills": "node ./scripts/build-intl-polyfills.js", "build:translations": "node ./scripts/build-translations.js && npm run build:intl-polyfills", From f93b773d26e448adbf8c39dc450c1b9f7f124352 Mon Sep 17 00:00:00 2001 From: ErikSin <67773827+ErikSin@users.noreply.github.com> Date: Mon, 11 Mar 2024 11:59:23 -0700 Subject: [PATCH 5/6] chore:update test to build env --- .github/workflows/android.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 8b30b1118..a609560c6 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -47,6 +47,9 @@ jobs: - name: Build translations run: | npm run build:translations + - name: Build env files + run: | + npm run build:env - name: Frontend Tests run: | npm test From 8c7ac8cc5193826cf385d379a4ef119b8898ed0d Mon Sep 17 00:00:00 2001 From: ErikSin <67773827+ErikSin@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:10:58 -0700 Subject: [PATCH 6/6] chore:change name of script --- scripts/{generate_env.js => build-env.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename scripts/{generate_env.js => build-env.js} (100%) diff --git a/scripts/generate_env.js b/scripts/build-env.js similarity index 100% rename from scripts/generate_env.js rename to scripts/build-env.js