Comparing version 5.1.0 to 6.0.0
@@ -1,39 +0,33 @@ | ||
declare const username: { | ||
/** | ||
Get the username of the current user. | ||
/** | ||
Get the username of the current user. | ||
It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached. | ||
It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached. | ||
@returns The username. | ||
@returns The username. | ||
@example | ||
``` | ||
import username = require('username'); | ||
@example | ||
``` | ||
import {username} from 'username'; | ||
(async () => { | ||
console.log(await username()); | ||
//=> 'sindresorhus' | ||
})(); | ||
``` | ||
*/ | ||
(): Promise<string | undefined>; | ||
console.log(await username()); | ||
//=> 'sindresorhus' | ||
``` | ||
*/ | ||
export function username(): Promise<string | undefined>; | ||
/** | ||
Synchronously get the username of the current user. | ||
/** | ||
Synchronously get the username of the current user. | ||
It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached. | ||
It first tries to get the username from the `SUDO_USER` `LOGNAME` `USER` `LNAME` `USERNAME` environment variables. Then falls back to `$ id -un` on macOS / Linux and `$ whoami` on Windows, in the rare case none of the environment variables are set. The result is cached. | ||
@returns The username. | ||
@returns The username. | ||
@example | ||
``` | ||
import username = require('username'); | ||
@example | ||
``` | ||
import {usernameSync} from 'username'; | ||
console.log(username.sync()); | ||
//=> 'sindresorhus' | ||
``` | ||
*/ | ||
sync(): string | undefined; | ||
}; | ||
export = username; | ||
console.log(usernameSync()); | ||
//=> 'sindresorhus' | ||
``` | ||
*/ | ||
export function usernameSync(): string | undefined; |
50
index.js
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
const os = require('os'); | ||
const execa = require('execa'); | ||
const mem = require('mem'); | ||
import process from 'node:process'; | ||
import os from 'node:os'; | ||
import execa from 'execa'; | ||
import mem from 'mem'; | ||
@@ -10,8 +10,8 @@ const getEnvironmentVariable = () => { | ||
return ( | ||
env.SUDO_USER || | ||
env.C9_USER /* Cloud9 */ || | ||
env.LOGNAME || | ||
env.USER || | ||
env.LNAME || | ||
env.USERNAME | ||
env.SUDO_USER | ||
|| env.C9_USER | ||
|| /* Cloud9 */ env.LOGNAME | ||
|| env.USER | ||
|| env.LNAME | ||
|| env.USERNAME | ||
); | ||
@@ -23,3 +23,3 @@ }; | ||
return os.userInfo().username; | ||
} catch (_) {} | ||
} catch {} | ||
}; | ||
@@ -31,6 +31,6 @@ | ||
module.exports = mem(async () => { | ||
const envVariable = getEnvironmentVariable(); | ||
if (envVariable) { | ||
return envVariable; | ||
export const username = mem(async () => { | ||
const environmentVariable = getEnvironmentVariable(); | ||
if (environmentVariable) { | ||
return environmentVariable; | ||
} | ||
@@ -48,15 +48,17 @@ | ||
if (process.platform === 'win32') { | ||
return cleanWindowsCommand(await execa.stdout('whoami')); | ||
const {stdout} = await execa('whoami'); | ||
return cleanWindowsCommand(stdout); | ||
} | ||
const userId = await execa.stdout('id', ['-u']); | ||
const {stdout: userId} = await execa('id', ['-u']); | ||
try { | ||
return await execa.stdout('id', ['-un', userId]); | ||
} catch (_) {} | ||
const {stdout} = await execa('id', ['-un', userId]); | ||
return stdout; | ||
} catch {} | ||
return makeUsernameFromId(userId); | ||
} catch (_) {} | ||
} catch {} | ||
}); | ||
module.exports.sync = mem(() => { | ||
export const usernameSync = mem(() => { | ||
const envVariable = getEnvironmentVariable(); | ||
@@ -77,9 +79,9 @@ if (envVariable) { | ||
const userId = execa.sync('id', ['-u']).stdout; | ||
const {stdout: userId} = execa.sync('id', ['-u']); | ||
try { | ||
return execa.sync('id', ['-un', userId]).stdout; | ||
} catch (_) {} | ||
} catch {} | ||
return makeUsernameFromId(userId); | ||
} catch (_) {} | ||
} catch {} | ||
}); |
{ | ||
"name": "username", | ||
"version": "5.1.0", | ||
"version": "6.0.0", | ||
"description": "Get the username of the current user", | ||
"license": "MIT", | ||
"repository": "sindresorhus/username", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -36,10 +39,10 @@ "scripts": { | ||
"dependencies": { | ||
"execa": "^1.0.0", | ||
"mem": "^4.3.0" | ||
"execa": "^5.1.1", | ||
"mem": "^9.0.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# username [![Build Status](https://travis-ci.org/sindresorhus/username.svg?branch=master)](https://travis-ci.org/sindresorhus/username) | ||
# username | ||
@@ -7,3 +7,2 @@ > Get the username of the current user | ||
## Install | ||
@@ -15,2 +14,3 @@ | ||
*This package only works in Node.js, not in browsers.* | ||
@@ -20,11 +20,8 @@ ## Usage | ||
```js | ||
const username = require('username'); | ||
import {username} from 'username'; | ||
(async () => { | ||
console.log(await username()); | ||
//=> 'sindresorhus' | ||
})(); | ||
console.log(await username()); | ||
//=> 'sindresorhus' | ||
``` | ||
## API | ||
@@ -38,7 +35,6 @@ | ||
### username.sync() | ||
### usernameSync() | ||
Returns the username. | ||
## Related | ||
@@ -48,6 +44,1 @@ | ||
- [fullname](https://github.com/sindresorhus/fullname) - Get the fullname of the current user | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
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
5854
Yes
91
40
+ Addedcross-spawn@7.0.5(transitive)
+ Addedexeca@5.1.1(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedhuman-signals@2.1.0(transitive)
+ Addedis-stream@2.0.1(transitive)
+ Addedmem@9.0.2(transitive)
+ Addedmerge-stream@2.0.0(transitive)
+ Addedmimic-fn@4.0.0(transitive)
+ Addednpm-run-path@4.0.1(transitive)
+ Addedonetime@5.1.2(transitive)
+ Addedpath-key@3.1.1(transitive)
+ Addedshebang-command@2.0.0(transitive)
+ Addedshebang-regex@3.0.0(transitive)
+ Addedstrip-final-newline@2.0.0(transitive)
+ Addedwhich@2.0.2(transitive)
- Removedcross-spawn@6.0.5(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexeca@1.0.0(transitive)
- Removedget-stream@4.1.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedmem@4.3.0(transitive)
- Removednice-try@1.0.5(transitive)
- Removednpm-run-path@2.0.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedp-is-promise@2.1.0(transitive)
- Removedpath-key@2.0.1(transitive)
- Removedpump@3.0.2(transitive)
- Removedsemver@5.7.2(transitive)
- Removedshebang-command@1.2.0(transitive)
- Removedshebang-regex@1.0.0(transitive)
- Removedstrip-eof@1.0.0(transitive)
- Removedwhich@1.3.1(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedexeca@^5.1.1
Updatedmem@^9.0.1