From de1b3ab315997bb31c3edb3c78bf88e3962f286c Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Sun, 3 Nov 2019 18:21:25 +0100 Subject: [PATCH] Fix #29 And as a bonus, add the -n option to echo. --- src/main/js/Commands.ts | 15 +++++++++------ src/main/js/FileSystem.ts | 20 +++++++++++--------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/js/Commands.ts b/src/main/js/Commands.ts index 0974b03..a0b915b 100644 --- a/src/main/js/Commands.ts +++ b/src/main/js/Commands.ts @@ -70,8 +70,9 @@ export class Commands { "echo": new Command( this.echo, `display text`, - `echo [TEXT]`, - `Displays [TEXT].`.trimLines(), + `echo [-n] [TEXT]`, + `Displays [TEXT]. + Unless the -n parameter is given, a newline is appended to the end.`.trimLines(), new InputValidator() ), "exit": new Command( @@ -109,10 +110,11 @@ export class Commands { "mkdir": new Command( this.mkdir, `make directories`, - `mkdir DIRECTORY...`, + `mkdir [-p] DIRECTORY ...`, `Creates the directories given by DIRECTORY. - If more than one directory is given, the directories are created in the order they are given in.`.trimLines(), + If more than one directory is given, the directories are created in the order they are given in. + If the -p option is given, parent directories that do not exist are created as well.`.trimLines(), new InputValidator({minArgs: 1}) ), "mv": new Command( @@ -262,7 +264,8 @@ export class Commands { } private echo(input: InputArgs): string { - return input.args.join(" ").replace("hunter2", "*******") + "\n"; + return input.args.join(" ").replace("hunter2", "*******") + + input.hasOption("n") ? "\n" : ""; } private exit(): string { @@ -336,7 +339,7 @@ export class Commands { } private mkdir(input: InputArgs): string { - return this.fileSystem.mkdirs(input.args); + return this.fileSystem.mkdirs(input.args, input.hasOption("p")); } private mv(input: InputArgs): string { diff --git a/src/main/js/FileSystem.ts b/src/main/js/FileSystem.ts index fa5e6af..d7d2088 100644 --- a/src/main/js/FileSystem.ts +++ b/src/main/js/FileSystem.ts @@ -298,11 +298,14 @@ export class FileSystem { /** * Creates an empty directory in the file system. * - * @param pathString {string} the absolute or relative path to the directory to create + * @param pathString the absolute or relative path to the directory to create + * @param createParents `true` if and only intermediate directories that do not exist should be created * @return an empty string if the removal was successful, or a message explaining what went wrong */ - private mkdir(pathString: string): string { + private mkdir(pathString: string, createParents: boolean): string { const path = this.getPathTo(pathString); + if (createParents && path.toString() !== "/") + this.mkdir(path.parent.toString(), true); const parentNode = this.getNode(path.parent); if (parentNode === undefined) @@ -317,13 +320,14 @@ export class FileSystem { } /** - * Calls {@link mkdir} on all elements in {@code paths}. + * Calls `mkdir` on all elements in `paths`. * - * @param paths {string[]} the absolute or relative paths to the directories to create + * @param paths the absolute or relative paths to the directories to create + * @param createParents `true` if and only intermediate directories that do not exist should be created * @return the warnings generated during creation of the directories */ - mkdirs(paths: string[]): string { - return this.executeForEach(paths, this.mkdir.bind(this)); + mkdirs(paths: string[], createParents: boolean): string { + return this.executeForEach(paths, path => this.mkdir(path, createParents)); } /** @@ -401,9 +405,7 @@ export class FileSystem { * @return the warnings generated during removal of the directories */ rms(paths: string[], force: boolean = false, recursive: boolean = false, noPreserveRoot: boolean = false): string { - return this.executeForEach(paths, path => { - return this.rm(path, force, recursive, noPreserveRoot); - }); + return this.executeForEach(paths, path => this.rm(path, force, recursive, noPreserveRoot)); } /**