mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-29 19:13:49 +01:00
Refactor download.ts (export "download *" command)
Export the "download *" helper to be able to be called from Electron.
This commit is contained in:
parent
5c1504d7e4
commit
588c3e42d7
@ -6,22 +6,8 @@ import { isScriptFilename } from "../../Script/isScriptFilename";
|
|||||||
import FileSaver from "file-saver";
|
import FileSaver from "file-saver";
|
||||||
import JSZip from "jszip";
|
import JSZip from "jszip";
|
||||||
|
|
||||||
export function download(
|
export function exportScripts(pattern: string, server: BaseServer): void {
|
||||||
terminal: ITerminal,
|
const matchEnding = pattern.length == 1 || pattern === "*.*" ? null : pattern.slice(1); // Treat *.* the same as *
|
||||||
router: IRouter,
|
|
||||||
player: IPlayer,
|
|
||||||
server: BaseServer,
|
|
||||||
args: (string | number | boolean)[],
|
|
||||||
): void {
|
|
||||||
try {
|
|
||||||
if (args.length !== 1) {
|
|
||||||
terminal.error("Incorrect usage of download command. Usage: download [script/text file]");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const fn = args[0] + "";
|
|
||||||
// If the parameter starts with *, download all files that match the wildcard pattern
|
|
||||||
if (fn.startsWith("*")) {
|
|
||||||
const matchEnding = fn.length == 1 || fn === "*.*" ? null : fn.slice(1); // Treat *.* the same as *
|
|
||||||
const zip = new JSZip();
|
const zip = new JSZip();
|
||||||
// Helper function to zip any file contents whose name matches the pattern
|
// Helper function to zip any file contents whose name matches the pattern
|
||||||
const zipFiles = (fileNames: string[], fileContents: string[]): void => {
|
const zipFiles = (fileNames: string[], fileContents: string[]): void => {
|
||||||
@ -44,11 +30,34 @@ export function download(
|
|||||||
server.textFiles.map((s) => s.fn),
|
server.textFiles.map((s) => s.fn),
|
||||||
server.textFiles.map((s) => s.text),
|
server.textFiles.map((s) => s.text),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Return an error if no files matched, rather than an empty zip folder
|
// Return an error if no files matched, rather than an empty zip folder
|
||||||
if (Object.keys(zip.files).length == 0) return terminal.error(`No files match the pattern ${fn}`);
|
if (Object.keys(zip.files).length == 0) throw new Error(`No files match the pattern ${pattern}`);
|
||||||
const zipFn = `bitburner${isScriptFilename(fn) ? "Scripts" : fn === "*.txt" ? "Texts" : "Files"}.zip`;
|
const zipFn = `bitburner${isScriptFilename(pattern) ? "Scripts" : pattern === "*.txt" ? "Texts" : "Files"}.zip`;
|
||||||
zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn));
|
zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function download(
|
||||||
|
terminal: ITerminal,
|
||||||
|
router: IRouter,
|
||||||
|
player: IPlayer,
|
||||||
|
server: BaseServer,
|
||||||
|
args: (string | number | boolean)[],
|
||||||
|
): void {
|
||||||
|
try {
|
||||||
|
if (args.length !== 1) {
|
||||||
|
terminal.error("Incorrect usage of download command. Usage: download [script/text file]");
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
const fn = args[0] + "";
|
||||||
|
// If the parameter starts with *, download all files that match the wildcard pattern
|
||||||
|
if (fn.startsWith("*")) {
|
||||||
|
try {
|
||||||
|
exportScripts(fn, server);
|
||||||
|
return;
|
||||||
|
} catch (error: any) {
|
||||||
|
return terminal.error(error.message);
|
||||||
|
}
|
||||||
} else if (isScriptFilename(fn)) {
|
} else if (isScriptFilename(fn)) {
|
||||||
// Download a single script
|
// Download a single script
|
||||||
const script = terminal.getScript(player, fn);
|
const script = terminal.getScript(player, fn);
|
||||||
|
Loading…
Reference in New Issue
Block a user