mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 06:18:42 +02:00
CLI: Add --temporary flag to run command (#2354)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<string, string[]> = {
|
||||
"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. ",
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 = {
|
||||
_: [],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user