Comparing version 1.0.1 to 2.0.0
67
index.js
'use strict'; | ||
var exec = require('child_process').exec; | ||
var username; | ||
var env = process.env; | ||
var first = true; | ||
const childProcess = require('child_process'); | ||
const execa = require('execa'); | ||
const mem = require('mem'); | ||
module.exports = function (cb) { | ||
if (!first) { | ||
return cb(null, username); | ||
} | ||
function getEnvVar() { | ||
const env = process.env; | ||
return env.LOGNAME || env.USER || env.LNAME || env.USERNAME; | ||
} | ||
first = false; | ||
function cleanWinCmd(x) { | ||
return x.replace(/^.*\\/, ''); | ||
} | ||
username = env.LOGNAME || env.USER || env.LNAME || env.USERNAME; | ||
function noop() {} | ||
if (username) { | ||
return cb(null, username); | ||
module.exports = mem(() => { | ||
const envVar = getEnvVar(); | ||
if (envVar) { | ||
return Promise.resolve(envVar); | ||
} | ||
if (process.platform === 'darwin' || process.platform === 'linux') { | ||
exec('id -un', function (err, stdout) { | ||
if (err) { | ||
return cb(); | ||
} | ||
return execa('id', ['-un']).then(x => x.stdout).catch(noop); | ||
} else if (process.platform === 'win32') { | ||
return execa('whoami').then(x => cleanWinCmd(x.stdout)).catch(noop); | ||
} | ||
username = stdout.trim(); | ||
return Promise.resolve(); | ||
}); | ||
cb(null, username); | ||
}); | ||
} else if (process.platform === 'win32') { | ||
exec('whoami', function (err, stdout) { | ||
if (err) { | ||
return cb(); | ||
} | ||
module.exports.sync = mem(() => { | ||
const envVar = getEnvVar(); | ||
username = stdout.trim().replace(/^.*\\/, ''); | ||
cb(null, username); | ||
}); | ||
} else { | ||
cb(); | ||
if (envVar) { | ||
return envVar; | ||
} | ||
}; | ||
module.exports.sync = require('./sync'); | ||
try { | ||
if (process.platform === 'darwin' || process.platform === 'linux') { | ||
// TODO: use `execa` when it gets support for sync methods | ||
return childProcess.execFileSync('id', ['-un'], {encoding: 'utf8'}); | ||
} else if (process.platform === 'win32') { | ||
return cleanWinCmd(childProcess.execFileSync('whoami', {encoding: 'utf8'})); | ||
} | ||
} catch (err) {} | ||
}); |
{ | ||
"name": "username", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Get the username of the current user", | ||
@@ -12,3 +12,2 @@ "license": "MIT", | ||
}, | ||
"bin": "cli.js", | ||
"engines": { | ||
@@ -18,14 +17,8 @@ "node": ">=0.10.0" | ||
"scripts": { | ||
"test": "xo && mocha test.js && mocha test-fallback.js" | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"cli.js", | ||
"index.js", | ||
"sync.js" | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"cli-app", | ||
"cli", | ||
"bin", | ||
"app", | ||
"username", | ||
@@ -37,11 +30,20 @@ "user", | ||
"lname", | ||
"whoami" | ||
"whoami", | ||
"shell", | ||
"env", | ||
"var", | ||
"environment", | ||
"variable" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"dependencies": { | ||
"meow": "^3.4.0" | ||
"execa": "^0.2.2", | ||
"mem": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"xo": "*" | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -16,13 +16,8 @@ # username [![Build Status](https://travis-ci.org/sindresorhus/username.svg?branch=master)](https://travis-ci.org/sindresorhus/username) | ||
```js | ||
var username = require('username'); | ||
const username = require('username'); | ||
username(function (err, username) { | ||
username().then(username => { | ||
console.log(username); | ||
//=> 'sindresorhus' | ||
}); | ||
// or | ||
username.sync(); | ||
//=> 'sindresorhus' | ||
``` | ||
@@ -33,30 +28,16 @@ | ||
Tries to get the username from the `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. The result is cached. | ||
First tries to get the username from the `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on OS X / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached. | ||
### username(callback) | ||
### username() | ||
Falls back to `id -un` on OS X / Linux and `whoami` on Windows in the rare case none of the environment variables are set. | ||
Returns a promise for the username. | ||
##### callback(error, username) | ||
### username.sync() | ||
Returns the username. | ||
## CLI | ||
``` | ||
$ npm install --global username | ||
``` | ||
``` | ||
$ username --help | ||
Example | ||
$ username | ||
sindresorhus | ||
``` | ||
## Related | ||
- [username-cli](https://github.com/sindresorhus/username-cli) - CLI for this module | ||
- [fullname](https://github.com/sindresorhus/fullname) - Get the fullname of the current user | ||
@@ -63,0 +44,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4028
2
4
38
47
+ Addedexeca@^0.2.2
+ Addedmem@^0.1.0
+ Addedcross-spawn-async@2.2.5(transitive)
+ Addedexeca@0.2.2(transitive)
+ Addedisexe@2.0.0(transitive)
+ Addedlru-cache@4.1.5(transitive)
+ Addedmem@0.1.1(transitive)
+ Addednpm-run-path@1.0.0(transitive)
+ Addedpath-key@1.0.0(transitive)
+ Addedpseudomap@1.0.2(transitive)
+ Addedstrip-eof@1.0.0(transitive)
+ Addedwhich@1.3.1(transitive)
+ Addedyallist@2.1.2(transitive)
- Removedmeow@^3.4.0
- Removedarray-find-index@1.0.2(transitive)
- Removedcamelcase@2.1.1(transitive)
- Removedcamelcase-keys@2.1.0(transitive)
- Removedcurrently-unhandled@0.4.1(transitive)
- Removeddecamelize@1.2.0(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedfind-up@1.1.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-stdin@4.0.1(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhosted-git-info@2.8.9(transitive)
- Removedindent-string@2.1.0(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedis-core-module@2.15.1(transitive)
- Removedis-finite@1.1.0(transitive)
- Removedis-utf8@0.2.1(transitive)
- Removedload-json-file@1.1.0(transitive)
- Removedloud-rejection@1.6.0(transitive)
- Removedmap-obj@1.0.1(transitive)
- Removedmeow@3.7.0(transitive)
- Removedminimist@1.2.8(transitive)
- Removednormalize-package-data@2.5.0(transitive)
- Removedparse-json@2.2.0(transitive)
- Removedpath-exists@2.1.0(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpath-type@1.1.0(transitive)
- Removedpify@2.3.0(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedread-pkg@1.1.0(transitive)
- Removedread-pkg-up@1.0.1(transitive)
- Removedredent@1.0.0(transitive)
- Removedrepeating@2.0.1(transitive)
- Removedresolve@1.22.8(transitive)
- Removedsemver@5.7.2(transitive)
- Removedsignal-exit@3.0.7(transitive)
- Removedspdx-correct@3.2.0(transitive)
- Removedspdx-exceptions@2.5.0(transitive)
- Removedspdx-expression-parse@3.0.1(transitive)
- Removedspdx-license-ids@3.0.20(transitive)
- Removedstrip-bom@2.0.0(transitive)
- Removedstrip-indent@1.0.1(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtrim-newlines@1.0.0(transitive)
- Removedvalidate-npm-package-license@3.0.4(transitive)