GeezerVison is showing github.com — os: add homedir() · nodejs/node@e7e5992
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/os.markdown +4Lines changed: 4 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,10 @@ Use `require('os')` to access this module. | 10 | 10 |
| 11 | 11 | Returns the operating system's default directory for temporary files. | 12 | 12 |
| | 13 | +## os.homedir() | | 14 | + | | 15 | +Returns the home directory of the current user. | | 16 | + | 13 | 17 | ## os.endianness() | 14 | 18 |
| 15 | 19 | Returns the endianness of the CPU. Possible values are `'BE'` for big endian | |
Collapse file Copy file name to clipboardExpand all lines: lib/os.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,8 @@ exports.cpus = binding.getCPUs; | 13 | 13 | exports.type = binding.getOSType; | 14 | 14 | exports.release = binding.getOSRelease; | 15 | 15 | exports.networkInterfaces = binding.getInterfaceAddresses; | | 16 | +exports.homedir = binding.getHomeDirectory; | | 17 | + | 16 | 18 |
| 17 | 19 | exports.arch = function() { | 18 | 20 | return process.arch; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_os.cc +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -271,6 +271,29 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { | 271 | 271 | } | 272 | 272 |
| 273 | 273 |
| | 274 | +static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) { | | 275 | + Environment* env = Environment::GetCurrent(args); | | 276 | +#ifdef _WIN32 | | 277 | + char buf[MAX_PATH]; | | 278 | +#else | | 279 | + char buf[PATH_MAX]; | | 280 | +#endif | | 281 | + | | 282 | + size_t len = sizeof(buf); | | 283 | + int err = uv_os_homedir(buf, &len); | | 284 | + | | 285 | + if (err) { | | 286 | + return env->ThrowUVException(err, "uv_os_homedir"); | | 287 | + } | | 288 | + | | 289 | + Local<String> home = String::NewFromUtf8(env->isolate(), | | 290 | + buf, | | 291 | + String::kNormalString, | | 292 | + len); | | 293 | + args.GetReturnValue().Set(home); | | 294 | +} | | 295 | + | | 296 | + | 274 | 297 | void Initialize(Handle<Object> target, | 275 | 298 | Handle<Value> unused, | 276 | 299 | Handle<Context> context) { | @@ -284,6 +307,7 @@ void Initialize(Handle<Object> target, | 284 | 307 | env->SetMethod(target, "getOSType", GetOSType); | 285 | 308 | env->SetMethod(target, "getOSRelease", GetOSRelease); | 286 | 309 | env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses); | | 310 | + env->SetMethod(target, "getHomeDirectory", GetHomeDirectory); | 287 | 311 | target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"), | 288 | 312 | Boolean::New(env->isolate(), IsBigEndian())); | 289 | 313 | } | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-os.js +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | var common = require('../common'); | 3 | 3 | var assert = require('assert'); | 4 | 4 | var os = require('os'); | | 5 | +var isWindows = process.platform === 'win32'; | 5 | 6 |
| 6 | 7 |
| 7 | 8 | process.env.TMPDIR = '/tmpdir'; | 8 | 9 | process.env.TMP = '/tmp'; | 9 | 10 | process.env.TEMP = '/temp'; | 10 | | -if (process.platform === 'win32') { | | 11 | +if (isWindows) { | 11 | 12 | assert.equal(os.tmpdir(), '/temp'); | 12 | 13 | process.env.TEMP = ''; | 13 | 14 | assert.equal(os.tmpdir(), '/tmp'); | @@ -101,3 +102,15 @@ switch (platform) { | 101 | 102 |
| 102 | 103 | var EOL = os.EOL; | 103 | 104 | assert.ok(EOL.length > 0); | | 105 | + | | 106 | + | | 107 | +var home = os.homedir(); | | 108 | + | | 109 | +console.log('homedir = ' + home); | | 110 | +assert.ok(home.length > 0); | | 111 | + | | 112 | +if (isWindows && process.env.USERPROFILE) { | | 113 | + assert.equal(home, process.env.USERPROFILE); | | 114 | +} else if (!isWindows && process.env.HOME) { | | 115 | + assert.equal(home, process.env.HOME); | | 116 | +} |
|