From 222e42000cf31bce14c056c2d8897ac73acbf3cc Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Sun, 19 Oct 2025 02:04:39 +0700 Subject: [PATCH] CLI: Add --temporary flag to run command (#2354) --- src/NetscriptWorker.ts | 12 +++++----- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 8 ++++++- src/Terminal/HelpText.ts | 23 ++++++++++++------- src/Terminal/commands/run.ts | 2 +- src/Terminal/commands/runScript.ts | 13 ++++++++--- src/Terminal/getTabCompletionPossibilities.ts | 2 +- 6 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index 493f3a3c8..18ee8c1d1 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -266,8 +266,7 @@ export function loadAllRunningScripts(): void { export function createRunningScriptInstance( server: BaseServer, scriptPath: ScriptFilePath, - ramOverride: number | null | undefined, - threads: number, + runOpts: CompleteRunOptions, args: ScriptArg[], ): Result<{ runningScript: RunningScript }> { const script = server.scripts.get(scriptPath); @@ -285,25 +284,26 @@ export function createRunningScriptInstance( }; } - const singleRamUsage = ramOverride ?? script.getRamUsage(server.scripts); + const singleRamUsage = runOpts.ramOverride ?? script.getRamUsage(server.scripts); if (!singleRamUsage) { return { success: false, message: `Cannot calculate RAM usage of ${scriptPath}. Reason: ${script.ramCalculationError}`, }; } - const ramUsage = singleRamUsage * threads; + const ramUsage = singleRamUsage * runOpts.threads; const ramAvailable = server.maxRam - server.ramUsed; if (ramUsage > ramAvailable + 0.001) { return { success: false, - message: `Cannot run ${scriptPath} (t=${threads}) on ${server.hostname}. This script requires ${formatRam( + message: `Cannot run ${scriptPath} (t=${runOpts.threads}) on ${server.hostname}. This script requires ${formatRam( ramUsage, )} of RAM.`, }; } const runningScript = new RunningScript(script, singleRamUsage, args); + runningScript.temporary = runOpts.temporary; return { success: true, runningScript, @@ -320,7 +320,7 @@ export function runScriptFromScript( runOpts: CompleteRunOptions, ): number { // This does not adjust server RAM usage or change any state, so it is safe to call before performing other checks - const result = createRunningScriptInstance(server, scriptPath, runOpts.ramOverride, runOpts.threads, args); + const result = createRunningScriptInstance(server, scriptPath, runOpts, args); if (!result.success) { workerScript.log(caller, () => result.message); return 0; diff --git a/src/ScriptEditor/ui/ScriptEditorRoot.tsx b/src/ScriptEditor/ui/ScriptEditorRoot.tsx index 9c6560443..fb0a0e73b 100644 --- a/src/ScriptEditor/ui/ScriptEditorRoot.tsx +++ b/src/ScriptEditor/ui/ScriptEditorRoot.tsx @@ -48,6 +48,7 @@ import { SpecialServers } from "../../Server/data/SpecialServers"; import { SnackbarEvents } from "../../ui/React/Snackbar"; import { ToastVariant } from "@enums"; import { createRunningScriptInstance, startWorkerScript } from "../../NetscriptWorker"; +import type { PositiveInteger } from "../../types"; // Extend acorn-walk to support TypeScript nodes. extendAcornWalkForTypeScriptNodes(walk.base); @@ -242,7 +243,12 @@ function Root(props: IProps): React.ReactElement { // Always save before doing anything else. await save(); - const result = createRunningScriptInstance(server, currentScript.path, null, 1, []); + const result = createRunningScriptInstance( + server, + currentScript.path, + { threads: 1 as PositiveInteger, temporary: false, preventDuplicates: false }, + [], + ); if (!result.success) { dialogBoxCreate(result.message); return; diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index 2e0e8e83e..4132c6e6e 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -35,7 +35,8 @@ export const TerminalHelpText: string[] = [ " ps Display all scripts that are currently running", " rm [OPTIONS]... [FILE]... Delete a file from the server", " run [script] [-t n] [--tail] Execute a program or script", - " [--ram-override n] [args...]", + " [--ram-override n]", + " [--temporary] [args...]", " scan Prints all immediately-available network connections", " scan-analyze [d] [-a] Prints info for all servers up to d nodes away", " scp [files...] [server] Copies a file to a destination server", @@ -417,17 +418,23 @@ export const HelpTexts: Record = { "Note that if you use rm to remove a file, the contents of the file will be lost. This is irreversible.", ], run: [ - "Usage: run [file name] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [args...]", + "Usage: run [file name] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [--temporary] [args...]", " ", "Execute a program, script or coding contract.", " ", - "The '[-t num_threads]', '[--tail]', `[--ram-override ram_in_GBs]`, and '[args...]' arguments are only valid", - "when running a script. The '-t' flag is used to indicate that the script should be run with the specified", + "The '[-t num_threads]', '[--tail]', `[--ram-override ram_in_GBs]`, [--temporary], and '[args...]' arguments are", + "only valid when running a script. The '-t' flag is used to indicate that the script should be run with the specified", "number of threads. If the flag is omitted, then the script will be run with a single thread by default. The", - "'--tail' flag is used to immediately open a tail window for the script being ran. And the '--ram-override'", - "flag is used to override the amount of ram (per thread) the script is ran with. If the script ends up using", - "more than that amount of ram it will crash. If any of the flags are used, then they MUST come immediately", - "after the script name.", + "'--tail' flag is used to immediately open a tail window for the script being ran. The '--ram-override' flag is used", + "to override the amount of ram (per thread) the script is ran with. If the script ends up using more than that", + "amount of ram it will crash. The '--temporary' flag is used to indicate that the script should be excluded from the", + "save data. You must specify [file name] after 'run' and before any flags. You can use the built-in flags or mix them", + "with your custom flags in any orders.", + " ", + "Note: 'ns.args' only contains custom flags, not built-in flags. You can use '--' to end special args processing, if", + "you want to explicitly pass these built-in flags to your script. For example:", + "- 'run a.js -t 5': Run a.js with 5 threads. 'ns.args' is an empty array.", + "- 'run a.js -- -t 10': Run a.js with 1 thread. 'ns.args' is an array: ['-t', 10].", " ", "[args...] represents a variable number of arguments that will be passed into the script. See the documentation ", "about script arguments. Each specified argument must be separated by a space. ", diff --git a/src/Terminal/commands/run.ts b/src/Terminal/commands/run.ts index b9fc67d84..7d3587432 100644 --- a/src/Terminal/commands/run.ts +++ b/src/Terminal/commands/run.ts @@ -11,7 +11,7 @@ export function run(args: (string | number | boolean)[], server: BaseServer): vo const arg = args.shift(); if (!arg) return Terminal.error( - "Usage: run [program/script] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [args...]", + "Usage: run [program/script] [-t num_threads] [--tail] [--ram-override ram_in_GBs] [--temporary] [args...]", ); const path = Terminal.getFilepath(String(arg)); diff --git a/src/Terminal/commands/runScript.ts b/src/Terminal/commands/runScript.ts index 90aa57723..7b6f37e14 100644 --- a/src/Terminal/commands/runScript.ts +++ b/src/Terminal/commands/runScript.ts @@ -20,12 +20,13 @@ export function runScript( sendDeprecationNotice(); return; } - const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number }; + const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number, "--temporary": Boolean }; let flags: { _: ScriptArg[]; "--tail": boolean; "-t": string; "--ram-override": string; + "--temporary": boolean; }; try { // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment @@ -39,7 +40,7 @@ export function runScript( } const tailFlag = flags["--tail"] === true; const numThreads = parseFloat(flags["-t"] ?? 1); - const ramOverride = flags["--ram-override"] != null ? roundToTwo(parseFloat(flags["--ram-override"])) : null; + const ramOverride = flags["--ram-override"] != null ? roundToTwo(parseFloat(flags["--ram-override"])) : undefined; if (!isPositiveInteger(numThreads)) { return Terminal.error("Invalid number of threads specified. Number of threads must be an integer greater than 0"); } @@ -49,11 +50,17 @@ export function runScript( ); return; } + const tempFlag = flags["--temporary"] === true; // Todo: Switch out arg for something with typescript support const args = flags._; - const result = createRunningScriptInstance(server, scriptPath, ramOverride, numThreads, args); + const result = createRunningScriptInstance( + server, + scriptPath, + { threads: numThreads, temporary: tempFlag, ramOverride, preventDuplicates: false }, + args, + ); if (!result.success) { Terminal.error(result.message); return; diff --git a/src/Terminal/getTabCompletionPossibilities.ts b/src/Terminal/getTabCompletionPossibilities.ts index c3f518333..be2cb5a29 100644 --- a/src/Terminal/getTabCompletionPossibilities.ts +++ b/src/Terminal/getTabCompletionPossibilities.ts @@ -294,7 +294,7 @@ export async function getTabCompletionPossibilities(terminalText: string, baseDi return ["--tail"]; } - const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number }; + const runArgs = { "--tail": Boolean, "-t": Number, "--ram-override": Number, "--temporary": Boolean }; let flags = { _: [], };