Skip to content

Commit

Permalink
feat(cli): load .env files
Browse files Browse the repository at this point in the history
Valid env files include:

  .env.tusken
  .env.${NODE_ENV}
  .env

The order they are listed is the precedence.
  • Loading branch information
aleclarson committed Sep 12, 2022
1 parent b87487d commit b573f45
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/tusken-cli/package.json
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@tusken/schema": "1.0.0-alpha.5",
"chokidar": "^3.5.3",
"dotenv": "^16.0.2",
"jiti": "^1.14.0"
},
"peerDependencies": {
Expand Down
10 changes: 6 additions & 4 deletions packages/tusken-cli/src/config.ts
Expand Up @@ -3,16 +3,16 @@ import type { ClientConfig } from 'pg'
import type { Config } from 'tusken/config'
import escalade from './escalade/sync'

const defaultConnection = {
const getDefaultConnection = () => ({
host: process.env.PGHOST || 'localhost',
port: +(process.env.PGPORT || 5432),
user: process.env.PGUSER,
password: process.env.PGPASSWORD,
database: process.env.PGDATABASE || 'main',
}
})

export type LoadedConfig = Config & {
connection: ClientConfig & typeof defaultConnection
connection: ClientConfig & ReturnType<typeof getDefaultConnection>
schemaDir: string
dataDir: string
tuskenId: string
Expand All @@ -29,6 +29,8 @@ export function loadConfig(configPath?: string, database?: string) {
}
})

const defaultConnection = getDefaultConnection()

if (configPath) {
const jiti = require('jiti') as typeof import('jiti').default
const load = jiti(__filename, { interopDefault: true, esmResolve: true })
Expand All @@ -44,7 +46,7 @@ export function loadConfig(configPath?: string, database?: string) {
}

config ||= {
connection: { ...defaultConnection },
connection: defaultConnection,
}

assertType<LoadedConfig>(config)
Expand Down
21 changes: 21 additions & 0 deletions packages/tusken-cli/src/dotenv.ts
@@ -0,0 +1,21 @@
import { config as loadDotEnv } from 'dotenv'
import escalade from './escalade/sync'

export function dotenv() {
const envFiles = [
'.env.tusken',
'.env.' + (process.env.NODE_ENV || 'development'),
'.env',
]

const envPath = escalade(process.cwd(), (dir, files) => {
const file = envFiles.find(f => files.includes(f))
if (file) {
return file
}
})

loadDotEnv({
path: envPath,
})
}
4 changes: 3 additions & 1 deletion packages/tusken-cli/src/index.ts
Expand Up @@ -11,7 +11,8 @@ import { Client } from 'pg'
import { loadConfig } from './config'
import { debounce } from './debounce'
import { defer, Deferred } from './defer'
import { firstLine } from './firstline/index'
import { dotenv } from './dotenv'
import { firstLine } from './firstline'

export default async function () {
const tusken = cac('tusken')
Expand Down Expand Up @@ -239,6 +240,7 @@ export default async function () {

tusken.help()
try {
dotenv()
tusken.parse(process.argv, { run: false })
await tusken.runMatchedCommand()
} catch (e: any) {
Expand Down
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit b573f45

Please sign in to comment.