Skip to content

Commit

Permalink
Documentation and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
denizariyan committed Oct 2, 2020
1 parent 0f64ae7 commit f3987cd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
admin-check
# Admin Check

![Version](https://img.shields.io/github/v/release/denizariyan/admin-check)

Admin Check is an NPM package to check if the current script is running with admin privileges.

Currently only available for Windows. It uses a native Windows command("net session") that is only available with admin privileges to check if the admin privileges are present.

## Installation

Use the package manager NPM to install Admin Check.

```bash
npm i admin-check
```

## Usage

Returns a `<Boolean>` which is true when admin privileges are present.
```nodejs
const admin = require("admin-check");
admin.check().then(result => {
if (result) {
// Do something when admin privileges are present
} else {
// Do something when admin privileges are NOT present
}
});
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


## License
[MIT](https://github.com/denizariyan/admin-check/blob/main/LICENSE.md)
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
var platform = require("os").platform();
const platform = require("os").platform();

function _check() {
return new Promise(resolve => {
return new Promise(resolve => { // Has to be a promise since Windows can take quite a while to respond under some circumstances
if (platform == "win32" || platform == "win64") {
require('child_process').exec('net session', function (err, stdout, stderr) {
require('child_process').exec('net session', function (err, stdout, stderr) { // "net session" will return an error when admin privileges are not present
if (err) {
resolve(false);
} else {
resolve(true);
}
});
} else {
throw new Error('Can not determine if admin priviliges are present or not')
throw new Error('Can not determine if admin priviliges are present or not. This package is only compatible with Windows OS')
}
});
});
}


exports.check = async function () {
var result = await _check();
return result

}
return await _check();
}
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "admin-check",
"version": "1.1.0",
"description": "package to check if the script is runnig with admin permissions",
"version": "1.1.1",
"description": "package to check if the script is running with admin permissions",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test.js"
},
"repository": {
"type": "git",
Expand All @@ -14,8 +14,12 @@
"admin",
"permission",
"check",
"is-admin",
"administrator"
"is admin",
"administrator",
"is",
"admin",
"root",
"if admin"
],
"author": "denizariyan",
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const admin = require("./index");

admin.check().then(result => {
if (result) {
console.log("1");
} else {
console.log("0");
}
});

0 comments on commit f3987cd

Please sign in to comment.