Socket
Socket
Sign inDemoInstall

username

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

username - npm Package Compare versions

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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc