From 588c3e42d736283f9c27775a0a36cccff6c16293 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Tue, 18 Jan 2022 11:43:03 -0500 Subject: [PATCH] Refactor download.ts (export "download *" command) Export the "download *" helper to be able to be called from Electron. --- src/Terminal/commands/download.ts | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/Terminal/commands/download.ts b/src/Terminal/commands/download.ts index fa392d444..5d79f2dba 100644 --- a/src/Terminal/commands/download.ts +++ b/src/Terminal/commands/download.ts @@ -6,6 +6,37 @@ import { isScriptFilename } from "../../Script/isScriptFilename"; import FileSaver from "file-saver"; import JSZip from "jszip"; +export function exportScripts(pattern: string, server: BaseServer): void { + const matchEnding = pattern.length == 1 || pattern === "*.*" ? null : pattern.slice(1); // Treat *.* the same as * + const zip = new JSZip(); + // Helper function to zip any file contents whose name matches the pattern + const zipFiles = (fileNames: string[], fileContents: string[]): void => { + for (let i = 0; i < fileContents.length; ++i) { + let name = fileNames[i]; + if (name.startsWith("/")) name = name.slice(1); + if (!matchEnding || name.endsWith(matchEnding)) + zip.file(name, new Blob([fileContents[i]], { type: "text/plain" })); + } + }; + // In the case of script files, we pull from the server.scripts array + if (!matchEnding || isScriptFilename(matchEnding)) + zipFiles( + server.scripts.map((s) => s.filename), + server.scripts.map((s) => s.code), + ); + // In the case of text files, we pull from the server.scripts array + if (!matchEnding || matchEnding.endsWith(".txt")) + zipFiles( + server.textFiles.map((s) => s.fn), + server.textFiles.map((s) => s.text), + ); + + // Return an error if no files matched, rather than an empty zip folder + if (Object.keys(zip.files).length == 0) throw new Error(`No files match the pattern ${pattern}`); + const zipFn = `bitburner${isScriptFilename(pattern) ? "Scripts" : pattern === "*.txt" ? "Texts" : "Files"}.zip`; + zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn)); +} + export function download( terminal: ITerminal, router: IRouter, @@ -21,34 +52,12 @@ export function download( 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(); - // Helper function to zip any file contents whose name matches the pattern - const zipFiles = (fileNames: string[], fileContents: string[]): void => { - for (let i = 0; i < fileContents.length; ++i) { - let name = fileNames[i]; - if (name.startsWith("/")) name = name.slice(1); - if (!matchEnding || name.endsWith(matchEnding)) - zip.file(name, new Blob([fileContents[i]], { type: "text/plain" })); - } - }; - // In the case of script files, we pull from the server.scripts array - if (!matchEnding || isScriptFilename(matchEnding)) - zipFiles( - server.scripts.map((s) => s.filename), - server.scripts.map((s) => s.code), - ); - // In the case of text files, we pull from the server.scripts array - if (!matchEnding || matchEnding.endsWith(".txt")) - zipFiles( - server.textFiles.map((s) => s.fn), - server.textFiles.map((s) => s.text), - ); - // 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}`); - const zipFn = `bitburner${isScriptFilename(fn) ? "Scripts" : fn === "*.txt" ? "Texts" : "Files"}.zip`; - zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn)); - return; + try { + exportScripts(fn, server); + return; + } catch (error: any) { + return terminal.error(error.message); + } } else if (isScriptFilename(fn)) { // Download a single script const script = terminal.getScript(player, fn);