From 8cf14eeb6aea68134f0f0b304a83a5d24ec43793 Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Sun, 10 May 2015 12:51:39 +0200 Subject: [PATCH] os: add .homedir() In almost every cli-client that I write I need the current homedir at a certain point. While we have a function for getting the directory for storing temporary files (callled `tmpdir`), a function for receiving the location of the home directory is still missing. --- doc/api/os.markdown | 4 ++++ lib/os.js | 5 +++++ test/parallel/test-os.js | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/doc/api/os.markdown b/doc/api/os.markdown index 9202c74914c374..e3e40ee2df85a8 100644 --- a/doc/api/os.markdown +++ b/doc/api/os.markdown @@ -10,6 +10,10 @@ Use `require('os')` to access this module. Returns the operating system's default directory for temporary files. +## os.homedir() + +Returns the home directory path of the current user. + ## os.endianness() Returns the endianness of the CPU. Possible values are `'BE'` for big endian diff --git a/lib/os.js b/lib/os.js index cd4eb1c12f49bc..50a17b3e33f411 100644 --- a/lib/os.js +++ b/lib/os.js @@ -51,3 +51,8 @@ if (binding.isBigEndian) exports.endianness = function() { return 'BE'; }; else exports.endianness = function() { return 'LE'; }; + +exports.homedir = function() { + const location = isWindows ? process.env.USERPROFILE : process.env.HOME; + return location; +}; diff --git a/test/parallel/test-os.js b/test/parallel/test-os.js index b5f39973a6f71f..200763c5ed86d3 100644 --- a/test/parallel/test-os.js +++ b/test/parallel/test-os.js @@ -90,3 +90,9 @@ switch (platform) { var EOL = os.EOL; assert.ok(EOL.length > 0); + +if (process.platform === 'win32') { + assert.equal(os.homedir(), process.env.USERPROFILE); +} else { + assert.equal(os.homedir(), process.env.HOME); +}