@@ -0,0 +1,69 @@
|
||||
const { execFileSync } = require("child_process");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
function commandExists(command) {
|
||||
try {
|
||||
execFileSync("where.exe", [command], { stdio: "ignore" });
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function run(command, args, options = {}) {
|
||||
return execFileSync(command, args, Object.assign({ stdio: "inherit" }, options));
|
||||
}
|
||||
|
||||
function addToZip(sourceDir, to, password) {
|
||||
fs.mkdirSync(path.dirname(path.resolve(to)), { recursive: true });
|
||||
|
||||
if (password) {
|
||||
if (!commandExists("7z.exe") && !commandExists("7z")) {
|
||||
throw new Error("Windows password zip requires 7z in PATH");
|
||||
}
|
||||
const bin = commandExists("7z.exe") ? "7z.exe" : "7z";
|
||||
run(bin, ["a", "-tzip", `-p${password}`, "-y", path.resolve(to), "."], { cwd: sourceDir, stdio: "inherit" });
|
||||
return;
|
||||
}
|
||||
|
||||
const destination = path.resolve(to).replace(/'/g, "''");
|
||||
const source = path.resolve(sourceDir, "*").replace(/'/g, "''");
|
||||
const update = fs.existsSync(to) ? " -Update" : "";
|
||||
const script = `Compress-Archive -Path '${source}' -DestinationPath '${destination}'${update} -Force`;
|
||||
run("powershell.exe", ["-NoProfile", "-Command", script]);
|
||||
}
|
||||
|
||||
function addToTar(sourceDir, to) {
|
||||
fs.mkdirSync(path.dirname(path.resolve(to)), { recursive: true });
|
||||
const args = fs.existsSync(to) ? ["-rf", path.resolve(to), "."] : ["-cf", path.resolve(to), "."];
|
||||
run("tar.exe", args, { cwd: sourceDir, stdio: "inherit" });
|
||||
}
|
||||
|
||||
function zip(sourceDir, to, params = {}) {
|
||||
if (params.type === "tar") {
|
||||
addToTar(sourceDir, to);
|
||||
} else {
|
||||
addToZip(sourceDir, to, params.password);
|
||||
}
|
||||
}
|
||||
|
||||
function scp(from, to, params = {}) {
|
||||
const source = path.resolve(from);
|
||||
if (!fs.existsSync(source)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (params.pwd) {
|
||||
if (!commandExists("pscp.exe") && !commandExists("pscp")) {
|
||||
throw new Error("Windows scp with pwd requires pscp in PATH");
|
||||
}
|
||||
const bin = commandExists("pscp.exe") ? "pscp.exe" : "pscp";
|
||||
run(bin, ["-pw", params.pwd, "-r", source, to]);
|
||||
return;
|
||||
}
|
||||
|
||||
run("scp.exe", ["-r", source, to]);
|
||||
}
|
||||
|
||||
module.exports = { zip, scp };
|
||||
Reference in New Issue
Block a user