2020-07-07 09:02:51 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Shell API
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
2020-07-21 09:01:38 +00:00
|
|
|
var FILE = require("lib/file");
|
2020-07-07 09:02:51 +00:00
|
|
|
|
2020-10-26 09:08:53 +00:00
|
|
|
var addslashes = function(s) {
|
2020-07-30 02:53:45 +00:00
|
|
|
return s.toString().replace(/\\/g, '\\\\').
|
2020-07-26 11:40:25 +00:00
|
|
|
replace(/\u0008/g, '\\b').
|
|
|
|
replace(/\t/g, '\\t').
|
|
|
|
replace(/\n/g, '\\n').
|
|
|
|
replace(/\f/g, '\\f').
|
|
|
|
replace(/\r/g, '\\r').
|
|
|
|
replace(/'/g, '\\\'').
|
|
|
|
replace(/"/g, '\\"');
|
|
|
|
};
|
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
var ShellObject = function() {
|
|
|
|
this.interface = null;
|
|
|
|
this.currentDirectory = null;
|
|
|
|
this.workingDirectory = null;
|
|
|
|
this.isElevated = false;
|
|
|
|
this.isFork = false;
|
2020-11-18 07:58:01 +00:00
|
|
|
this.isVisibleWindow = false;
|
2020-11-18 04:13:38 +00:00
|
|
|
|
|
|
|
this.create = function() {
|
|
|
|
try {
|
|
|
|
this.interface = CreateObject("WScript.Shell");
|
|
|
|
this.currentDirectory = this.interface.CurrentDirectory;
|
|
|
|
} catch (e) {
|
|
|
|
console.error("ShellObject.create() -> " + e.message);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setWorkingDirectory = function(dirname) {
|
|
|
|
if (typeof(dirname) === "string") {
|
|
|
|
this.workingDirectory = dirname;
|
|
|
|
this.interface.CurrentDirectory = this.workingDirectory;
|
|
|
|
console.info("ShellObject.workingDirectory -> " + this.workingDirectory);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2020-11-18 07:58:01 +00:00
|
|
|
this.setVisibleWindow = function(visible) {
|
|
|
|
this.isVisibleWindow = visible;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
this.build = function(cmd) {
|
|
|
|
if (typeof(cmd) === "string") {
|
|
|
|
return cmd;
|
|
|
|
} else if (typeof(cmd) === "object") {
|
|
|
|
return cmd.map(function(s) {
|
|
|
|
var regex = /[ "]/g;
|
|
|
|
if (!regex.test(s)) {
|
|
|
|
return s;
|
|
|
|
} else {
|
|
|
|
return "\"" + addslashes(s) + "\"";
|
|
|
|
}
|
|
|
|
}).join(' ');
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.createProcess = function(cmd) {
|
|
|
|
try {
|
|
|
|
var c = this.build(cmd);
|
|
|
|
console.info("ShellObject.createProcess() -> " + c);
|
|
|
|
return this.interface.Exec(c);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("ShellObject.createProcess() -> " + e.message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.exec = function(cmd, stdOutPath) {
|
|
|
|
var data;
|
|
|
|
|
|
|
|
if (typeof(stdOutPath) === "undefined") {
|
|
|
|
stdOutPath = "stdout.txt";
|
|
|
|
}
|
|
|
|
var c = "%comspec% /c (" + this.build(cmd) + ") 1> " + stdOutPath;
|
|
|
|
c += " 2>&1";
|
|
|
|
this.interface.Run(c, 0, true);
|
|
|
|
console.info("ShellObject.exec() -> " + c);
|
|
|
|
data = FILE.readFile(stdOutPath, "utf-8");
|
|
|
|
|
|
|
|
if (FILE.fileExists(stdOutPath)) {
|
|
|
|
FILE.deleteFile(stdOutPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.run = function(cmd, fork) {
|
|
|
|
var fork = (typeof(fork) !== "undefined") ? fork : true;
|
|
|
|
var c = "%comspec% /q /c (" + this.build(cmd) + ")";
|
|
|
|
console.info("ShellObject.run() -> " + c);
|
2020-11-18 07:58:01 +00:00
|
|
|
this.interface.Run(c, (!this.isVisibleWindow ? 0 : 1), !fork);
|
2020-11-18 04:13:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.runAs = function(FN, args) {
|
|
|
|
var oShell = CreateObject("Shell.Application");
|
2020-11-18 07:58:01 +00:00
|
|
|
console.info("ShellObject.runAs() -> " + FN);
|
|
|
|
oShell.shellExecute(FN, args, this.workingDirectory, "runas", (!this.isVisibleWindow ? 0 : 1));
|
2020-11-18 04:13:38 +00:00
|
|
|
return oShell;
|
|
|
|
};
|
|
|
|
|
2020-11-18 10:55:06 +00:00
|
|
|
this.createDesktopIcon = function(shoutcutname, cmd, workingDirectory) {
|
2020-11-18 06:46:34 +00:00
|
|
|
var desktopPath = this.interface.SpecialFolders("Desktop");
|
2020-11-18 10:55:06 +00:00
|
|
|
var path = desktopPath + "\\" + shoutcutname + ".lnk";
|
|
|
|
|
|
|
|
if (!FILE.fileExists(path)) {
|
|
|
|
var link = this.interface.CreateShortcut(path);
|
|
|
|
link.TargetPath = "cmd";
|
|
|
|
link.Arguments = "/q /c " + this.build(cmd);
|
|
|
|
link.WindowStyle = 3;
|
|
|
|
link.WorkingDirectory = workingDirectory;
|
|
|
|
//link.Hotkey = "";
|
|
|
|
//link.IconLocation = "";
|
|
|
|
link.Save();
|
|
|
|
}
|
2020-11-18 06:46:34 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
this.release = function() {
|
|
|
|
console.info("ShellObject.release() -> " + this.currentDirectory);
|
|
|
|
this.interface.CurrentDirectory = this.currentDirectory;
|
|
|
|
this.interface = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.create();
|
2020-07-26 11:40:25 +00:00
|
|
|
};
|
2020-07-07 09:02:51 +00:00
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
exports.create = function() {
|
|
|
|
return new ShellObject();
|
|
|
|
};
|
2020-07-07 09:02:51 +00:00
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
exports.build = function(cmd) {
|
|
|
|
return (new ShellObject()).build(cmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.exec = function(cmd, stdOutPath) {
|
|
|
|
return (new ShellObject()).exec(cmd, stdOutPath);
|
|
|
|
};
|
2020-07-07 09:02:51 +00:00
|
|
|
|
2020-07-20 18:57:53 +00:00
|
|
|
exports.run = function(cmd, fork) {
|
2020-11-18 04:13:38 +00:00
|
|
|
return (new ShellObject()).run(cmd, fork);
|
2020-07-07 09:02:51 +00:00
|
|
|
};
|
2020-08-04 09:29:42 +00:00
|
|
|
|
2020-11-18 07:58:01 +00:00
|
|
|
exports.runVisibleWindow = function(cmd, fork) {
|
|
|
|
return (new ShellObject()).setVisibleWindow(true).run(cmd, fork);
|
2020-11-05 08:51:09 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
exports.createProcess = function(cmd, workingDirectory) {
|
|
|
|
if (typeof(workingDirectory) !== "undefined") {
|
|
|
|
console.info("Working directory: " + workingDirectory);
|
|
|
|
}
|
|
|
|
return (new ShellObject()).setWorkingDirectory(workingDirectory).createProcess(cmd);
|
2020-10-26 09:08:53 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 04:13:38 +00:00
|
|
|
exports.runAs = function(FN, args) {
|
|
|
|
return (new ShellObject()).runAs(FN, args);
|
2020-08-09 09:14:05 +00:00
|
|
|
};
|
2020-11-18 04:13:38 +00:00
|
|
|
|
2020-11-18 10:55:06 +00:00
|
|
|
exports.createDesktopIcon = function(name, cmd, workingDirectory) {
|
|
|
|
return (new ShellObject()).createDesktopIcon(name, cmd, workingDirectory);
|
2020-11-18 06:46:34 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 07:58:01 +00:00
|
|
|
exports.VERSIONINFO = "Shell interface (shell.js) version 0.2";
|
2020-11-18 04:13:38 +00:00
|
|
|
exports.global = global;
|
|
|
|
exports.require = global.require;
|