Skip to content
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
18 changes: 17 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ const packageJson = require('../package.json')

const version = packageJson.version

// Array of tips to display randomly
const TIPS = [
'encrypt with dotenvx: https://dotenvx.com',
'specify custom .env file path with { path: \'/custom/path/.env\' }',
'enable debug logging with { debug: true }',
'override existing env vars with { override: true }',
'suppress all logs with { quiet: true }',
'write to custom object with { processEnv: myObject }',
'load multiple .env files with { path: [\'.env.local\', \'.env\'] }'
]

// Get a random tip from the tips array
function _getRandomTip () {
return TIPS[Math.floor(Math.random() * TIPS.length)]
}

const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg

// Parse src into an Object
Expand Down Expand Up @@ -274,7 +290,7 @@ function configDotenv (options) {
}
}

_log(`injecting env (${keysCount}) from ${shortPaths.join(',')} – [tip] encrypt with dotenvx: https://dotenvx.com`)
_log(`injecting env (${keysCount}) from ${shortPaths.join(',')} – [tip] ${_getRandomTip()}`)
}

if (lastError) {
Expand Down
51 changes: 51 additions & 0 deletions tests/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,57 @@ t.test('deals with file:// path and debug true', ct => {
ct.end()
})

t.test('displays random tips from the tips array', ct => {
ct.plan(2)

const logStub = sinon.stub(console, 'log')
const testPath = 'tests/.env'

// Test that tips are displayed (run config multiple times to see variation)
dotenv.config({ path: testPath })
dotenv.config({ path: testPath })
dotenv.config({ path: testPath })

// Should have at least one call that contains a tip
let foundTip = false
for (const call of logStub.getCalls()) {
if (call.args[0] && call.args[0].includes('[tip]')) {
foundTip = true
break
}
}

ct.ok(foundTip, 'Should display a tip')

// Test that the tip contains one of our expected tip messages
let foundExpectedTip = false
const expectedTips = [
'encrypt with dotenvx: https://dotenvx.com',
'specify custom .env file path with { path: \'/custom/path/.env\' }',
'enable debug logging with { debug: true }',
'override existing env vars with { override: true }',
'suppress all logs with { quiet: true }',
'write to custom object with { processEnv: myObject }',
'load multiple .env files with { path: [\'.env.local\', \'.env\'] }'
]

for (const call of logStub.getCalls()) {
if (call.args[0] && call.args[0].includes('[tip]')) {
for (const expectedTip of expectedTips) {
if (call.args[0].includes(expectedTip)) {
foundExpectedTip = true
break
}
}
}
}

ct.ok(foundExpectedTip, 'Should display one of the expected tips')

logStub.restore()
ct.end()
})

t.test('path.relative fails somehow', ct => {
const logStub = sinon.stub(console, 'log')
const pathRelativeStub = sinon.stub(path, 'relative').throws(new Error('fail'))
Expand Down