Update powershell.js

This commit is contained in:
Namhyeon Go 2023-03-10 11:36:36 +09:00 committed by GitHub
parent f5833dd103
commit aed2ab6e04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,33 +4,38 @@
var SHELL = require("lib/shell"); var SHELL = require("lib/shell");
function PowershellInterface() { var PowershellObject = function() {
this.execType = "ps1"; this.execType = "ps1";
this.dataType = -1; this.dataType = -1;
this.target = null; this.target = null;
this.setExecType = function(execType) { this.setExecType = function(execType) {
this.execType = execType; this.execType = execType;
return this;
}; };
this.load = function(script) { this.load = function(script) {
this.target = script; this.target = script;
this.dataType = 0; this.dataType = 0;
return this;
}; };
this.loadCommand = function(command) { this.loadCommand = function(command) {
this.target = command; this.target = command;
this.dataType = 1; this.dataType = 1;
return this;
}; };
this.loadFile = function(filename) { this.loadFile = function(filename) {
this.target = filename; this.target = filename;
this.dataType = 2; this.dataType = 2;
return this;
}; };
this.loadUrl = function(url) { this.loadUrl = function(url) {
this.target = url; this.target = url;
this.dataType = 3; this.dataType = 3;
return this;
}; };
// For example: // For example:
@ -60,6 +65,7 @@ function PowershellInterface() {
default: default:
console.error("Invalid scheme"); console.error("Invalid scheme");
} }
return this;
}; };
this.exec = function(args) { this.exec = function(args) {
@ -79,7 +85,8 @@ function PowershellInterface() {
]; ];
switch (this.dataType) { switch (this.dataType) {
case 3: // dataType: URL(3) case 3: // dataType: URL(3)\
// todo
break; break;
case 2: // dataType: file(2) case 2: // dataType: file(2)
@ -90,9 +97,9 @@ function PowershellInterface() {
case 1: // dataType: command(1) case 1: // dataType: command(1)
cmd.push("-Command"); cmd.push("-Command");
if (typeof this.target === "string") { if (typeof this.target === "string") {
cmd.push("& {" + cmd + "}"); cmd.push("& {" + this.target + "}");
} else { } else {
cmd.push("& {" + SHELL.build(cmd) + "}"); cmd.push("& {" + SHELL.build(this.target) + "}");
} }
break; break;
@ -114,24 +121,24 @@ function PowershellInterface() {
this.runAs = function() { this.runAs = function() {
if (this.execType != "ps1") { if (this.execType != "ps1") {
console.warn("The execType is not set 'ps1'. Will be forward it to the default shell."); console.warn("The execType is not set 'ps1'. Will be forward it to the default shell.");
return execCommand("Start-Process cmd \"/q /c " + SHELL.addslashes(SHELL.build(this.target)) + "\" -Verb RunAs"); return this.exec("Start-Process cmd \"/q /c " + SHELL.addslashes(SHELL.build(this.target)) + "\" -Verb RunAs");
} else { } else {
return execCommand("Start-Process cmd \"/q /c " + SHELL.addslashes(SHELL.build(cmd)) + "\" -Verb RunAs"); return this.exec("Start-Process cmd \"/q /c " + SHELL.addslashes(SHELL.build(cmd)) + "\" -Verb RunAs");
} }
}; };
} }
function execScript(scriptName, args) { function execScript(scriptName, args) {
return (new PowershellInterface()).loadFile(scriptName).exec(args); return (new PowershellObject()).loadFile(scriptName).exec(args);
}; };
function execCommand(cmd) { function execCommand(cmd) {
return (new PowershellInterface()).loadCommand(cmd).exec(); return (new PowershellObject()).loadCommand(cmd).exec();
}; };
function runAs(cmd) { function runAs(cmd) {
return (new PowershellInterface()).setExecType("cmd").runAs(); return (new PowershellObject()).setExecType("cmd").runAs();
}; };
exports.execScript = execScript; exports.execScript = execScript;