Repository: echobind/bisonapp
Pull request #313 (2023-04-07 23:21:41)

chore: Simplify env files.
## Changes

- fix: A few `copyFile` entries mistakenly target the wrong files.
- chore: Remove `.env.local`. We shouldn't ever need `.env.local` because it will be applied at every stage (not usually intended).
- chore: Don't override values in `env.*.local` unless needed.
- chore: Don't set any values in `.env`. Use it as basically a template. The value in `.env` will be applied to every environment, which doesn't usually make sense.

## Checklist

- [ ] Requires dependency update?
- [x] Generating a new app works



Patch:
diff --git a/packages/create-bison-app/tasks/copyFiles.js b/packages/create-bison-app/tasks/copyFiles.js
index bd62785..663a64c 100644
--- a/packages/create-bison-app/tasks/copyFiles.js
+++ b/packages/create-bison-app/tasks/copyFiles.js
@@ -41,15 +41,9 @@ async function copyFiles({ variables, targetFolder }) {
 
     copyWithTemplate(fromPath("_.env.ejs"), toPath(".env"), variables),
 
-    copyWithTemplate(
-      fromPath("_.env.local.ejs"),
-      toPath(".env.local"),
-      variables
-    ),
-
     copyWithTemplate(
       fromPath("_.env.development.ejs"),
-      toPath(".env.local"),
+      toPath(".env.development"),
       variables
     ),
 
@@ -67,7 +61,7 @@ async function copyFiles({ variables, targetFolder }) {
 
     copyWithTemplate(
       fromPath("_.env.test.local.ejs"),
-      toPath(".env.test"),
+      toPath(".env.test.local"),
       variables
     ),
 
diff --git a/packages/create-bison-app/template/_.env.development.ejs b/packages/create-bison-app/template/_.env.development.ejs
index f1b25eb..f954572 100644
--- a/packages/create-bison-app/template/_.env.development.ejs
+++ b/packages/create-bison-app/template/_.env.development.ejs
@@ -1,12 +1,4 @@
-# Copy with real values to .env.development.local
-
-# Environment variables declared in this file are automatically made available to Prisma.
-# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
-
-# Prisma supports the native connection string format for PostgreSQL, MySQL and SQLite.
-# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
-
-# Include NODE_ENV for package script withEnv to work as expected
+# Override values in .env.development.local
 
 NEXT_PUBLIC_APP_ENV="development"
 NODE_ENV="development"
diff --git a/packages/create-bison-app/template/_.env.development.local.ejs b/packages/create-bison-app/template/_.env.development.local.ejs
index 303572b..e69de29 100644
--- a/packages/create-bison-app/template/_.env.development.local.ejs
+++ b/packages/create-bison-app/template/_.env.development.local.ejs
@@ -1,19 +0,0 @@
-# Include your development ENVs here
-
-# Environment variables declared in this file are automatically made available to Prisma.
-# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
-
-# Prisma supports the native connection string format for PostgreSQL, MySQL and SQLite.
-# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
-
-# Include NODE_ENV for package script withEnv to work as expected
-
-NEXT_PUBLIC_APP_ENV="development"
-NODE_ENV="development"
-PORT=3000
-SHOULD_MIGRATE=0
-
-DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.dev.name %>?schema=public"
-
-NEXTAUTH_SECRET="bisonDev"
-NEXTAUTH_URL="http://localhost:3000"
diff --git a/packages/create-bison-app/template/_.env.ejs b/packages/create-bison-app/template/_.env.ejs
index 6100598..b68e73c 100644
--- a/packages/create-bison-app/template/_.env.ejs
+++ b/packages/create-bison-app/template/_.env.ejs
@@ -1,19 +1,9 @@
-# Copy to .env.local to mimic production
+NEXT_PUBLIC_APP_ENV=
+NODE_ENV=
+PORT=
 
-# Environment variables declared in this file are automatically made available to Prisma.
-# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables
+# Set to 1 to run a database migration when building the app (likely only in
+# production)
+SHOULD_MIGRATE=
 
-# Prisma supports the native connection string format for PostgreSQL, MySQL and SQLite.
-# See the documentation for all the connection string options: https://pris.ly/d/connection-strings
-
-# Include NODE_ENV for package script withEnv to work as expected
-
-NEXT_PUBLIC_APP_ENV="production"
-NODE_ENV="development" # production // change if you truly want PROD
-PORT=3000
-SHOULD_MIGRATE=0
-
-DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.dev.name %>?schema=public"
-
-NEXTAUTH_SECRET="bisonProd"
-NEXTAUTH_URL="http://localhost:3000"
+DATABASE_URL=
\ No newline at end of file
diff --git a/packages/create-bison-app/template/_.env.local.ejs b/packages/create-bison-app/template/_.env.local.ejs
deleted file mode 100644
index 66a72ab..0000000
--- a/packages/create-bison-app/template/_.env.local.ejs
+++ /dev/null
@@ -1,12 +0,0 @@
-# Overwrite envs here to mimic production
-# Include NODE_ENV for package script withEnv to work as expected
-
-NEXT_PUBLIC_APP_ENV="production"
-NODE_ENV="development" # production // change if you truly want PROD
-PORT=3000
-SHOULD_MIGRATE=0
-
-DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.dev.name %>?schema=public"
-
-NEXTAUTH_SECRET="bisonProd"
-NEXTAUTH_URL="http://localhost:3000"
diff --git a/packages/create-bison-app/template/_.gitignore b/packages/create-bison-app/template/_.gitignore
index 4a8fe4b..d468979 100644
--- a/packages/create-bison-app/template/_.gitignore
+++ b/packages/create-bison-app/template/_.gitignore
@@ -27,10 +27,7 @@ yarn-debug.log*
 yarn-error.log*
 
 # local env files
-.env.local
-.env.development.local
-.env.test.local
-.env.production.local
+.env*.local
 
 .vercel
 
diff --git a/packages/create-bison-app/template/package.json.ejs b/packages/create-bison-app/template/package.json.ejs
index 930df24..018816e 100644
--- a/packages/create-bison-app/template/package.json.ejs
+++ b/packages/create-bison-app/template/package.json.ejs
@@ -9,12 +9,12 @@
     "build": "yarn ts-node-wrap ./scripts/buildProd",
     "build:prisma": "prisma generate",
     "build:next": "next build",
-    "db:migrate": "prisma migrate dev",
+    "db:migrate": "yarn withEnv:dev prisma migrate dev",
     "db:migrate:prod": "prisma migrate deploy",
     "db:deploy": "prisma migrate deploy",
-    "db:reset": "prisma migrate reset",
+    "db:reset": "yarn withEnv:dev prisma migrate reset",
     "db:reset:test": "yarn withEnv:test prisma migrate reset",
-    "db:seed": "prisma db seed",
+    "db:seed": "yarn withEnv:dev prisma db seed",
     "db:seed:prod": "cross-env NODE_ENV=production prisma db seed",
     "db:setup": "yarn db:reset",
     "dev": "next dev",
@@ -44,6 +44,7 @@
     "test:e2e": "yarn withEnv:test playwright test --workers 1",
     "test:e2e:debug": "PWDEBUG=1 yarn test:e2e",
     "ts-node-wrap": "ts-node --project tsconfig.cjs.json -r tsconfig-paths/register",
+    "withEnv:dev": "dotenv -c development --",
     "withEnv:test": "dotenv -c test --",
     "watch:ts": "yarn dev:typecheck --watch"
   },


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 4ccd8b3..0b070d1 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -86,7 +86,8 @@ jobs:
       - name: Build production bison app
         run: yarn build
         env:
-          DATABASE_URL: not_used_but_needs_to_be_set_to_pass_zod_config_check
+          DATABASE_URL: postgresql://postgres:postgres@localhost/${{ needs.create-app.outputs.appName }}_test
+          NODE_ENV: test
 
       - name: Lint bison app
         run: yarn lint
diff --git a/packages/create-bison-app/template/_.env.test.ejs b/packages/create-bison-app/template/_.env.test.ejs
index e360efd..a378a27 100644
--- a/packages/create-bison-app/template/_.env.test.ejs
+++ b/packages/create-bison-app/template/_.env.test.ejs
@@ -1,5 +1,4 @@
-# Copy to .env.test.local to override
-# Include NODE_ENV for package script withEnv to work as expected
+# Override values in .env.test.local
 
 NEXT_PUBLIC_APP_ENV="test"
 NODE_ENV="test"
diff --git a/packages/create-bison-app/template/_.env.test.local.ejs b/packages/create-bison-app/template/_.env.test.local.ejs
index 6656d11..e69de29 100644
--- a/packages/create-bison-app/template/_.env.test.local.ejs
+++ b/packages/create-bison-app/template/_.env.test.local.ejs
@@ -1,12 +0,0 @@
-# ENV vars here override .env.test when running locally
-# Include NODE_ENV for package script withEnv to work as expected
-
-NEXT_PUBLIC_APP_ENV="test"
-NODE_ENV="test"
-PORT=3001
-SHOULD_MIGRATE=0
-
-DATABASE_URL="postgresql://<%= db.dev.user %><% if (db.dev.password) { %>:<%= db.dev.password %><% } %>@<%= db.dev.host %>:<%= db.dev.port %>/<%= db.test.name %>?schema=public"
-
-NEXTAUTH_SECRET="bisonTest"
-NEXTAUTH_URL="http://localhost:3001"
diff --git a/packages/create-bison-app/test/tasks/copyFiles.test.js b/packages/create-bison-app/test/tasks/copyFiles.test.js
index 1a91a8e..4372615 100644
--- a/packages/create-bison-app/test/tasks/copyFiles.test.js
+++ b/packages/create-bison-app/test/tasks/copyFiles.test.js
@@ -53,8 +53,8 @@ describe("copyFiles", () => {
       expect(() => fs.statSync(target)).not.toThrowError();
     });
 
-    it("copies and renames .env.local and env.test", async () => {
-      const files = [".env.local", ".env.test"];
+    it("copies and renames .env.development and env.test", async () => {
+      const files = [".env.development", ".env.test"];
 
       files.forEach((file) => {
         const filePath = path.join(targetFolder, file);
@@ -129,7 +129,7 @@ describe("copyFiles", () => {
     });
 
     it("copies env with the correct contents", async () => {
-      const target = path.join(targetFolder, ".env");
+      const target = path.join(targetFolder, ".env.development");
       const file = await fs.promises.readFile(target);
       const fileString = file.toString();
       const { user, password, host, port, name } = variables.db.dev;