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: provide support for .env files injects via API #1034

Merged
merged 5 commits into from
Dec 27, 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
14 changes: 14 additions & 0 deletions docs/v7/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,17 @@ The [yaml](https://www.npmjs.com/package/yaml) package.
```js
console.log(YAML.parse('foo: bar').foo)
```


## loadDotenv

Read env files and collects it into environment variables.

```js
const env = loadDotenv(env1, env2)
console.log((await $({ env })`echo $FOO`).stdout)
---
const env = loadDotenv(env1)
$.env = env
console.log((await $`echo $FOO`).stdout)
```
8 changes: 8 additions & 0 deletions src/goods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isStringLiteral,
parseBool,
parseDuration,
readEnvFromFile,
toCamelCase,
} from './util.js'
import {
Expand Down Expand Up @@ -217,3 +218,10 @@ export async function spinner<T>(
}
})
}

/**
*
* Read env files and collects it into environment variables
*/
export const loadDotenv = (...files: string[]): NodeJS.ProcessEnv =>
files.reduce<NodeJS.ProcessEnv>((m, f) => readEnvFromFile(f, m), {})
17 changes: 2 additions & 15 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,8 @@ import { test, describe, before, after } from 'node:test'
import { fileURLToPath } from 'node:url'
import net from 'node:net'
import getPort from 'get-port'
import {
argv,
importPath,
injectGlobalRequire,
isMain,
main,
normalizeExt,
runScript,
printUsage,
scriptFromStdin,
scriptFromHttp,
transformMarkdown,
writeAndImport,
} from '../build/cli.js'
import { $, path, fs, tmpfile, tmpdir } from '../build/index.js'
import { $, path, tmpfile, tmpdir, fs } from '../build/index.js'
import { isMain, normalizeExt, transformMarkdown } from '../build/cli.js'

const __filename = fileURLToPath(import.meta.url)
const spawn = $.spawn
Expand Down
12 changes: 4 additions & 8 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,20 @@ import {
resolveDefaults,
cd,
syncProcessCwd,
log,
kill,
defaults,
within,
usePowerShell,
usePwsh,
useBash,
} from '../build/core.js'
import {
tempfile,
fs,
nothrow,
quiet,
quote,
quotePowerShell,
sleep,
tempfile,
tempdir,
quiet,
which,
} from '../build/index.js'
import { quote, quotePowerShell } from '../build/util.js'

describe('core', () => {
describe('resolveDefaults()', () => {
Expand Down
1 change: 1 addition & 0 deletions test/export.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ describe('index', () => {
assert.equal(typeof index.globby.isGitIgnored, 'function', 'index.globby.isGitIgnored')
assert.equal(typeof index.globby.isGitIgnoredSync, 'function', 'index.globby.isGitIgnoredSync')
assert.equal(typeof index.kill, 'function', 'index.kill')
assert.equal(typeof index.loadDotenv, 'function', 'index.loadDotenv')
assert.equal(typeof index.log, 'function', 'index.log')
assert.equal(typeof index.minimist, 'function', 'index.minimist')
assert.equal(typeof index.nothrow, 'function', 'index.nothrow')
Expand Down
47 changes: 44 additions & 3 deletions test/goods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// limitations under the License.

import assert from 'node:assert'
import { test, describe } from 'node:test'
import { $, chalk } from '../build/index.js'
import { echo, sleep, parseArgv } from '../build/goods.js'
import { test, describe, after } from 'node:test'
import { $, chalk, fs, tempfile } from '../build/index.js'
import { echo, sleep, parseArgv, loadDotenv } from '../build/goods.js'

describe('goods', () => {
function zx(script) {
Expand Down Expand Up @@ -173,4 +173,45 @@ describe('goods', () => {
}
)
})

describe('loadDotenv()', () => {
const env1 = tempfile(
'.env',
`FOO=BAR
BAR=FOO+`
)
const env2 = tempfile('.env.default', `BAR2=FOO2`)

after(() => {
fs.remove(env1)
fs.remove(env2)
})

test('handles multiple dotenv files', async () => {
const env = loadDotenv(env1, env2)

assert.equal((await $({ env })`echo $FOO`).stdout, 'BAR\n')
assert.equal((await $({ env })`echo $BAR`).stdout, 'FOO+\n')
assert.equal((await $({ env })`echo $BAR2`).stdout, 'FOO2\n')
})

test('handles replace evn', async () => {
const env = loadDotenv(env1)
$.env = env
assert.equal((await $`echo $FOO`).stdout, 'BAR\n')
assert.equal((await $`echo $BAR`).stdout, 'FOO+\n')
$.env = process.env
})

test('handle error', async () => {
try {
loadDotenv('./.env')

assert.throw()
} catch (e) {
assert.equal(e.code, 'ENOENT')
assert.equal(e.errno, -2)
}
})
})
})
8 changes: 5 additions & 3 deletions test/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

import assert from 'node:assert'
import fs from 'node:fs'
import { test, describe } from 'node:test'
import { test, describe, after } from 'node:test'
import { fs as fsCore } from '../build/index.js'
import {
formatCmd,
isString,
Expand Down Expand Up @@ -164,16 +165,17 @@ e.g. a private SSH key
})

describe('readEnvFromFile()', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
after(() => fsCore.remove(file))

test('handles correct proccess.env', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
const env = readEnvFromFile(file)
assert.equal(env.ENV, 'value1')
assert.equal(env.ENV2, 'value24')
assert.ok(env.NODE_VERSION !== '')
})

test('handles correct some env', () => {
const file = tempfile('.env', 'ENV=value1\nENV2=value24')
const env = readEnvFromFile(file, { version: '1.0.0', name: 'zx' })
assert.equal(env.ENV, 'value1')
assert.equal(env.ENV2, 'value24')
Expand Down
Loading