2021-09-16 01:50:44 +02:00
|
|
|
import { ITerminal } from "../ITerminal";
|
2021-09-17 08:58:02 +02:00
|
|
|
import { IRouter } from "../../ui/Router";
|
2021-09-16 01:50:44 +02:00
|
|
|
import { IPlayer } from "../../PersonObjects/IPlayer";
|
|
|
|
import { BaseServer } from "../../Server/BaseServer";
|
2021-09-24 22:34:21 +02:00
|
|
|
import { isScriptFilename } from "../../Script/isScriptFilename";
|
2021-09-16 01:50:44 +02:00
|
|
|
|
|
|
|
export function nano(
|
|
|
|
terminal: ITerminal,
|
2021-09-17 08:58:02 +02:00
|
|
|
router: IRouter,
|
2021-09-16 01:50:44 +02:00
|
|
|
player: IPlayer,
|
|
|
|
server: BaseServer,
|
|
|
|
args: (string | number)[],
|
|
|
|
): void {
|
|
|
|
if (args.length !== 1) {
|
|
|
|
terminal.error("Incorrect usage of nano command. Usage: nano [scriptname]");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const filename = args[0] + "";
|
2021-09-24 00:47:43 +02:00
|
|
|
if (isScriptFilename(filename)) {
|
2021-09-16 01:50:44 +02:00
|
|
|
const filepath = terminal.getFilepath(filename);
|
|
|
|
const script = terminal.getScript(player, filename);
|
|
|
|
if (script == null) {
|
|
|
|
let code = "";
|
|
|
|
if (filename.endsWith(".ns") || filename.endsWith(".js")) {
|
2021-10-10 05:07:18 +02:00
|
|
|
code = `/**
|
|
|
|
* @param {NS} ns
|
2021-10-10 05:21:22 +02:00
|
|
|
**/
|
2021-10-10 05:07:18 +02:00
|
|
|
export async function main(ns) {
|
2021-09-16 01:50:44 +02:00
|
|
|
|
|
|
|
}`;
|
|
|
|
}
|
2021-09-17 08:58:02 +02:00
|
|
|
router.toScriptEditor(filepath, code);
|
2021-09-16 01:50:44 +02:00
|
|
|
} else {
|
2021-09-17 08:58:02 +02:00
|
|
|
router.toScriptEditor(filepath, script.code);
|
2021-09-16 01:50:44 +02:00
|
|
|
}
|
|
|
|
} else if (filename.endsWith(".txt")) {
|
|
|
|
const filepath = terminal.getFilepath(filename);
|
|
|
|
const txt = terminal.getTextFile(player, filename);
|
|
|
|
if (txt == null) {
|
2021-09-17 08:58:02 +02:00
|
|
|
router.toScriptEditor(filepath);
|
2021-09-16 01:50:44 +02:00
|
|
|
} else {
|
2021-09-17 08:58:02 +02:00
|
|
|
router.toScriptEditor(filepath, txt.text);
|
2021-09-16 01:50:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
terminal.error(
|
|
|
|
"Invalid file. Only scripts (.script, .ns, .js), text files (.txt), or .fconf can be edited with nano",
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
terminal.error(e + "");
|
|
|
|
}
|
|
|
|
}
|