-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue #11] Add support for relative & absolute filepaths
- Loading branch information
Showing
9 changed files
with
143 additions
and
10 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
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,36 @@ | ||
import { PathLike } from 'fs'; | ||
import * as path from 'path'; | ||
import { ConfigurationOptions } from './options'; | ||
|
||
/** | ||
* Resolve the target filepath from options | ||
* | ||
* @param options Configuration options | ||
* @returns Returns the target filepath | ||
*/ | ||
export function resolveOptionsFilepath( | ||
options: ConfigurationOptions | ||
): PathLike { | ||
let file: PathLike; | ||
|
||
if (options.absolutePath) { | ||
file = options.absolutePath; | ||
} | ||
else if (options.relativePath) { | ||
file = path.join(process.cwd(), options.relativePath); | ||
} | ||
else { | ||
file = process.cwd(); | ||
} | ||
|
||
const filename = path.basename(file); | ||
|
||
if (!filename.includes('.')) { | ||
file = path.join(file, '.env'); | ||
} | ||
else { | ||
file = path.join(file); | ||
} | ||
|
||
return file; | ||
} |
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 @@ | ||
APP_TITLE="Absolute Path" |
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 @@ | ||
APP_TITLE="Relative Path" |
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,43 @@ | ||
import { | ||
resolveOptionsFilepath | ||
} from '../../../src/options/resolve-options-filepath'; | ||
import * as path from 'path'; | ||
|
||
describe('options/resolve-options-filepath', () => { | ||
it('can use a default file', () => { | ||
const file = resolveOptionsFilepath({ }); | ||
|
||
expect(file).toEqual(path.join(process.cwd(), '.env')); | ||
}); | ||
|
||
it('can accept a relative file', () => { | ||
const file = resolveOptionsFilepath( | ||
{ relativePath: 'test/relative.env' } | ||
); | ||
|
||
expect(file).toEqual(path.join( | ||
process.cwd(), | ||
'test/relative.env' | ||
)); | ||
}); | ||
|
||
it('can accept a relative directory', () => { | ||
const file = resolveOptionsFilepath({ relativePath: 'test/' }); | ||
|
||
expect(file).toEqual(path.join(process.cwd(), 'test', '.env')); | ||
}); | ||
|
||
it('can accept an absolute file', () => { | ||
const file = resolveOptionsFilepath( | ||
{ absolutePath: 'C:/test/absolute.env' } | ||
); | ||
|
||
expect(file).toEqual(path.join('C:/test/absolute.env')); | ||
}); | ||
|
||
it('can accept an absolute directory', () => { | ||
const file = resolveOptionsFilepath({ absolutePath: 'C:/test/' }); | ||
|
||
expect(file).toEqual(path.join('C:/test/.env')); | ||
}); | ||
}); |