From ac5187f74513bd9348a85771178711937e4e1771 Mon Sep 17 00:00:00 2001 From: nemanjam Date: Tue, 2 Jan 2024 10:59:08 +0100 Subject: [PATCH] add env var template in yarn to fix build with site url --- .github/workflows/deploy-gh-pages.yml | 2 +- .vscode/settings.json | 9 ++-- apps/nemanjamiticcom/.env.development.example | 3 +- apps/nemanjamiticcom/.env.production.example | 2 +- apps/nemanjamiticcom/astro.config.mjs | 2 +- apps/nemanjamiticcom/package.json | 4 ++ apps/nemanjamiticcom/src/config.yaml | 3 +- apps/nemanjamiticcom/src/utils/config.ts | 15 +++++- docs/todo.md | 2 + turbo.json | 6 ++- yarn.lock | 54 ++++++++++++++++++- 11 files changed, 86 insertions(+), 16 deletions(-) diff --git a/.github/workflows/deploy-gh-pages.yml b/.github/workflows/deploy-gh-pages.yml index b114dfa..db7fc98 100644 --- a/.github/workflows/deploy-gh-pages.yml +++ b/.github/workflows/deploy-gh-pages.yml @@ -57,7 +57,7 @@ jobs: - name: Install dependencies run: yarn install --frozen-lockfile - - name: Build nemanjamiticcom + - name: Build nemanjam.github.io run: yarn build:nmc deploy: diff --git a/.vscode/settings.json b/.vscode/settings.json index 4c37518..84e4b60 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,13 +7,10 @@ }, "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, - "prettier.configPath": "packages/prettier-config/.prettierrc.js", - "prettier.ignorePath": "packages/prettier-config/.prettierignore", + "prettier.configPath": "./packages/prettier-config/.prettierrc.js", + "prettier.ignorePath": "./packages/prettier-config/.prettierignore", "prettier.documentSelectors": ["**/*.astro"], - "[astro]": { - "editor.defaultFormatter": "esbenp.prettier-vscode" - }, - "[json][jsonc]": { + "[astro][json][jsonc]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "eslint.validate": ["javascript", "javascriptreact", "typescript", "typescriptreact", "astro"], diff --git a/apps/nemanjamiticcom/.env.development.example b/apps/nemanjamiticcom/.env.development.example index a1c77a8..62fb7dd 100644 --- a/apps/nemanjamiticcom/.env.development.example +++ b/apps/nemanjamiticcom/.env.development.example @@ -1 +1,2 @@ -PUBLIC_SITE_HOSTNAME=localhost:3000 \ No newline at end of file +# must have https:// or http:// +PUBLIC_SITE_HOSTNAME=http://localhost:3000 diff --git a/apps/nemanjamiticcom/.env.production.example b/apps/nemanjamiticcom/.env.production.example index 10576b4..87b3f6f 100644 --- a/apps/nemanjamiticcom/.env.production.example +++ b/apps/nemanjamiticcom/.env.production.example @@ -1,2 +1,2 @@ -# nemanjamitic.com | nemanjam.github.io +# https://nemanjamitic.com | https://nemanjam.github.io, must have https:// or http:// PUBLIC_SITE_HOSTNAME= \ No newline at end of file diff --git a/apps/nemanjamiticcom/astro.config.mjs b/apps/nemanjamiticcom/astro.config.mjs index a8efc00..a2d5b1b 100644 --- a/apps/nemanjamiticcom/astro.config.mjs +++ b/apps/nemanjamiticcom/astro.config.mjs @@ -24,7 +24,7 @@ const whenExternalScripts = (items = []) => : []; export default defineConfig({ - site: import.meta.env.PUBLIC_SITE_HOSTNAME, // todo: merge env in yaml + site: SITE.site, // must have https:// or http:// base: SITE.base, trailingSlash: SITE.trailingSlash ? 'always' : 'never', server: { port: 3000 }, diff --git a/apps/nemanjamiticcom/package.json b/apps/nemanjamiticcom/package.json index 799b81c..80b2ff6 100644 --- a/apps/nemanjamiticcom/package.json +++ b/apps/nemanjamiticcom/package.json @@ -21,6 +21,8 @@ "@fontsource-variable/inter": "^5.0.16", "astro": "^4.0.6", "astro-icon": "1.0.1", + "dotenv": "^16.3.1", + "ejs": "^3.1.9", "limax": "4.1.0", "lodash.merge": "^4.6.2", "typescript-esbuild": "^0.3.2", @@ -36,6 +38,8 @@ "@repo/prettier-config": "*", "@repo/typescript-config": "*", "@tailwindcss/typography": "^0.5.10", + "@types/ejs": "^3.1.5", + "@types/js-yaml": "^4.0.9", "@types/lodash.merge": "^4.6.9", "@typescript-eslint/eslint-plugin": "^6.13.2", "@typescript-eslint/parser": "^6.13.2", diff --git a/apps/nemanjamiticcom/src/config.yaml b/apps/nemanjamiticcom/src/config.yaml index 97f086c..235e587 100644 --- a/apps/nemanjamiticcom/src/config.yaml +++ b/apps/nemanjamiticcom/src/config.yaml @@ -1,6 +1,7 @@ site: name: AstroWind - site: 'https://astrowind.vercel.app' + # site: 'https://astrowind.vercel.app' + site: <%= process.env.PUBLIC_SITE_HOSTNAME %> base: '/' trailingSlash: false diff --git a/apps/nemanjamiticcom/src/utils/config.ts b/apps/nemanjamiticcom/src/utils/config.ts index c6b863b..83966a8 100644 --- a/apps/nemanjamiticcom/src/utils/config.ts +++ b/apps/nemanjamiticcom/src/utils/config.ts @@ -1,9 +1,15 @@ import fs from 'fs'; import yaml from 'js-yaml'; import merge from 'lodash.merge'; +import ejs from 'ejs'; +import dotenv from 'dotenv'; import type { MetaData } from '~/types'; +// write zod validation for env vars +const envFileName = `.env.${process.env.NODE_ENV}`; +dotenv.config({ path: envFileName }); + export interface SiteConfig { name: string; site?: string; @@ -66,8 +72,13 @@ export interface AnalyticsConfig { }; }; } +// todo: put in function and write zod validation for env vars +// merge .env vars and yaml +const envContext = { site: { site: process.env.PUBLIC_SITE_HOSTNAME } }; +const configTemplate = fs.readFileSync('src/config.yaml', 'utf8'); +const configString = ejs.render(configTemplate, envContext); -const config = yaml.load(fs.readFileSync('src/config.yaml', 'utf8')) as { +const config = yaml.load(configString) as { site?: SiteConfig; metadata?: MetaDataConfig; i18n?: I18NConfig; @@ -78,7 +89,7 @@ const config = yaml.load(fs.readFileSync('src/config.yaml', 'utf8')) as { analytics?: unknown; }; -// console.log('config', JSON.stringify(config, null, 2)); +console.log('config', JSON.stringify(config, null, 2)); const DEFAULT_SITE_NAME = 'Website'; diff --git a/docs/todo.md b/docs/todo.md index 39c6943..25f8937 100644 --- a/docs/todo.md +++ b/docs/todo.md @@ -109,3 +109,5 @@ https://dev.to/emekaofe/how-to-pass-environment-variables-to-a-yaml-file-in-a-no add ssh keys again for nginx action + +write zod validation for env vars diff --git a/turbo.json b/turbo.json index 2c24bd0..f108f10 100644 --- a/turbo.json +++ b/turbo.json @@ -18,12 +18,14 @@ }, "@apps/nemanjamiticcom#build": { "cache": false, - "outputs": [] + "outputs": [], + "env": ["NODE_ENV", "PUBLIC_SITE_HOSTNAME"] }, "lint": { "dependsOn": ["^lint"] }, "format": {}, "check-types": {} - } + }, + "globalDependencies": ["**/.env.*"] } diff --git a/yarn.lock b/yarn.lock index 430114b..cb3d31c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1138,6 +1138,11 @@ dependencies: "@types/ms" "*" +"@types/ejs@^3.1.5": + version "3.1.5" + resolved "https://registry.yarnpkg.com/@types/ejs/-/ejs-3.1.5.tgz#49d738257cc73bafe45c13cb8ff240683b4d5117" + integrity sha512-nv+GSx77ZtXiJzwKdsASqi+YQ5Z7vwHsTP0JY2SiQgjGckkBRKZnk8nIM+7oUZ1VCtuTz0+By4qVR7fqzp/Dfg== + "@types/estree-jsx@^1.0.0": version "1.0.3" resolved "https://registry.yarnpkg.com/@types/estree-jsx/-/estree-jsx-1.0.3.tgz#f8aa833ec986d82b8271a294a92ed1565bf2c66a" @@ -1162,6 +1167,11 @@ resolved "https://registry.yarnpkg.com/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz#b979ebad3919799c979b17c72621c0bc0a31c6c4" integrity sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA== +"@types/js-yaml@^4.0.9": + version "4.0.9" + resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.9.tgz#cd82382c4f902fed9691a2ed79ec68c5898af4c2" + integrity sha512-k4MGaQl5TGo/iipqb2UDG2UwjXziSWkh0uysQelTlJpX1qGlpUZYm8PnO4DxG1qBomtJUdYJ6qR6xdIah10JLg== + "@types/json-schema@^7.0.12": version "7.0.15" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841" @@ -1664,6 +1674,11 @@ astrojs-compiler-sync@^0.3.0: dependencies: synckit "^0.8.0" +async@^3.2.3: + version "3.2.5" + resolved "https://registry.yarnpkg.com/async/-/async-3.2.5.tgz#ebd52a8fdaf7a2289a24df399f8d8485c8a46b66" + integrity sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg== + asynciterator.prototype@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/asynciterator.prototype/-/asynciterator.prototype-1.0.0.tgz#8c5df0514936cdd133604dfcc9d3fb93f09b2b62" @@ -1886,7 +1901,7 @@ chalk@^2.4.2: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chalk@^4.0.0, chalk@^4.1.2: +chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01" integrity sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA== @@ -2332,6 +2347,11 @@ dotenv@16.0.3: resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.0.3.tgz#115aec42bac5053db3c456db30cc243a5a836a07" integrity sha512-7GO6HghkA5fYG9TYnNxi14/7K9f5occMlp3zXAuSxn7CKCxt9xbNWG7yF8hTCSUchlfWSe3uLmlPfigevRItzQ== +dotenv@^16.3.1: + version "16.3.1" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e" + integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ== + dset@^3.1.2, dset@^3.1.3: version "3.1.3" resolved "https://registry.yarnpkg.com/dset/-/dset-3.1.3.tgz#c194147f159841148e8e34ca41f638556d9542d2" @@ -2342,6 +2362,13 @@ eastasianwidth@^0.2.0: resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb" integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA== +ejs@^3.1.9: + version "3.1.9" + resolved "https://registry.yarnpkg.com/ejs/-/ejs-3.1.9.tgz#03c9e8777fe12686a9effcef22303ca3d8eeb361" + integrity sha512-rC+QVNMJWv+MtPgkt0y+0rVEIdbtxVADApW9JXrUVlzHetgcyczP/E7DJmWJ4fJCZF2cPcBk0laWO9ZHMG3DmQ== + dependencies: + jake "^10.8.5" + electron-to-chromium@^1.4.601: version "1.4.616" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.616.tgz#4bddbc2c76e1e9dbf449ecd5da3d8119826ea4fb" @@ -2868,6 +2895,13 @@ file-entry-cache@^6.0.1: dependencies: flat-cache "^3.0.4" +filelist@^1.0.4: + version "1.0.4" + resolved "https://registry.yarnpkg.com/filelist/-/filelist-1.0.4.tgz#f78978a1e944775ff9e62e744424f215e58352b5" + integrity sha512-w1cEuf3S+DrLCQL7ET6kz+gmlJdbq9J7yXCSjK/OZCPA+qEN1WyF4ZAf0YYJa4/shHJra2t/d/r8SV4Ji+x+8Q== + dependencies: + minimatch "^5.0.1" + fill-range@^7.0.1: version "7.0.1" resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40" @@ -3772,6 +3806,16 @@ jackspeak@^2.3.5: optionalDependencies: "@pkgjs/parseargs" "^0.11.0" +jake@^10.8.5: + version "10.8.7" + resolved "https://registry.yarnpkg.com/jake/-/jake-10.8.7.tgz#63a32821177940c33f356e0ba44ff9d34e1c7d8f" + integrity sha512-ZDi3aP+fG/LchyBzUM804VjddnwfSfsdeYkwt8NcbKRvo4rFkjhs456iLFn3k2ZUWvNe4i48WACDbza8fhq2+w== + dependencies: + async "^3.2.3" + chalk "^4.0.2" + filelist "^1.0.4" + minimatch "^3.1.2" + jiti@^1.19.1: version "1.21.0" resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.21.0.tgz#7c97f8fe045724e136a397f7340475244156105d" @@ -4706,6 +4750,13 @@ minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2: dependencies: brace-expansion "^1.1.7" +minimatch@^5.0.1: + version "5.1.6" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.6.tgz#1cfcb8cf5522ea69952cd2af95ae09477f122a96" + integrity sha512-lKwV/1brpG6mBUFHtb7NUmtABCb2WZZmm2wNiOA5hAb8VdCS4B3dtMWyvcoViccwAW/COERjXLt0zP1zXUN26g== + dependencies: + brace-expansion "^2.0.1" + minimist@^1.2.0: version "1.2.8" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c" @@ -5891,6 +5942,7 @@ stream-parser@~0.3.1: debug "2" "string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0: + name string-width-cjs version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==