welsonjs/lib/shell.js

168 lines
5.1 KiB
JavaScript
Raw Normal View History

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) {
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;
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;
};
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);
this.interface.Run(c, 0, !fork);
};
this.runWindow = function(cmd, fork) {
var fork = (typeof(fork) !== "undefined") ? fork : true;
var c = "%comspec% /q /c (" + this.build(cmd) + ")";
console.info("ShellObject.runWindow() -> " + c);
this.interface.Run(c, 1, !fork);
};
this.runAs = function(FN, args) {
var c = FN + " " + args.join(' ');
var oShell = CreateObject("Shell.Application");
console.info("ShellObject.runAs() -> " + c);
oShell.shellExecute(FN, args, null, "runas", 0);
return oShell;
};
2020-11-18 06:46:34 +00:00
this.createDesktopIcon = function(filename, workingDirectory) {
var desktopPath = this.interface.SpecialFolders("Desktop");
var link = this.interface.CreateShortcut(desktopPath + "\\" + name + ".lnk");
link.IconLocation = filename + ",1";
link.TargetPath = workingDirectory + "\\" + filename;
link.WindowStyle = 3;
link.WorkingDirectory = workingDirectory;
link.Save();
};
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
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-05 08:51:09 +00:00
exports.runWindow = function(cmd, fork) {
2020-11-18 04:13:38 +00:00
return (new ShellObject()).runWindow(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 06:46:34 +00:00
exports.createDesktopIcon = function(filename, workingDirectory) {
return (new ShellObject()).createDesktopIcon(filename, workingDirectory);
};
2020-11-18 04:13:38 +00:00
exports.VERSIONINFO = "Shell Lib (shell.js) version 0.1";
exports.global = global;
exports.require = global.require;