mirror of
https://github.com/denoland/deno.git
synced 2025-01-22 23:19:55 -05:00
parent
69d4d88b4f
commit
58bac5dc29
4 changed files with 51 additions and 0 deletions
16
os/README.md
Normal file
16
os/README.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# 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%.
|
||||
Needs permissions to access env (--allow-env).
|
||||
|
||||
```ts
|
||||
import { userHomeDir } from "https://deno.land/std/os/mod.ts";
|
||||
|
||||
userHomeDir();
|
||||
```
|
26
os/mod.ts
Normal file
26
os/mod.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
// 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%.
|
||||
* Needs permissions to access env (--allow-env).
|
||||
*
|
||||
* 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.`);
|
||||
}
|
8
os/test.ts
Normal file
8
os/test.ts
Normal file
|
@ -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(), "");
|
||||
});
|
1
test.ts
1
test.ts
|
@ -23,6 +23,7 @@ import "./textproto/test.ts";
|
|||
import "./util/test.ts";
|
||||
import "./ws/test.ts";
|
||||
import "./encoding/test.ts";
|
||||
import "./os/test.ts";
|
||||
|
||||
import { xrun } from "./prettier/util.ts";
|
||||
import { red, green } from "./colors/mod.ts";
|
||||
|
|
Loading…
Add table
Reference in a new issue