diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CleanCommand.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CleanCommand.java index 6d28d9dbb972..6f8eff3fbe20 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CleanCommand.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/cmd/CleanCommand.java @@ -77,7 +77,20 @@ public void execute() { return; } - if (this.targetDir == null) { + if (this.targetDir != null) { + if (Files.notExists(this.targetDir)) { + CommandUtil.printError(this.outStream, + "provided target directory '" + this.targetDir + "' does not exist.", + null, false); + } else if (!Files.isDirectory(this.targetDir)) { + CommandUtil.printError(this.outStream, + "provided target path '" + this.targetDir + "' is not a directory.", + null, false); + } else { + ProjectUtils.deleteDirectory(this.targetDir); + this.outStream.println("Successfully deleted '" + this.targetDir + "'."); + } + } else { try { Project project = BuildProject.load(this.projectPath); this.targetDir = project.targetDir(); @@ -86,23 +99,13 @@ public void execute() { CommandUtil.exitError(this.exitWhenFinish); return; } + if (Files.exists(this.targetDir)) { + ProjectUtils.deleteDirectory(this.targetDir); + this.outStream.println("Successfully deleted '" + this.targetDir + "'."); + } } - // Delete the target directory - if (Files.notExists(this.targetDir)) { - CommandUtil.printError(this.outStream, - "provided target directory '" + this.targetDir + "' does not exist.", - null, false); - } else if (!Files.isDirectory(this.targetDir)) { - CommandUtil.printError(this.outStream, - "provided target path '" + this.targetDir + "' is not a directory.", - null, false); - } else { - ProjectUtils.deleteDirectory(this.targetDir); - this.outStream.println("Successfully deleted '" + this.targetDir + "'."); - } - - //delete the generated directory + // delete the generated directory Path generatedDir; try { Project project = BuildProject.load(this.projectPath);