-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow .env style output from resolve and add associated docs
- Loading branch information
1 parent
03eb697
commit aab732c
Showing
4 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { expect, test, describe } from 'vitest'; | ||
import { stringifyObjectAsEnvFile } from './env-file-helpers'; | ||
|
||
describe('stringifyObjectAsEnvFile', () => { | ||
test('basic', () => { | ||
const result = stringifyObjectAsEnvFile({ foo: 'bar', baz: 'qux' }); | ||
expect(result).toEqual('foo="bar"\nbaz="qux"'); | ||
}); | ||
test('escapes backslashes', () => { | ||
const result = stringifyObjectAsEnvFile({ foo: 'bar\\baz' }); | ||
expect(result).toEqual('foo="bar\\\\baz"'); | ||
}); | ||
test('escapes newlines', () => { | ||
const result = stringifyObjectAsEnvFile({ foo: 'bar\nbaz' }); | ||
expect(result).toEqual('foo="bar\\nbaz"'); | ||
}); | ||
test('escapes double quotes', () => { | ||
const result = stringifyObjectAsEnvFile({ foo: 'bar"baz' }); | ||
expect(result).toEqual('foo="bar\\"baz"'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function stringifyObjectAsEnvFile(obj: Record<string, string>) { | ||
return Object.entries(obj).map(([key, value]) => { | ||
// Handle newlines and quotes by wrapping in double quotes and escaping | ||
const formattedValue = String(value) | ||
.replace(/\\/g, '\\\\') // escape backslashes first | ||
.replace(/\n/g, '\\n') // escape newlines | ||
.replace(/"/g, '\\"'); // escape double quotes | ||
|
||
return `${key}="${formattedValue}"`; | ||
}).join('\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters