welsonjs/lib/shell.js

228 lines
7.0 KiB
JavaScript
Raw Normal View History

2024-07-10 03:00:55 +00:00
// Windows Shell Interface with WelsonJS Pipe-IPC module
2024-07-10 03:01:05 +00:00
// Namhyeon Go (Catswords Research) <abuse@catswords.net>
2024-07-10 03:00:55 +00:00
// https://github.com/gnh1201/welsonjs
2020-07-21 09:01:38 +00:00
var FILE = require("lib/file");
2023-09-20 03:27:38 +00:00
var PipeIPC = require("lib/pipe-ipc");
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;
2023-09-20 03:27:38 +00:00
this.charset = PipeIPC.CdoUS_ASCII;
2022-09-22 05:35:15 +00:00
2022-05-04 06:47:51 +00:00
this.stdout = null;
this.stderr = null;
2024-07-10 02:32:02 +00:00
this.prefix = null;
2020-11-18 04:13:38 +00:00
this.create = function() {
try {
this.interface = CreateObject("WScript.Shell");
this.currentDirectory = this.interface.CurrentDirectory;
2024-07-10 02:27:02 +00:00
this.workingDirectory = this.currentDirectory;
2020-11-18 04:13:38 +00:00
} 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;
};
2024-07-10 02:27:02 +00:00
2024-07-10 02:32:02 +00:00
this.setPrefix = function(prefix) {
this.prefix = prefix;
2024-07-10 03:03:22 +00:00
return this;
2024-07-10 02:27:02 +00:00
};
2023-09-20 03:27:38 +00:00
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
2020-11-18 04:13:38 +00:00
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) {
2024-07-10 02:27:02 +00:00
var wrap = function(s) {
return this.target != null ? [this.target, s].join(' ') : s;
};
2020-11-18 04:13:38 +00:00
if (typeof(cmd) === "string") {
2024-07-10 02:27:02 +00:00
return wrap(cmd);
2020-11-18 04:13:38 +00:00
} else if (typeof(cmd) === "object") {
2024-07-10 02:27:02 +00:00
return wrap(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) + "\"";
}
2024-07-10 02:27:02 +00:00
}).join(' '));
2020-11-18 04:13:38 +00:00
} else {
2024-07-10 02:27:02 +00:00
return wrap('');
2020-11-18 04:13:38 +00:00
}
};
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) {
2023-09-21 01:59:22 +00:00
var stdout, stderr;
2023-09-20 03:27:38 +00:00
this.stdout = PipeIPC.connect("volatile");
this.stderr = PipeIPC.connect("volatile");
this.stdout.flush();
this.stderr.flush();
2020-11-18 04:13:38 +00:00
2023-09-20 03:27:38 +00:00
if (typeof stdOutPath === "string")
this.stdout.startRecorder(stdOutPath, PipeIPC.ForWriting);
if (typeof stdErrPath === "string")
this.stderr.startRecorder(stdErrPath, PipeIPC.ForWriting);
var c = "%comspec% /c (" + this.build(cmd) + ") 1> " + this.stdout.path;
2021-06-24 19:29:07 +00:00
//c += " 2>&1";
2023-09-20 03:27:38 +00:00
c += " 2> " + this.stderr.path;
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
2023-09-21 06:08:52 +00:00
this.stdout.reload(this.charset);
this.stderr.reload(this.charset);
2021-06-24 19:29:07 +00:00
2023-09-21 09:58:28 +00:00
stdout = this.stdout.read();
stderr = this.stderr.read();
2022-05-04 06:47:29 +00:00
2023-09-21 06:08:52 +00:00
//stdout = this.stdout.read();
//stderr = this.stderr.read();
2021-06-24 19:29:51 +00:00
//console.log("[stdout] " + stdout);
//console.log("[stderr] " + stderr);
2020-11-18 04:13:38 +00:00
2023-09-20 03:27:38 +00:00
this.stdout.destroy();
this.stderr.destroy();
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;
};
2023-10-30 18:13:02 +00:00
this.createShoutcut = function(shoutcutName, cmd) {
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;
2023-10-30 18:13:02 +00:00
link.WorkingDirectory = this.workingDirectory;
2020-11-18 10:55:06 +00:00
//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;
};
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);
};
2023-09-20 03:27:38 +00:00
exports.exec = function(cmd, stdOutPath, stdErrPath) {
return (new ShellObject()).setCharset(PipeIPC.CdoEUC_KR).exec(cmd, stdOutPath, stdErrPath);
2020-11-18 04:13:38 +00:00
};
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") {
2023-10-30 18:13:02 +00:00
console.info("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) {
2023-10-30 18:13:02 +00:00
if (typeof(workingDirectory) !== "undefined") {
console.info("Working directory: " + workingDirectory);
}
return (new ShellObject()).setWorkingDirectory(workingDirectory).createDesktopIcon(name, cmd);
2020-11-18 06:46:34 +00:00
};
2020-11-20 08:44:50 +00:00
exports.getPathOfMyDocuments = function() {
return (new ShellObject()).getPathOfMyDocuments();
};
2023-10-30 18:13:02 +00:00
exports.CdoCharset = PipeIPC.CdoCharset;
2024-07-10 03:03:22 +00:00
exports.VERSIONINFO = "Windows Shell Interface with WelsonJS Pipe-IPC module (shell.js) version 0.3.11";
2023-09-20 03:27:38 +00:00
exports.AUTHOR = "abuse@catswords.net";
2020-11-18 04:13:38 +00:00
exports.global = global;
exports.require = global.require;