welsonjs/lib/shell.js

207 lines
6.3 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");
2022-06-28 04:01:19 +00:00
var RAND = require("lib/rand");
var SEED = RAND.getSeed();
2020-07-07 09:02:51 +00:00
2020-11-18 04:13:38 +00:00
var ShellObject = function() {
this.interface = null;
2022-09-22 05:35:15 +00:00
2020-11-18 04:13:38 +00:00
this.currentDirectory = null;
this.workingDirectory = null;
this.isElevated = false;
this.isFork = false;
2020-11-18 07:58:01 +00:00
this.isVisibleWindow = false;
2022-09-22 05:35:15 +00:00
this.charset = "utf-8";
2022-05-04 06:47:51 +00:00
this.stdout = null;
this.stderr = null;
2020-11-18 04:13:38 +00:00
this.create = function() {
try {
this.interface = CreateObject("WScript.Shell");
this.currentDirectory = this.interface.CurrentDirectory;
} catch (e) {
2021-08-11 07:29:49 +00:00
console.error("ShellObject.create() ->", e.message);
2020-11-18 04:13:38 +00:00
}
return this;
};
this.setWorkingDirectory = function(dirname) {
if (typeof(dirname) === "string") {
this.workingDirectory = dirname;
this.interface.CurrentDirectory = this.workingDirectory;
2022-02-10 06:25:19 +00:00
console.log("ShellObject.workingDirectory ->", this.workingDirectory);
2020-11-18 04:13:38 +00:00
}
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) {
2022-01-13 08:46:28 +00:00
if (s == '') {
return "''";
2022-03-29 06:22:40 +00:00
} else if (!/[ "=]/g.test(s)) {
2020-11-18 04:13:38 +00:00
return s;
} else {
return "\"" + addslashes(s) + "\"";
}
}).join(' ');
} else {
return "";
}
};
this.createProcess = function(cmd) {
try {
var c = this.build(cmd);
2022-02-10 06:25:19 +00:00
console.log("ShellObject.createProcess() ->", c);
2020-11-18 04:13:38 +00:00
return this.interface.Exec(c);
} catch (e) {
2021-08-11 07:29:49 +00:00
console.error("ShellObject.createProcess() ->", e.message);
2020-11-18 04:13:38 +00:00
}
};
2021-06-24 19:29:07 +00:00
this.exec = function(cmd, stdOutPath, stdErrPath) {
var stdout, stderr;
2022-06-28 04:01:19 +00:00
var stdOutPath = (typeof(stdOutPath) === "undefined" ? "tmp\\stdout_" + SEED + ".txt" : stdOutPath);
var stdErrPath = (typeof(stdErrPath) === "undefined" ? "tmp\\stderr_" + SEED + ".txt" : stdErrPath);
2020-11-18 04:13:38 +00:00
var c = "%comspec% /c (" + this.build(cmd) + ") 1> " + stdOutPath;
2021-06-24 19:29:07 +00:00
//c += " 2>&1";
2021-06-24 19:29:51 +00:00
c += " 2> " + stdErrPath;
2020-11-18 04:13:38 +00:00
this.interface.Run(c, 0, true);
2022-02-10 06:25:19 +00:00
console.log("ShellObject.exec() ->", c);
2021-06-24 19:29:51 +00:00
sleep(1);
2021-06-24 19:29:07 +00:00
2021-06-24 19:29:51 +00:00
if (FILE.fileExists(stdOutPath)) {
2022-09-22 05:35:15 +00:00
stdout = FILE.readFile(stdOutPath, this.charset);
2021-06-24 19:29:51 +00:00
FILE.deleteFile(stdOutPath);
}
2021-06-24 19:29:07 +00:00
2021-06-24 19:29:51 +00:00
if (FILE.fileExists(stdErrPath)) {
2022-09-22 05:35:15 +00:00
stderr = FILE.readFile(stdErrPath, this.charset);
2021-06-24 19:29:51 +00:00
FILE.deleteFile(stdErrPath);
}
2021-06-24 19:29:07 +00:00
2022-05-04 06:47:29 +00:00
this.stdout = stdout;
this.stderr = stderr;
console.log(c);
2021-06-24 19:29:51 +00:00
//console.log("[stdout] " + stdout);
//console.log("[stderr] " + stderr);
2020-11-18 04:13:38 +00:00
2021-06-24 19:29:07 +00:00
return stdout;
2020-11-18 04:13:38 +00:00
};
this.run = function(cmd, fork) {
var fork = (typeof(fork) !== "undefined") ? fork : true;
var c = "%comspec% /q /c (" + this.build(cmd) + ")";
2022-02-10 06:25:19 +00:00
console.log("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-20 08:44:50 +00:00
var _args = null;
2022-02-10 06:25:19 +00:00
console.log("ShellObject.runAs() ->", FN);
2020-11-20 08:44:50 +00:00
if (typeof(args) !== "undefined") {
_args = args.join(' ');
}
oShell.shellExecute(FN, _args, this.workingDirectory, "runas", (!this.isVisibleWindow ? 0 : 1));
2020-11-18 04:13:38 +00:00
return oShell;
};
2021-08-10 18:03:57 +00:00
this.createShoutcut = function(shoutcutName, cmd, workingDirectory) {
2020-11-18 06:46:34 +00:00
var desktopPath = this.interface.SpecialFolders("Desktop");
2021-08-10 18:03:57 +00:00
var path = desktopPath + "\\" + shoutcutName + ".lnk";
2020-11-18 10:55:06 +00:00
if (!FILE.fileExists(path)) {
var link = this.interface.CreateShortcut(path);
2020-12-08 06:43:59 +00:00
//link.TargetPath = "cmd";
//link.Arguments = "/q /c " + this.build(cmd);
link.TargetPath = "wscript";
link.Arguments = "bgloader.js " + this.build(cmd);
2021-08-10 18:03:57 +00:00
//link.Arguments = this.build(cmd);
2020-12-08 06:43:59 +00:00
link.WindowStyle = 1;
2020-11-18 10:55:06 +00:00
link.WorkingDirectory = workingDirectory;
//link.Hotkey = "";
2020-12-08 06:43:59 +00:00
link.IconLocation = require("lib/system").getCurrentScriptDirectory() + "\\app\\favicon.ico";
2020-11-18 10:55:06 +00:00
link.Save();
}
2020-11-18 06:46:34 +00:00
};
2020-11-20 08:44:50 +00:00
this.getPathOfMyDocuments = function() {
return this.interface.SpecialFolders("MyDocuments");
};
2020-11-18 04:13:38 +00:00
this.release = function() {
2022-02-10 06:25:19 +00:00
console.log("ShellObject.release() ->", this.currentDirectory);
2020-11-18 04:13:38 +00:00
this.interface.CurrentDirectory = this.currentDirectory;
this.interface = null;
};
2022-09-22 05:35:15 +00:00
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
2020-11-18 04:13:38 +00:00
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
2022-02-28 10:33:54 +00:00
exports.show = function(cmd, fork) {
2020-11-18 07:58:01 +00:00
return (new ShellObject()).setVisibleWindow(true).run(cmd, fork);
2022-02-28 10:33:54 +00:00
};
exports.runAs = function(FN, args) {
return (new ShellObject()).runAs(FN, args);
};
exports.showAs = function(FN, args) {
return (new ShellObject()).setVisibleWindow(true).runAs(FN, args);
};
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") {
2022-02-10 06:25:19 +00:00
console.log("Working directory: " + workingDirectory);
2020-11-18 04:13:38 +00:00
}
return (new ShellObject()).setWorkingDirectory(workingDirectory).createProcess(cmd);
2020-10-26 09:08:53 +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-20 08:44:50 +00:00
exports.getPathOfMyDocuments = function() {
return (new ShellObject()).getPathOfMyDocuments();
};
2022-09-22 05:35:15 +00:00
exports.VERSIONINFO = "Shell interface (shell.js) version 0.3.1";
2020-11-18 04:13:38 +00:00
exports.global = global;
exports.require = global.require;