-
Notifications
You must be signed in to change notification settings - Fork 621
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1365d28
commit 80d8194
Showing
4 changed files
with
49 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# os | ||
|
||
Module provide platform-independent interface to operating system functionality. | ||
|
||
## Usage | ||
|
||
### userHomeDir | ||
|
||
Returns the current user's home directory. On Unix, including macOS, it returns the $HOME environment variable. On Windows, it returns %USERPROFILE%. | ||
|
||
```ts | ||
import { userHomeDir } from "https://deno.land/std/os/mod.ts"; | ||
|
||
userHomeDir(); | ||
``` |
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,25 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Returns the current user's home directory. | ||
* On Unix, including macOS, it returns the $HOME environment variable. | ||
* On Windows, it returns %USERPROFILE%. | ||
* | ||
* Ported from Go: https://github.com/golang/go/blob/go1.12.5/src/os/file.go#L389 | ||
*/ | ||
export function userHomeDir(): string { | ||
let env = "HOME"; | ||
let envErr = "$HOME"; | ||
|
||
if (Deno.platform.os === "win") { | ||
env = "USERPROFILE"; | ||
envErr = "%USERPROFILE%"; | ||
} | ||
|
||
const value = Deno.env()[env]; | ||
if (value !== "") { | ||
return value; | ||
} | ||
|
||
throw new Error(`Environment variable '${envErr}' is not defined.`); | ||
} |
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,8 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { test } from "../testing/mod.ts"; | ||
import { assertNotEquals } from "../testing/asserts.ts"; | ||
import { userHomeDir } from './mod.ts'; | ||
|
||
test(function testUserHomeDir(): void { | ||
assertNotEquals(userHomeDir(), ""); | ||
}); |
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