Update shell.js

This commit is contained in:
Namhyeon Go 2023-09-20 12:27:38 +09:00 committed by GitHub
parent b60ef15c91
commit 01cfdb0f72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,8 @@
////////////////////////////////////////////////////////////////////////
// Shell API
// Shell API with PIPE-IPC
////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file");
var RAND = require("lib/rand");
var SEED = RAND.getSeed();
var PipeIPC = require("lib/pipe-ipc");
var ShellObject = function() {
this.interface = null;
@ -14,7 +12,7 @@ var ShellObject = function() {
this.isElevated = false;
this.isFork = false;
this.isVisibleWindow = false;
this.charset = "utf-8";
this.charset = PipeIPC.CdoUS_ASCII;
this.stdout = null;
this.stderr = null;
@ -28,6 +26,12 @@ var ShellObject = function() {
}
return this;
};
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.setWorkingDirectory = function(dirname) {
if (typeof(dirname) === "string") {
@ -72,34 +76,36 @@ var ShellObject = function() {
};
this.exec = function(cmd, stdOutPath, stdErrPath) {
var stdout, stderr;
var stdOutPath = (typeof(stdOutPath) === "undefined" ? "tmp\\stdout_" + SEED + ".txt" : stdOutPath);
var stdErrPath = (typeof(stdErrPath) === "undefined" ? "tmp\\stderr_" + SEED + ".txt" : stdErrPath);
this.stdout = PipeIPC.connect("volatile");
this.stderr = PipeIPC.connect("volatile");
var c = "%comspec% /c (" + this.build(cmd) + ") 1> " + stdOutPath;
this.stdout.flush();
this.stderr.flush();
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;
//c += " 2>&1";
c += " 2> " + stdErrPath;
c += " 2> " + this.stderr.path;
this.interface.Run(c, 0, true);
console.log("ShellObject.exec() ->", c);
sleep(1);
if (FILE.fileExists(stdOutPath)) {
stdout = FILE.readFile(stdOutPath, this.charset);
FILE.deleteFile(stdOutPath);
}
this.stdout.loadFromFile(this.stdout.path, this.charset);
this.stderr.loadFromFile(this.stderr.path, this.charset);
if (FILE.fileExists(stdErrPath)) {
stderr = FILE.readFile(stdErrPath, this.charset);
FILE.deleteFile(stdErrPath);
}
var stdout = this.stdout.read();
var stderr = this.stderr.read();
this.stdout = stdout;
this.stderr = stderr;
console.log(c);
//console.log("[stdout] " + stdout);
//console.log("[stderr] " + stderr);
this.stdout.destroy();
this.stderr.destroy();
return stdout;
};
@ -150,11 +156,6 @@ var ShellObject = function() {
this.interface = null;
};
this.setCharset = function(charset) {
this.charset = charset;
return this;
};
this.create();
};
@ -166,8 +167,8 @@ exports.build = function(cmd) {
return (new ShellObject()).build(cmd);
};
exports.exec = function(cmd, stdOutPath) {
return (new ShellObject()).exec(cmd, stdOutPath);
exports.exec = function(cmd, stdOutPath, stdErrPath) {
return (new ShellObject()).setCharset(PipeIPC.CdoEUC_KR).exec(cmd, stdOutPath, stdErrPath);
};
exports.run = function(cmd, fork) {
@ -201,6 +202,7 @@ exports.getPathOfMyDocuments = function() {
return (new ShellObject()).getPathOfMyDocuments();
};
exports.VERSIONINFO = "Shell interface (shell.js) version 0.3.1";
exports.VERSIONINFO = "Shell interface (shell.js) version 0.3.2";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;