Skip to content

Commit

Permalink
Replaced optimist with yargs. Closes #308.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreybutler committed Aug 17, 2022
1 parent 5c6c110 commit 4710455
Show file tree
Hide file tree
Showing 13 changed files with 568 additions and 424 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
.DS_Store/
npm-debug.log
node_modules/
example/daemon
example/node_modules
1 change: 1 addition & 0 deletions example/install.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// var Service = require('../lib/node-windows.js').Service;
var Service = require('node-windows').Service;
var dir = require('path').join(process.cwd(), 'helloworld.js')

Expand Down
86 changes: 0 additions & 86 deletions example/package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion example/uninstall.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Service = require('../').Service;
var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
Expand Down
30 changes: 15 additions & 15 deletions lib/binaries.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var path = require('path'),
bin = path.join(__dirname,'..','bin'),
exec = require('child_process').exec;
bin = path.join(__dirname, '..', 'bin'),
exec = require('child_process').exec;

var params = function(options,callback) {
callback = callback || function(){};
var params = function (options, callback) {
callback = callback || function () { };
options = options || {};
if (typeof options === 'function'){
if (typeof options === 'function') {
callback = options;
options = {};
}
if (typeof options !== 'object'){
if (typeof options !== 'object') {
throw 'Invalid options parameter.';
}
return {options:options,callback:callback};
return { options: options, callback: callback };
}

module.exports = {
Expand All @@ -39,9 +39,9 @@ module.exports = {
* @param {Function} callback
* The callback function passed to `require('child_process').exec(cmd,options,<CALLBACK>)`.
*/
elevate: function(cmd,options,callback) {
var p = params(options,callback);
exec('"'+path.join(bin,'elevate','elevate.cmd')+'" '+cmd,p.options,p.callback);
elevate: function (cmd, options, callback) {
var p = params(options, callback);
exec('"' + path.join(bin, 'elevate', 'elevate.cmd') + '" ' + cmd, p.options, p.callback);
},

/**
Expand All @@ -65,14 +65,14 @@ module.exports = {
* @param {Function} [callback]
* The callback function passed to `require('child_process').exec(cmd,options,<CALLBACK>)`.
*/
sudo: function(cmd,password,options,callback){
sudo: function (cmd, password, options, callback) {
password = password || '';
if (typeof password !== 'string'){
if (typeof password !== 'string') {
callback = options;
options = password;
password = '';
}
var p = params(options,callback);
exec(path.join(bin,'sudowin','sudo.exe')+' '+(password !== '' ? '-p '+password:'')+cmd,p.options,p.callback);
var p = params(options, callback);
exec(path.join(bin, 'sudowin', 'sudo.exe') + ' ' + (password !== '' ? '-p ' + password : '') + cmd, p.options, p.callback);
}
}
}
42 changes: 21 additions & 21 deletions lib/cmd.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var exec = require('child_process').exec,
bin = require('./binaries');
bin = require('./binaries');

module.exports = {

Expand All @@ -13,10 +13,10 @@ module.exports = {
* @param {Boolean} callback.isAdmin
* Receives true/false as an argument to the callback.
*/
isAdminUser: function(callback){
exec('NET SESSION',function(err,so,se){
if (se.length !== 0){
bin.elevate('NET SESSION',function(_err,_so,_se){
isAdminUser: function (callback) {
exec('NET SESSION', function (err, so, se) {
if (se.length !== 0) {
bin.elevate('NET SESSION', function (_err, _so, _se) {
callback(_se.length === 0);
});
} else {
Expand All @@ -35,21 +35,21 @@ module.exports = {
* Force close the process.
* @param {Function} [callback]
*/
kill: function(pid,force,callback){
if (!pid){
kill: function (pid, force, callback) {
if (!pid) {
throw new Error('PID is required for the kill operation.');
}

if (isNaN(pid)) {
if (typeof isNaN(pid)) {
throw new Error('PID must be a number.')
}

callback = callback || function(){};
if (typeof force == 'function'){
callback = callback || function () { };
if (typeof force == 'function') {
callback = force;
force = false;
}
exec("taskkill /PID "+pid+(force==true?' /f':''),callback);
exec("taskkill /PID " + pid + (force == true ? ' /f' : ''), callback);
},

/**
Expand All @@ -60,24 +60,24 @@ module.exports = {
* Receives the process object as the only callback argument
* @param {Boolean} [verbose=false]
*/
list: function(callback,verbose){
list: function (callback, verbose) {
verbose = typeof verbose == 'boolean' ? verbose : false;
exec('tasklist /FO CSV'+(verbose==true?' /V':''),function(err,stdout,stderr){
exec('tasklist /FO CSV' + (verbose == true ? ' /V' : ''), function (err, stdout, stderr) {
var p = stdout.split('\r\n');
var proc = [];
var head = null;
while (p.length > 1){
while (p.length > 1) {
var rec = p.shift();
rec = rec.replace(/\"\,/gi,'";').replace(/\"|\'/gi,'').split(';');
if (head == null){
rec = rec.replace(/\"\,/gi, '";').replace(/\"|\'/gi, '').split(';');
if (head == null) {
head = rec;
for (var i=0;i<head.length;i++){
head[i] = head[i].replace(/ /gi,'');
for (var i = 0; i < head.length; i++) {
head[i] = head[i].replace(/ /gi, '');
}
} else {
var tmp = {};
for (var i=0;i<rec.length;i++){
tmp[head[i]] = rec[i].replace(/\"|\'/gi,'');
for (var i = 0; i < rec.length; i++) {
tmp[head[i]] = rec[i].replace(/\"|\'/gi, '');
}
proc.push(tmp);
}
Expand All @@ -86,4 +86,4 @@ module.exports = {
});
}

};
};
Loading

0 comments on commit 4710455

Please sign in to comment.