From 4d47785f28f0155751e1430d0edca8bfdcfb13a1 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Tue, 23 Aug 2022 22:06:05 -0400 Subject: [PATCH] Fix ns documentation for scp and write --- src/NetscriptFunctions.ts | 59 ++++++++++------------ src/ScriptEditor/NetscriptDefinitions.d.ts | 28 +++++----- 2 files changed, 41 insertions(+), 46 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index b1c102245..686526e89 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -1465,47 +1465,42 @@ const base: InternalAPI = { }, write: (ctx: NetscriptContext) => - (_port: unknown, _data: unknown = "", _mode: unknown = "a"): void => { - const port = helpers.string(ctx, "port", _port); + (_handle: unknown, _data: unknown = "", _mode: unknown = "a"): void => { + let fn = helpers.string(ctx, "handle", _handle); const data = helpers.string(ctx, "data", _data); const mode = helpers.string(ctx, "mode", _mode); - if (isString(port)) { - // Write to script or text file - let fn = port; - if (!isValidFilePath(fn)) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filepath: ${fn}`); + if (!isValidFilePath(fn)) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filepath: ${fn}`); - if (fn.lastIndexOf("/") === 0) fn = removeLeadingSlash(fn); + if (fn.lastIndexOf("/") === 0) fn = removeLeadingSlash(fn); - const server = helpers.getServer(ctx, ctx.workerScript.hostname); + const server = helpers.getServer(ctx, ctx.workerScript.hostname); - if (isScriptFilename(fn)) { - // Write to script - let script = ctx.workerScript.getScriptOnServer(fn, server); - if (script == null) { - // Create a new script - script = new Script(Player, fn, String(data), server.hostname, server.scripts); - server.scripts.push(script); - return script.updateRamUsage(Player, server.scripts); - } - mode === "w" ? (script.code = String(data)) : (script.code += data); + if (isScriptFilename(fn)) { + // Write to script + let script = ctx.workerScript.getScriptOnServer(fn, server); + if (script == null) { + // Create a new script + script = new Script(Player, fn, String(data), server.hostname, server.scripts); + server.scripts.push(script); return script.updateRamUsage(Player, server.scripts); - } else { - // Write to text file - const txtFile = getTextFile(fn, server); - if (txtFile == null) { - createTextFile(fn, String(data), server); - return; - } - if (mode === "w") { - txtFile.write(String(data)); - } else { - txtFile.append(String(data)); - } } - return; + mode === "w" ? (script.code = String(data)) : (script.code += data); + return script.updateRamUsage(Player, server.scripts); } else { - throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: ${port}`); + // Write to text file + if (!fn.endsWith(".txt")) throw helpers.makeRuntimeErrorMsg(ctx, `Invalid filename: ${fn}`); + const txtFile = getTextFile(fn, server); + if (txtFile == null) { + createTextFile(fn, String(data), server); + return; + } + if (mode === "w") { + txtFile.write(String(data)); + } else { + txtFile.append(String(data)); + } } + return; }, tryWritePort: (ctx: NetscriptContext) => diff --git a/src/ScriptEditor/NetscriptDefinitions.d.ts b/src/ScriptEditor/NetscriptDefinitions.d.ts index bc5be7694..5d9f0082d 100644 --- a/src/ScriptEditor/NetscriptDefinitions.d.ts +++ b/src/ScriptEditor/NetscriptDefinitions.d.ts @@ -5413,25 +5413,25 @@ export interface NS { * ```ts * // NS2: * //Copies foo.lit from the helios server to the home computer: - * await ns.scp("foo.lit", "home", "helios" ); + * ns.scp("foo.lit", "home", "helios" ); * * //Tries to copy three files from rothman-uni to home computer: * files = ["foo1.lit", "foo2.script", "foo3.script"]; - * await ns.scp(files, "home", "rothman-uni"); + * ns.scp(files, "home", "rothman-uni"); * ``` * @example * ```ts * //ns2, copies files from home to a target server * const server = ns.args[0]; * const files = ["hack.js","weaken.js","grow.js"]; - * await ns.scp(files, server, "home"); + * ns.scp(files, server, "home"); * ``` * @param files - Filename or an array of filenames of script/literature files to copy. - * @param source - Host of the source server, which is the server from which the file will be copied. This argument is optional and if it’s omitted the source will be the current server. * @param destination - Host of the destination server, which is the server to which the file will be copied. - * @returns True if the script/literature file is successfully copied over and false otherwise. If the files argument is an array then this function will return true if at least one of the files in the array is successfully copied. + * @param source - Host of the source server, which is the server from which the file will be copied. This argument is optional and if it’s omitted the source will be the current server. + * @returns True if the file is successfully copied over and false otherwise. If the files argument is an array then this function will return false if any of the operations failed. */ - scp(files: string | string[], destination: string, source?: string): Promise; + scp(files: string | string[], destination: string, source?: string): boolean; /** * List files on a server. @@ -5971,20 +5971,20 @@ export interface NS { * @remarks * RAM cost: 0 GB * - * This function can be used to write data to a text file (.txt). + * This function can be used to write data to a text file (.txt) or a script (.js or .script). * - * This function will write data to that text file. If the specified text file does not exist, + * This function will write data to that file. If the specified file does not exist, * then it will be created. The third argument mode, defines how the data will be written to - * the text file. If *mode is set to “w”, then the data is written in “write” mode which means - * that it will overwrite all existing data on the text file. If mode is set to any other value + * the file. If *mode is set to “w”, then the data is written in “write” mode which means + * that it will overwrite all existing data on the file. If mode is set to any other value * then the data will be written in “append” mode which means that the data will be added at the - * end of the text file. + * end of the file. * - * @param handle - Filename of the text file that will be written to. + * @param handle - Filename to be written to. * @param data - Data to write. - * @param mode - Defines the write mode. Only valid when writing to text files. + * @param mode - Defines the write mode. */ - write(handle: string, data?: string[] | number | string, mode?: "w" | "a"): Promise; + write(handle: string, data?: string[] | number | string, mode?: "w" | "a"): void; /** * Attempt to write to a port.