Skip to content

Commit

Permalink
create .env generation script
Browse files Browse the repository at this point in the history
  • Loading branch information
AmruthPillai committed Dec 16, 2022
1 parent 2ba6658 commit f9b6aef
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ POSTGRES_PASSWORD=postgres

# Server
SECRET_KEY=
POSTGRES_HOST=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_SSL_CERT=
JWT_SECRET=
Expand Down
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// TypeScript ESLint
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
Expand Down
12 changes: 3 additions & 9 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ tasks:
- name: Run PostgreSQL Database
command: docker run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=postgres -d postgres

- name: Restore Environment Variables
- name: Generate Environment Variables
command: |
if [ -f .env ]; then
echo "Found .env in workspace"
echo "Found .env in workspace, skipping generation"
else
if [ -z "${DOTENV}" ]; then
echo "Setting example .env"
cp .env.gitpod .env
else
echo "Restoring .env from Gitpod"
echo "${DOTENV}" | base64 -d > .env
fi
pnpm generate-env
fi
- name: Install Project Dependencies
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"private": true,
"scripts": {
"dev": "env-cmd --silent cross-var cross-env VERSION=$npm_package_version turbo run dev",
"lint": "turbo run lint",
"build": "env-cmd --silent cross-var cross-env VERSION=$npm_package_version turbo run build",
"start": "env-cmd --silent cross-var cross-env VERSION=$npm_package_version turbo run start",
"format": "prettier --write ."
"generate-env": "ts-node ./scripts/generate-env.ts",
"format": "prettier --write .",
"lint": "turbo run lint"
},
"workspaces": [
"schema",
Expand All @@ -18,15 +19,18 @@
"cross-env": "^7.0.3",
"cross-var": "^1.1.0",
"env-cmd": "^10.1.0",
"turbo": "^1.6.3"
"turbo": "^1.6.3",
"uuid": "^9.0.0"
},
"devDependencies": {
"@types/node": "^18.11.15",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"eslint": "^8.29.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-simple-import-sort": "^8.0.0",
"prettier": "^2.8.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"resolutions": {
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion scripts/generate-env.ts
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
console.log('Hello, World!');
import { randomBytes } from 'crypto';
import fs from 'fs/promises';

const envMap = {
TZ: 'UTC',
PUBLIC_URL: '',
PUBLIC_SERVER_URL: '',
PUBLIC_GOOGLE_CLIENT_ID: '',
POSTGRES_DB: 'postgres',
POSTGRES_USER: 'postgres',
POSTGRES_PASSWORD: 'postgres',
SECRET_KEY: '',
POSTGRES_HOST: 'localhost',
POSTGRES_PORT: '5432',
POSTGRES_SSL_CERT: '',
JWT_SECRET: '',
JWT_EXPIRY_TIME: '604800',
GOOGLE_CLIENT_SECRET: '',
GOOGLE_API_KEY: '',
MAIL_FROM_NAME: '',
MAIL_FROM_EMAIL: '',
MAIL_HOST: '',
MAIL_PORT: '',
MAIL_USERNAME: '',
MAIL_PASSWORD: '',
STORAGE_BUCKET: '',
STORAGE_REGION: '',
STORAGE_ENDPOINT: '',
STORAGE_URL_PREFIX: '',
STORAGE_ACCESS_KEY: '',
STORAGE_SECRET_KEY: '',
PDF_DELETION_TIME: '345600000',
PUBLIC_FLAG_DISABLE_SIGNUPS: 'false',
};

const main = async () => {
// URLs
// If running in a Gitpod environment, auto generated the URLs
if (process.env.GITPOD_WORKSPACE_URL) {
const baseUrl = new URL(process.env.GITPOD_WORKSPACE_URL!);

envMap['PUBLIC_SERVER_URL'] = `https://3100-${baseUrl}`;
envMap['PUBLIC_URL'] = `https://3000-${baseUrl}`;
}
// Otherwise, fallback to localhost
else {
envMap['PUBLIC_SERVER_URL'] = 'https://localhost:3100';
envMap['PUBLIC_URL'] = 'https://localhost:3000';
}

// Secret Key
envMap['SECRET_KEY'] = randomBytes(20).toString('hex');
envMap['JWT_SECRET'] = randomBytes(40).toString('hex');

const envFile = Object.entries(envMap)
.reduce((acc, [key, value]) => {
acc.push(`${key}=${value}`);

return acc;
}, [] as string[])
.join('\n');

await fs.writeFile('.env', envFile);
};

main();

0 comments on commit f9b6aef

Please sign in to comment.