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

feat: Custom dev ui port(load env from .env) #2702

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/neuron-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"watch": "tsc -w"
},
"devDependencies": {
"typescript": "5.1.3"
"dotenv-expand": "10.0.0"
}
}
4 changes: 2 additions & 2 deletions packages/neuron-shared/src/load-env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs'
FrankFang marked this conversation as resolved.
Show resolved Hide resolved
import dotenv from 'dotenv'
import dotenvExpand from 'dotenv-expand'

const dotenvPath = '.env'

Expand All @@ -17,8 +18,7 @@ export const loadEnv = () => {
for (let i = 0; i < dotenvFiles.length; i++) {
const dotenvFile = dotenvFiles[i]
if (fs.existsSync(dotenvFile)) {
dotenv.config({ path: dotenvFile })
break
dotenvExpand.expand(dotenv.config({ path: dotenvFile, override: false }))
Copy link
Author

@FrankFang FrankFang Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotenvExpand makes ENV_2=${ENV_1} possible.

}
}
}
77 changes: 61 additions & 16 deletions packages/neuron-shared/tests/load-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { loadEnv } from "../src/load-env"

jest.mock('fs')
Keith-CY marked this conversation as resolved.
Show resolved Hide resolved

const contentMap: Record<string, string> = {
'.env': 'TEST_VAR="from .env"',
'.env.local': 'TEST_VAR="from .env.local"',
'.env.test': 'TEST_VAR="from .env.test"',
'.env.test.local': 'TEST_VAR="from .env.test.local"',
'.env.development': 'TEST_VAR="from .env.development"',
'.env.development.local': 'TEST_VAR="from .env.development.local"',
'.env.production': 'TEST_VAR="from .env.production"',
'.env.production.local': 'TEST_VAR="from .env.production.local"',
}

describe("Load Env", () => {
const originalEnv = process.env;
const contentMap: Record<string, string> = {
'.env': 'TEST_VAR="from .env"',
'.env.local': 'TEST_VAR="from .env.local"',
'.env.test': 'TEST_VAR="from .env.test"',
'.env.test.local': 'TEST_VAR="from .env.test.local"',
'.env.development': 'TEST_VAR="from .env.development"',
'.env.development.local': 'TEST_VAR="from .env.development.local"',
'.env.production': 'TEST_VAR="from .env.production"',
'.env.production.local': 'TEST_VAR="from .env.production.local"',
}

beforeAll(() => {
fs.readFileSync = jest.fn().mockImplementation(
Expand All @@ -26,13 +26,13 @@ describe("Load Env", () => {
afterAll(() => {
jest.restoreAllMocks()
})
afterEach(() => {
process.env = originalEnv;
})
describe("in test environment", () => {
beforeEach(() => {
process.env = { ...originalEnv };
process.env.NODE_ENV = 'test'
delete process.env.TEST_VAR
})
afterEach(() => {
process.env = originalEnv;
})
it(".env", () => {
fs.existsSync = jest.fn().mockImplementation((filepath) => {
Expand Down Expand Up @@ -72,8 +72,11 @@ describe("Load Env", () => {
})
describe("in development environment", () => {
beforeEach(() => {
process.env = { ...originalEnv }
process.env.NODE_ENV = 'development'
delete process.env.TEST_VAR
})
afterEach(() => {
process.env = originalEnv;
})
it(".env", () => {
fs.existsSync = jest.fn().mockImplementation((filepath) => {
Expand Down Expand Up @@ -114,8 +117,11 @@ describe("Load Env", () => {

describe("in production environment", () => {
beforeEach(() => {
process.env = { ...originalEnv }
process.env.NODE_ENV = 'production'
delete process.env.TEST_VAR
})
afterEach(() => {
process.env = originalEnv;
})
it(".env", () => {
fs.existsSync = jest.fn().mockImplementation((filepath) => {
Expand Down Expand Up @@ -154,3 +160,42 @@ describe("Load Env", () => {
})
})
})


describe("Merge Env Vars But No Override for process.env", () => {
const originalEnv = process.env;
afterAll(() => {
jest.restoreAllMocks()
})
beforeEach(() => {
process.env = { ...originalEnv }
process.env.NODE_ENV = 'development'
})
afterEach(() => {
process.env = originalEnv;
})
it("process.env > .env*", () => {
const contentMap: Record<string, string> = {
'.env': 'VAR_1=.env\nVAR_2=.env\nVAR_3=.env\nVAR_4=.env',
'.env.development': 'VAR_1=.env.development\nVAR_2=.env.development\nVAR_3=.env.development',
'.env.local': 'VAR1=.env.local\nVAR_2=.env.local',
'.env.development.local': 'VAR_1=.env.development.local',
}
fs.existsSync = jest.fn().mockImplementation((filepath) => {
return ['.env.development.local', '.env.local', '.env.development', '.env'].includes(filepath)
})
fs.readFileSync = jest.fn().mockImplementation(
(filepath: string) => contentMap[filepath] || ''
)
Object.assign(process.env, {
VAR_5: 'process.env',
})
loadEnv()

expect(process.env.VAR_1).toEqual('.env.development.local')
expect(process.env.VAR_2).toEqual('.env.local')
expect(process.env.VAR_3).toEqual('.env.development')
expect(process.env.VAR_4).toEqual('.env')
expect(process.env.VAR_5).toEqual('process.env')
})
})
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10174,6 +10174,11 @@ dotenv-defaults@^1.0.2:
dependencies:
dotenv "^6.2.0"

[email protected]:
version "10.0.0"
resolved "https://registry.npmmirror.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37"
integrity sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==

dotenv-expand@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0"
Expand Down Expand Up @@ -22174,11 +22179,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.4.tgz#b217fd20119bd61a94d4011274e0ab369058da3b"
integrity sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==

[email protected]:
version "5.1.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.3.tgz#8d84219244a6b40b6fb2b33cc1c062f715b9e826"
integrity sha512-XH627E9vkeqhlZFQuL+UsyAXEnibT0kWR2FWONlr4sTjvxyJYnyefgrkyECLzM5NenmKzRAy2rR/OlYLA1HkZw==

"typescript@^3 || ^4":
version "4.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a"
Expand Down