Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add userHomeDir #521

Merged
merged 1 commit into from
Jun 24, 2019
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
16 changes: 16 additions & 0 deletions os/README.md
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions os/mod.ts
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add "Needs permissions to access env (--allow-env)."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

*/
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 changes: 8 additions & 0 deletions os/test.ts
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(), "");
});
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down