From f3987cd5211dd36cc951b9f82be5937ef3cf130a Mon Sep 17 00:00:00 2001 From: Deniz Ariyan Date: Fri, 2 Oct 2020 23:46:44 +0300 Subject: [PATCH] Documentation and cleanup --- README.md | 38 +++++++++++++++++++++++++++++++++++++- index.js | 17 +++++++---------- package.json | 14 +++++++++----- test.js | 9 +++++++++ 4 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 test.js diff --git a/README.md b/README.md index 00e926c..f6e7a14 100644 --- a/README.md +++ b/README.md @@ -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 `` 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) \ No newline at end of file diff --git a/index.js b/index.js index 0d615a9..79698cf 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ -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 { @@ -11,14 +11,11 @@ function _check() { } }); } 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 - -} \ No newline at end of file + return await _check(); + } diff --git a/package.json b/package.json index e52a834..2b90af9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -14,8 +14,12 @@ "admin", "permission", "check", - "is-admin", - "administrator" + "is admin", + "administrator", + "is", + "admin", + "root", + "if admin" ], "author": "denizariyan", "license": "MIT", diff --git a/test.js b/test.js new file mode 100644 index 0000000..4d44bfe --- /dev/null +++ b/test.js @@ -0,0 +1,9 @@ +const admin = require("./index"); + +admin.check().then(result => { + if (result) { + console.log("1"); + } else { + console.log("0"); + } +}); \ No newline at end of file