From 96e8b22ddbd6fc49bbce691eddc0b2fa9e69d4f6 Mon Sep 17 00:00:00 2001 From: nicolsss Date: Mon, 9 May 2022 16:28:31 -0500 Subject: [PATCH] Removing use of keys.ts to store evironment variables --- .github/workflows/CD-time-tracker-ui.yml | 4 +-- .github/workflows/CI-time-tracker-ui.yml | 11 +++++--- Dockerfile | 4 +-- package.json | 1 - scripts/populate-keys.sh | 12 ++++----- scripts/populate-var-file.sh | 10 ------- scripts/setenv.ts | 33 ------------------------ src/environments/environment.ts | 13 +++++----- webpack.config.js | 20 +++++++++++--- 9 files changed, 41 insertions(+), 67 deletions(-) delete mode 100644 scripts/populate-var-file.sh delete mode 100644 scripts/setenv.ts diff --git a/.github/workflows/CD-time-tracker-ui.yml b/.github/workflows/CD-time-tracker-ui.yml index 5934939b6..db0ed3d19 100644 --- a/.github/workflows/CD-time-tracker-ui.yml +++ b/.github/workflows/CD-time-tracker-ui.yml @@ -35,8 +35,8 @@ jobs: STACK_EXCHANGE_ACCESS_TOKEN: ${{ secrets.stack_exchange_access_token }} AZURE_APP_CONFIGURATION_CONNECTION_STRING: ${{ secrets.azure_app_configuration_connection_string }} run: | - chmod +x ./scripts/populate-var-file.sh - sh ./scripts/populate-var-file.sh + chmod +x ./scripts/populate-keys.sh + sh ./scripts/populate-keys.sh - name: 'run: npm install and build' run: | diff --git a/.github/workflows/CI-time-tracker-ui.yml b/.github/workflows/CI-time-tracker-ui.yml index f63625e00..bfa81fde6 100644 --- a/.github/workflows/CI-time-tracker-ui.yml +++ b/.github/workflows/CI-time-tracker-ui.yml @@ -48,11 +48,16 @@ jobs: STACK_EXCHANGE_ACCESS_TOKEN: ${{ secrets.STACK_EXCHANGE_ACCESS_TOKEN }} AZURE_APP_CONFIGURATION_CONNECTION_STRING: ${{ secrets.AZURE_APP_CONFIGURATION_CONNECTION_STRING }} run: | - chmod +x ./scripts/populate-var-file.sh - sh ./scripts/populate-var-file.sh + chmod +x ./scripts/populate-keys.sh + sh ./scripts/populate-keys.sh - name: Running tests - run: npm run ci-test --if-present + run: | + set -a + source .env + set +a + npm run ci-test --if-present + rm .env - name: Generate coverage report env: diff --git a/Dockerfile b/Dockerfile index e1e08bc62..aa7f1e0ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,13 +43,13 @@ USER ${USERNAME} RUN npm cache clean --force && npm install EXPOSE 4200 EXPOSE 9876 -CMD npm run config && ${HOME}/time-tracker-ui/node_modules/.bin/ng serve --host 0.0.0.0 --disableHostCheck +CMD ${HOME}/time-tracker-ui/node_modules/.bin/ng serve --host 0.0.0.0 --disableHostCheck FROM development as build COPY .env . -RUN npm run config && npm run build +RUN npm run build diff --git a/package.json b/package.json index c0661d64e..e40592e44 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "time-tracker", "version": "1.72.5", "scripts": { - "config": "ts-node ./scripts/setenv.ts", "preinstall": "npx npm-force-resolutions", "ng": "ng", "start": "ng serve", diff --git a/scripts/populate-keys.sh b/scripts/populate-keys.sh index 6ab4a5cd4..504fa13d7 100644 --- a/scripts/populate-keys.sh +++ b/scripts/populate-keys.sh @@ -1,10 +1,10 @@ #!/bin/bash > .env -echo 'AUTHORITY = '$AUTHORITY'' >> .env -echo 'CLIENT_ID = '$CLIENT_ID'' >> .env -echo 'SCOPES = '$SCOPES'' >> .env -echo 'STACK_EXCHANGE_ID = '$STACK_EXCHANGE_ID'' >> .env -echo 'STACK_EXCHANGE_ACCESS_TOKEN = '$STACK_EXCHANGE_ACCESS_TOKEN'' >> .env -echo 'AZURE_APP_CONFIGURATION_CONNECTION_STRING = '$AZURE_APP_CONFIGURATION_CONNECTION_STRING'' >> .env +echo "AUTHORITY='$AUTHORITY'" >> .env +echo "CLIENT_ID='$CLIENT_ID'" >> .env +echo "SCOPES='$SCOPES'" >> .env +echo "STACK_EXCHANGE_ID='$STACK_EXCHANGE_ID'" >> .env +echo "STACK_EXCHANGE_ACCESS_TOKEN='$STACK_EXCHANGE_ACCESS_TOKEN'" >> .env +echo "AZURE_APP_CONFIGURATION_CONNECTION_STRING='$AZURE_APP_CONFIGURATION_CONNECTION_STRING'" >> .env cat .env diff --git a/scripts/populate-var-file.sh b/scripts/populate-var-file.sh deleted file mode 100644 index f395689af..000000000 --- a/scripts/populate-var-file.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -> src/environments/keys.ts -echo 'export const AUTHORITY = "'$AUTHORITY'";' >> src/environments/keys.ts -echo 'export const CLIENT_ID = "'$CLIENT_ID'";' >> src/environments/keys.ts -echo 'export const SCOPES = ["'$SCOPES'"];' >> src/environments/keys.ts -echo 'export const STACK_EXCHANGE_ID = "'$STACK_EXCHANGE_ID'";' >> src/environments/keys.ts -echo 'export const STACK_EXCHANGE_ACCESS_TOKEN = "'$STACK_EXCHANGE_ACCESS_TOKEN'";' >> src/environments/keys.ts -echo 'export const AZURE_APP_CONFIGURATION_CONNECTION_STRING = "'$AZURE_APP_CONFIGURATION_CONNECTION_STRING'";' >> src/environments/keys.ts -cat src/environments/keys.ts diff --git a/scripts/setenv.ts b/scripts/setenv.ts deleted file mode 100644 index 659a587b2..000000000 --- a/scripts/setenv.ts +++ /dev/null @@ -1,33 +0,0 @@ -const { writeFile } = require('fs'); - -const pathJs = `./src/environments/keys.ts` -const contentKeys = -`export const AUTHORITY = '${process.env["AUTHORITY"]}'; -export const CLIENT_ID = '${process.env["CLIENT_ID"]}'; -export const SCOPES = ['${process.env["SCOPES"]}']; -export const STACK_EXCHANGE_ID = '${process.env["STACK_EXCHANGE_ID"]}'; -export const STACK_EXCHANGE_ACCESS_TOKEN = '${process.env["STACK_EXCHANGE_ACCESS_TOKEN"]}'; -export const AZURE_APP_CONFIGURATION_CONNECTION_STRING = '${process.env["AZURE_APP_CONFIGURATION_CONNECTION_STRING"]}'; -`; - -writeFile(pathJs, contentKeys, function (err) { - if (err) { - console.log(err); - } - console.log(`Wrote variables to ${pathJs}`); -}); - -const pathJson = `./src/environments/.keys.json` -const contentKeysJson = -`{ - "authority": "${process.env.AUTHORITY_JSON}", - "client_id": "${process.env.CLIENT_ID_JSON}", - "scopes": ["${process.env.SCOPES_JSON}"] -}`; - -writeFile(pathJson, contentKeysJson, function (err) { - if (err) { - console.log(err); - } - console.log(`Wrote variables to ${pathJson}`); -}); diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 6846b4ca6..d54957afd 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -1,7 +1,6 @@ // This file can be replaced during build by using the `fileReplacements` array. // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. // The list of file replacements can be found in `angular.json`. -import * as keys from './keys'; export const environment = { production: false, @@ -9,13 +8,13 @@ export const environment = { stackexchangeApiUrl: 'https://api.stackexchange.com', }; -export const AUTHORITY = keys.AUTHORITY; -export const CLIENT_ID = keys.CLIENT_ID; -export const SCOPES = keys.SCOPES; +export const AUTHORITY = process.env["AUTHORITY"]; +export const CLIENT_ID = process.env["CLIENT_ID"]; +export const SCOPES = process.env["SCOPES"].split(","); export const ITEMS_PER_PAGE = 5; -export const STACK_EXCHANGE_ID = keys.STACK_EXCHANGE_ID; -export const STACK_EXCHANGE_ACCESS_TOKEN = keys.STACK_EXCHANGE_ACCESS_TOKEN; -export const AZURE_APP_CONFIGURATION_CONNECTION_STRING = keys.AZURE_APP_CONFIGURATION_CONNECTION_STRING; +export const STACK_EXCHANGE_ID = process.env["STACK_EXCHANGE_ID"]; +export const STACK_EXCHANGE_ACCESS_TOKEN = process.env["STACK_EXCHANGE_ACCESS_TOKEN"]; +export const AZURE_APP_CONFIGURATION_CONNECTION_STRING = process.env["AZURE_APP_CONFIGURATION_CONNECTION_STRING"]; export const DATE_FORMAT = 'yyyy-MM-dd'; export const DATE_FORMAT_YEAR = 'YYYY-MM-DD'; export const GROUPS = { diff --git a/webpack.config.js b/webpack.config.js index 1a85a6c20..717582daa 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,11 +1,25 @@ +const webpack = require('webpack') const { addTailwindPlugin } = require("@ngneat/tailwind"); const tailwindConfig = require("./tailwind.config.js"); - module.exports = (config) => { + const config_ = { + ...config, + plugins : [ + ...config.plugins, + new webpack.DefinePlugin({ + 'process.env.AUTHORITY': JSON.stringify(process.env["AUTHORITY"]), + 'process.env.CLIENT_ID':JSON.stringify(process.env["CLIENT_ID"]), + 'process.env.SCOPES':JSON.stringify(process.env["SCOPES"]), + 'process.env.STACK_EXCHANGE_ID':JSON.stringify(process.env["STACK_EXCHANGE_ID"]), + 'process.env.STACK_EXCHANGE_ACCESS_TOKEN':JSON.stringify(process.env["STACK_EXCHANGE_ACCESS_TOKEN"]), + 'process.env.AZURE_APP_CONFIGURATION_CONNECTION_STRING':JSON.stringify(process.env["AZURE_APP_CONFIGURATION_CONNECTION_STRING"]) + }) + ] + } addTailwindPlugin({ - webpackConfig: config, + webpackConfig: config_, tailwindConfig, patchComponentsStyles: true }); - return config; + return config_; };