mirror of
https://github.com/denoland/deno.git
synced 2024-11-27 16:10:57 -05:00
58bac5dc29
Original: ff7fb5a5d6
26 lines
710 B
TypeScript
26 lines
710 B
TypeScript
// 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.`);
|
|
}
|