Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration management using .env files #40

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ export const create = async ({ projectName, projectPath }) => {
encoding: "utf-8",
});
const packageJson = JSON.parse(packageJsonFile);

packageJson.name = projectName;
packageJson.scripts = mapObject(packageJson.scripts, ([key, value]) => [
key,
Expand All @@ -136,7 +135,6 @@ export const create = async ({ projectName, projectPath }) => {
.replaceAll("PM_NAME", pmName)
.replaceAll("PM_LOCK_FILE", pmLockFile),
]);

await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + EOL);
await normalizeLineEndings(packageJsonPath);

Expand All @@ -145,6 +143,11 @@ export const create = async ({ projectName, projectPath }) => {
await writeFile(readmePath, readme);
await normalizeLineEndings(readmePath);

const envPath = pathJoin(projectPath, ".env");
const exampleEnvPath = pathJoin(projectPath, "example.env");
await copyFile(exampleEnvPath, envPath);
await normalizeLineEndings(envPath);

console.log();
`
Project created!
Expand Down
1 change: 1 addition & 0 deletions template/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const options = {

entryPoints: ["src/**"],
outdir: "dist",
outbase: "src",

platform: "node",
target: ["node20.9"],
Expand Down
5 changes: 5 additions & 0 deletions template/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example of .env file. Copy this file to .env and fill in the values.
# This file is meant to be used for local development only.
NODE_ENV=development
APP_ENV=local
PORT=3000
4 changes: 3 additions & 1 deletion template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@
"dev:build": "node build.mjs --watch",
"dev:types": "tsc --watch --noEmit --preserveWatchOutput --pretty",
"format": "prettier --write .",
"start": "node --enable-source-maps .",
"start": "node --import ./dist/setup.js --enable-source-maps .",
"test": "concurrently --group PM_NAME:test:* && PM_RUN test-node",
"test-node": "node --import ./test/setup.mjs --test --test-reporter spec",
"test:format": "prettier --check .",
"test:lint": "eslint .",
"test:types": "tsc --noEmit --pretty"
},
"dependencies": {},
"devDependencies": {
"@eslint/js": "^8.57.0",
"@types/eslint__js": "^8.42.3",
"@types/node": "^20.14.10",
"concurrently": "^8.2.2",
"dotenv": "^16.4.5",
"esbuild": "^0.23.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
17 changes: 16 additions & 1 deletion template/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ export const getEnv = (key: string, defaultValue?: string): string => {
return value;
};

export const port = Number(getEnv("PORT", "3000"));
export const port = Number(getEnv("PORT"));

type NodeEnv = "development" | "production" | "test";

const getNodeEnv = (): NodeEnv => {
const env = getEnv("NODE_ENV");
if (env !== "development" && env !== "production" && env !== "test") {
throw new Error(`Invalid NODE_ENV: ${env}`);
}
return env;
};

export const nodeEnv = getNodeEnv();

// The deployment environment, e.g. "staging" or "production"
export const appEnv = getEnv("APP_ENV", "development");
5 changes: 5 additions & 0 deletions template/src/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This file is loaded before anything else in the app
// Use this file to load environment variables and other global configuration
import dotenv from "dotenv";

dotenv.config();
6 changes: 6 additions & 0 deletions template/test/setup.mjs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// Common setup for all tests goes here

import dotenv from "dotenv";

dotenv.config();

process.env.NODE_ENV = "test";