Skip to content

Commit

Permalink
add env var template in yarn to fix build with site url
Browse files Browse the repository at this point in the history
  • Loading branch information
nemanjam committed Jan 2, 2024
1 parent 4bf502d commit ac5187f
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy-gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
3 changes: 2 additions & 1 deletion apps/nemanjamiticcom/.env.development.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PUBLIC_SITE_HOSTNAME=localhost:3000
# must have https:// or http://
PUBLIC_SITE_HOSTNAME=http://localhost:3000
2 changes: 1 addition & 1 deletion apps/nemanjamiticcom/.env.production.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# nemanjamitic.com | nemanjam.github.io
# https://nemanjamitic.com | https://nemanjam.github.io, must have https:// or http://
PUBLIC_SITE_HOSTNAME=
2 changes: 1 addition & 1 deletion apps/nemanjamiticcom/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
4 changes: 4 additions & 0 deletions apps/nemanjamiticcom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion apps/nemanjamiticcom/src/config.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
15 changes: 13 additions & 2 deletions apps/nemanjamiticcom/src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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';

Expand Down
2 changes: 2 additions & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"]
}
54 changes: 53 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -2332,6 +2347,11 @@ [email protected]:
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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit ac5187f

Please sign in to comment.