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