diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index f2bfb3b0d..27b8591ea 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -36,7 +36,7 @@ import {StockMarket, StockSymbols, SymbolToStockMap, initStockSymbols, Stock, shortStock, sellShort, OrderTypes, PositionTypes, placeOrder, cancelOrder} from "./StockMarket.js"; import {post} from "./Terminal.js"; -import {TextFile, getTextFile, createTextFile} from "./TextFile.js"; +import {TextFile, getTextFile, createTextFile} from "./TextFile"; import {WorkerScript, workerScripts, killWorkerScript, NetscriptPorts} from "./NetscriptWorker.js"; diff --git a/src/Script.js b/src/Script.js index 53d03e3c4..a98b49939 100644 --- a/src/Script.js +++ b/src/Script.js @@ -29,7 +29,7 @@ import {Player} from "./Player.js"; import {AllServers, processSingleServerGrowth} from "./Server.js"; import {Settings} from "./Settings.js"; import {post, Terminal} from "./Terminal.js"; -import {TextFile} from "./TextFile.js"; +import {TextFile} from "./TextFile"; import {parse, Node} from "../utils/acorn.js"; import {dialogBoxCreate} from "../utils/DialogBox.js"; diff --git a/src/Terminal.js b/src/Terminal.js index ae341a64e..91d49676b 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -29,8 +29,7 @@ import {AllServers, GetServerByHostname, import {Settings} from "./Settings.js"; import {SpecialServerIps, SpecialServerNames} from "./SpecialServerIps.js"; -import {TextFile, getTextFile, - createTextFile} from "./TextFile.js"; +import {TextFile, getTextFile} from "./TextFile"; import {containsAllStrings, longestCommonStart, formatNumber, isString} from "../utils/StringHelperFunctions.js"; diff --git a/src/TextFile.js b/src/TextFile.js deleted file mode 100644 index 711bc2422..000000000 --- a/src/TextFile.js +++ /dev/null @@ -1,90 +0,0 @@ -import {Server} from "./Server.js"; -import {dialogBoxCreate} from "../utils/DialogBox.js"; -import {Reviver, Generic_toJSON, - Generic_fromJSON} from "../utils/JSONReviver.js"; - -function TextFile(fn="", txt="") { - this.fn = fn.endsWith(".txt") ? fn : fn + ".txt"; - this.fn = this.fn.replace(/\s+/g, ''); - this.text = String(txt); -} - -TextFile.prototype.append = function(txt) { - this.text += String(txt); -} - -TextFile.prototype.write = function(txt) { - this.text = String(txt); -} - -TextFile.prototype.read = function() { - return this.txt; -} - -TextFile.prototype.show = function() { - dialogBoxCreate(this.fn + "

" + this.text, true); -} - -TextFile.prototype.download = function() { - var filename = this.fn; - var file = new Blob([this.text], {type: 'text/plain'}); - if (window.navigator.msSaveOrOpenBlob) {// IE10+ - window.navigator.msSaveOrOpenBlob(file, filename); - } else { // Others - var a = document.createElement("a"), - url = URL.createObjectURL(file); - a.href = url; - a.download = this.fn; - document.body.appendChild(a); - a.click(); - setTimeout(function() { - document.body.removeChild(a); - window.URL.revokeObjectURL(url); - }, 0); - } -} - -TextFile.prototype.toJSON = function() { - return Generic_toJSON("TextFile", this); -} - -TextFile.fromJSON = function(value) { - return Generic_fromJSON(TextFile, value.data); -} - -Reviver.constructors.TextFile = TextFile; - -function getTextFile(fn, server) { - if (!fn.endsWith(".txt")) {fn += ".txt";} - for (var i = 0; i < server.textFiles.length; ++i) { - if (server.textFiles[i].fn === fn) { - return server.textFiles[i]; - } - } - return null; -} - -//Returns the TextFile object that was just created -function createTextFile(fn, txt, server) { - if (getTextFile(fn, server) !== null) { - console.log("ERROR: createTextFile failed because the specified " + - "server already has a text file with the same fn"); - return; - } - var file = new TextFile(fn, txt); - server.textFiles.push(file); - return file; -} - -function deleteTextFile(fn, server) { - if (!fn.endsWith(".txt")) {fn += ".txt";} - for (var i = 0; i < server.textFiles.length; ++i) { - if (server.textFiles[i].fn === fn) { - server.textFiles.splice(i, 1); - return true; - } - } - return false; -} - -export {TextFile, getTextFile, createTextFile}; diff --git a/src/TextFile.ts b/src/TextFile.ts new file mode 100644 index 000000000..8405aded7 --- /dev/null +++ b/src/TextFile.ts @@ -0,0 +1,103 @@ +import {dialogBoxCreate} from "../utils/DialogBox"; +import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; + +export class TextFile { + fn: string; + text: string; + + constructor(fn = "", txt = "") { + this.fn = (fn.endsWith(".txt") ? fn : `${fn}.txt`).replace(/\s+/g, ''); + this.text = txt; + } + + append(txt: string) { + this.text += txt; + } + + write(txt: string) { + this.text = txt; + } + + read() { + return this.text; + } + + show() { + dialogBoxCreate(`${this.fn}

${this.text}`, true); + } + + download() { + const filename = this.fn; + const file = new Blob([ this.text ], { type: 'text/plain' }); + if (window.navigator.msSaveOrOpenBlob) { + // IE10+ + window.navigator.msSaveOrOpenBlob(file, filename); + } else { + // Others + const a = document.createElement("a"); + const url = URL.createObjectURL(file); + a.href = url; + a.download = this.fn; + document.body.appendChild(a); + a.click(); + setTimeout(() => { + document.body.removeChild(a); + window.URL.revokeObjectURL(url); + }, 0); + } + } + + toJSON() { + return Generic_toJSON("TextFile", this); + } + + static fromJSON(value: any) { + return Generic_fromJSON(TextFile, value.data); + } +} + +Reviver.constructors.TextFile = TextFile; + +export function getTextFile(fn: string, server: any): string | null { + if (!fn.endsWith(".txt")) { + fn += ".txt"; + } + + for (let i = 0; i < server.textFiles.length; i++) { + if (server.textFiles[i].fn === fn) { + return server.textFiles[i]; + } + } + + return null; +} + +/** + * Creates a TextFile on the target server. + * @param {string} fn The file name to create. + * @param {string} txt The contents of the file. + * @param {*} server The server that the file should be created on. + * @returns {TextFile} The instance of the file. + */ +export function createTextFile(fn: string, txt: string, server: any): TextFile { + if (getTextFile(fn, server) !== null) { + console.error(`A file named "${fn}" already exists on server ${server.hostname}.`); + return; + } + const file = new TextFile(fn, txt); + server.textFiles.push(file); + return file; +} + +function deleteTextFile(fn, server) { + if (!fn.endsWith(".txt")) { + fn += ".txt"; + } + for (var i = 0; i < server.textFiles.length; ++i) { + if (server.textFiles[i].fn === fn) { + server.textFiles.splice(i, 1); + return true; + } + } + return false; +} diff --git a/tsconfig.json b/tsconfig.json index da5e37315..26bb34a1a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "module": "commonjs", - "target": "es5", + "target": "es6", "sourceMap": true }, "exclude": [