Skip to content
Closed
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
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

const dotenv = require('./lib/main')
const run = require('./cli')

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Loads environment variables from .env file",
"main": "dist/index.cjs",
"bin": {
"dotenv": "./dist/index.cjs"
"dotenv": "./dist/cli.cjs"
},
"types": "dist/index.d.ts",
"exports": {
Expand Down
7 changes: 4 additions & 3 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ const dist = path.join(root, 'dist')
fs.rmSync(dist, { recursive: true, force: true })

esbuild.buildSync({
entryPoints: [path.join(root, 'index.js')],
entryPoints: [path.join(root, 'index.js'), path.join(root, 'cli.js')],
bundle: true,
platform: 'node',
format: 'cjs',
target: 'node12',
outfile: path.join(dist, 'index.cjs'),
outdir: dist,
outExtension: { '.js': '.cjs' },
legalComments: 'none',
minify: true
})
Expand All @@ -22,4 +23,4 @@ fs.copyFileSync(path.join(root, 'lib/main.d.ts'), path.join(dist, 'index.d.ts'))
const config = fs.readFileSync(path.join(root, 'config.js'), 'utf8')
fs.writeFileSync(path.join(dist, 'config.cjs'), config.replace("require('./lib/main')", "require('./index.cjs')"))
fs.copyFileSync(path.join(root, 'config.d.ts'), path.join(dist, 'config.d.ts'))
fs.chmodSync(path.join(dist, 'index.cjs'), 0o755)
fs.chmodSync(path.join(dist, 'cli.cjs'), 0o755)
6 changes: 4 additions & 2 deletions tests/test-cli-signals.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const os = require('os')
const path = require('path')
const t = require('tap')

const cli = path.resolve(__dirname, '..', require('../package.json').bin.dotenv)

function kill (pid, signal = 'SIGKILL') {
try {
process.kill(pid, signal)
Expand All @@ -15,7 +17,7 @@ function kill (pid, signal = 'SIGKILL') {
async function launch (ct, script, preload) {
const args = preload ? ['--require', preload] : []
const child = cp.spawn(process.execPath, [
...args, 'dist/index.cjs', 'run', '-q', '--', process.execPath, '-e', script
...args, cli, 'run', '-q', '--', process.execPath, '-e', script
], {
cwd: path.resolve(__dirname, '..'),
detached: true,
Expand Down Expand Up @@ -77,7 +79,7 @@ t.test('POSIX signal forwarding', { skip: process.platform === 'win32' }, async

await ct.test('exits when the command dies from a signal Node ignores', async st => {
// Node ignores SIGPIPE, so re-raising it does not stop dotenv (`dotenv run -- cmd | head`).
const result = cp.spawnSync(process.execPath, ['dist/index.cjs', 'run', '-q', '--', 'sh', '-c', 'kill -PIPE $$'], {
const result = cp.spawnSync(process.execPath, [cli, 'run', '-q', '--', 'sh', '-c', 'kill -PIPE $$'], {
cwd: path.resolve(__dirname, '..'),
encoding: 'utf8',
timeout: 5000,
Expand Down
8 changes: 5 additions & 3 deletions tests/test-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const cp = require('child_process')
const path = require('path')
const t = require('tap')

const cli = path.resolve(__dirname, '..', require('../package.json').bin.dotenv)

function run (args, env = process.env) {
return cp.spawnSync(process.execPath, ['dist/index.cjs', 'run', ...args], {
return cp.spawnSync(process.execPath, [cli, 'run', ...args], {
cwd: path.resolve(__dirname, '..'),
encoding: 'utf8',
env
Expand Down Expand Up @@ -94,7 +96,7 @@ t.test('empty path environment variables load the default .env like config()', c
delete env.BASIC
for (const name of ['DOTENV_PATH', 'DOTENV_CONFIG_PATH']) {
const result = cp.spawnSync(process.execPath, [
path.resolve(__dirname, '../dist/index.cjs'), 'run', '-q',
cli, 'run', '-q',
process.execPath, '-e', 'console.log(process.env.BASIC)'
], { cwd, encoding: 'utf8', env: { ...env, [name]: '' } })

Expand Down Expand Up @@ -167,7 +169,7 @@ for (const testCase of [
const cwd = ct.testdir({ '.env': testCase.file })
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.toUpperCase().startsWith('DOTENV_')))
const result = cp.spawnSync(process.execPath, [
path.resolve(__dirname, '../dist/index.cjs'), 'run', ...(testCase.flags || []),
cli, 'run', ...(testCase.flags || []),
process.execPath, '-e', 'console.log("child ran")'
], { cwd, encoding: 'utf8', env: { ...env, ...testCase.env } })

Expand Down
12 changes: 12 additions & 0 deletions tests/test-config-import.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
const cp = require('child_process')
const fs = require('fs')
const { createRequire, wrap } = require('module')
const path = require('path')
const vm = require('vm')
const t = require('tap')

const cleanEnv = Object.fromEntries(Object.entries(process.env).filter(([key]) => {
return !key.toUpperCase().startsWith('DOTENV_') && key !== 'BASIC'
}))

t.test('built library can be wrapped by a bundler', ct => {
const filename = require.resolve('dotenv')
const load = vm.runInThisContext(wrap(fs.readFileSync(filename, 'utf8')), { filename })
const entry = { exports: {} }
load(entry.exports, createRequire(filename), entry, filename, path.dirname(filename))
ct.same(entry.exports.parse('HELLO=world'), { HELLO: 'world' })
ct.end()
})

t.test('built package supports named, default, and namespace ESM imports', ct => {
const result = cp.spawnSync(process.execPath, [
'--input-type=module',
Expand Down