feat: Support opening multiple files from command line

This commit is contained in:
Billy Vong
2021-12-20 12:42:47 -05:00
parent d30edc7f59
commit bb2f8e883c
5 changed files with 99 additions and 94 deletions
+42 -39
View File
@@ -1,5 +1,5 @@
import { ITerminal } from "../../ITerminal";
import { IRouter, ScriptEditorRouteOptions} from "../../../ui/Router";
import { IRouter, ScriptEditorRouteOptions } from "../../../ui/Router";
import { IPlayer } from "../../../PersonObjects/IPlayer";
import { BaseServer } from "../../../Server/BaseServer";
import { isScriptFilename } from "../../../Script/isScriptFilename";
@@ -13,53 +13,56 @@ interface EditorParameters {
args: (string | number | boolean)[];
}
function isNs2(filename: string): boolean {
return filename.endsWith(".ns") || filename.endsWith(".js");
}
export function commonEditor(command: string, {
terminal,
router,
player,
server,
args,
}: EditorParameters, scriptEditorRouteOptions?: ScriptEditorRouteOptions): void {
if (args.length !== 1) {
const newNs2Template = `/** @param {NS} ns **/
export async function main(ns) {
}`;
export function commonEditor(
command: string,
{ terminal, router, player, server, args }: EditorParameters,
scriptEditorRouteOptions?: ScriptEditorRouteOptions,
): void {
if (args.length < 1) {
terminal.error(`Incorrect usage of ${command} command. Usage: ${command} [scriptname]`);
return;
}
try {
const filename = args[0] + "";
if (isScriptFilename(filename)) {
const filepath = terminal.getFilepath(filename);
const script = terminal.getScript(player, filename);
if (script == null) {
let code = "";
if (filename.endsWith(".ns") || filename.endsWith(".js")) {
code = `/** @param {NS} ns **/
export async function main(ns) {
}`;
const files = args.map((arg) => {
const filename = `${arg}`;
if (isScriptFilename(filename)) {
const filepath = terminal.getFilepath(filename);
const script = terminal.getScript(player, filename);
const fileIsNs2 = isNs2(filename);
const code = script !== null ? script.code : fileIsNs2 ? newNs2Template : "";
if (code === newNs2Template) {
CursorPositions.saveCursor(filename, {
row: 3,
column: 5,
});
}
CursorPositions.saveCursor(filename, {
row: 3,
column: 5,
});
router.toScriptEditor(filepath, code, scriptEditorRouteOptions);
} else {
router.toScriptEditor(filepath, script.code, scriptEditorRouteOptions);
return [filepath, code];
}
} else if (filename.endsWith(".txt")) {
const filepath = terminal.getFilepath(filename);
const txt = terminal.getTextFile(player, filename);
if (txt == null) {
router.toScriptEditor(filepath, "", scriptEditorRouteOptions);
} else {
router.toScriptEditor(filepath, txt.text, scriptEditorRouteOptions);
if (filename.endsWith(".txt")) {
const filepath = terminal.getFilepath(filename);
const txt = terminal.getTextFile(player, filename);
return [filepath, txt == null ? "" : txt.text];
}
} else {
terminal.error("Invalid file. Only scripts (.script, .ns, .js), or text files (.txt) can be edited with vim");
return;
}
throw new Error(`Invalid file. Only scripts (.script, .ns, .js), or text files (.txt) can be edited with ${command}`);
});
router.toScriptEditor(Object.fromEntries(files), scriptEditorRouteOptions);
} catch (e) {
terminal.error(e + "");
terminal.error(`${e}`);
}
}