From 69141afde67a2a1df8ab2be755cea8be46519363 Mon Sep 17 00:00:00 2001 From: Ben Merckx Date: Tue, 21 Nov 2023 14:25:15 +0100 Subject: [PATCH] Support .env.local, fixes #320 --- dev.js | 5 ++--- package.json | 1 - src/cli/util/EnsureEnv.ts | 16 +++++++++++----- yarn.lock | 3 +-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/dev.js b/dev.js index bf8fb0bb9..459ef3d1f 100644 --- a/dev.js +++ b/dev.js @@ -1,12 +1,11 @@ +import {ensureEnv} from 'alinea/cli/util/EnsureEnv' import {forwardCommand} from 'alinea/cli/util/ForwardCommand' -import dotenv from 'dotenv' -import findConfig from 'find-config' import path from 'node:path' import sade from 'sade' async function run({production, dir, config}) { + ensureEnv(dir) const forceProduction = process.env.ALINEA_CLOUD_URL - dotenv.config({path: findConfig('.env')}) process.env.NODE_ENV = forceProduction || production ? 'production' : 'development' const {serve} = await import('alinea/cli/Serve') diff --git a/package.json b/package.json index 39d470898..42599e2de 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,6 @@ "autoprefixer": "^10.4.2", "copy-env-cli": "1.0.0", "cross-env": "^7.0.3", - "dotenv": "^16.3.1", "esbx": "^0.0.18", "find-config": "^1.0.0", "glob": "^7.2.0", diff --git a/src/cli/util/EnsureEnv.ts b/src/cli/util/EnsureEnv.ts index cdc4c487d..33f1e62e8 100644 --- a/src/cli/util/EnsureEnv.ts +++ b/src/cli/util/EnsureEnv.ts @@ -3,14 +3,20 @@ import path from 'node:path' // Source: stripped down version of dotenv +const attempt = ['.env.local', '.env'] + export function ensureEnv(cwd = process.cwd()) { - const dotEnvFile = path.join(cwd, '.env') const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/gm - let src: string - try { - src = fs.readFileSync(dotEnvFile, 'utf-8') - } catch (err) { + let src: string | undefined = undefined + for (const file of attempt) { + try { + const dotEnvFile = path.resolve(cwd, file) + src = fs.readFileSync(dotEnvFile, 'utf-8') + break + } catch {} + } + if (src === undefined) { const parent = path.dirname(cwd) if (parent === cwd) return return ensureEnv(path.dirname(cwd)) diff --git a/yarn.lock b/yarn.lock index 381dac29e..481f31305 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2714,7 +2714,6 @@ __metadata: autoprefixer: ^10.4.2 copy-env-cli: 1.0.0 cross-env: ^7.0.3 - dotenv: ^16.3.1 esbuild: ^0.19.2 esbx: ^0.0.18 find-config: ^1.0.0 @@ -3925,7 +3924,7 @@ __metadata: languageName: node linkType: hard -"dotenv@npm:^16.0.3, dotenv@npm:^16.3.1": +"dotenv@npm:^16.0.3": version: 16.3.1 resolution: "dotenv@npm:16.3.1" checksum: 15d75e7279018f4bafd0ee9706593dd14455ddb71b3bcba9c52574460b7ccaf67d5cf8b2c08a5af1a9da6db36c956a04a1192b101ee102a3e0cf8817bbcf3dfd