From 2f6a18eda4a59df9fcc28b7b690494260d17b518 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Rodr=C3=ADguez?= Date: Sun, 25 Feb 2024 00:50:28 -0600 Subject: [PATCH] Fixed: Fix bugs in the functions and variables --- src/commands/clear/index.ts | 83 ++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 47 deletions(-) diff --git a/src/commands/clear/index.ts b/src/commands/clear/index.ts index 0d43a400..ec90d667 100644 --- a/src/commands/clear/index.ts +++ b/src/commands/clear/index.ts @@ -1,17 +1,21 @@ import { SwankyCommand } from "../../lib/swankyCommand.js"; import { ConfigError, FileError } from "../../lib/errors.js"; -import fs from "fs-extra" -import path from "node:path" +import fs from "fs-extra"; +import path from "node:path"; import { Args, Flags } from "@oclif/core"; -type Folder = {name: string, contractName?: string, path: string} +interface Folder { + name: string, + contractName?: string, + path: string +} export default class Clear extends SwankyCommand { static flags = { all: Flags.boolean({ char: "a", - description: "Select all the project artifacts for delete" + description: "Select all the project artifacts for delete", }), }; @@ -26,57 +30,42 @@ export default class Clear extends SwankyCommand { async deleteFolder(path: string): Promise { try { await fs.remove(path); - } catch (err) { - throw new FileError( - `Error deleting the folder ${path}.`, {cause: err} - ); + this.log(`Successfully deleted ${path}`); + } catch (err: any) { + if (err.code === "ENOENT") { + this.log(`Folder ${path} does not exist, skipping.`); + } else { + throw new FileError(`Error deleting the folder ${path}.`, { cause: err }); + } } - } + } public async run(): Promise { const { flags, args } = await this.parse(Clear); - if(args.contractName === undefined && !flags.all) throw new ConfigError("You need to send any flag or argument.") + if (args.contractName === undefined && !flags.all) { + throw new ConfigError("Specify a contract name or use the --all flag to delete all artifacts."); + } const workDirectory = process.cwd(); - let foldersToDelete: Folder[] = [] - - if(flags.all) { - - foldersToDelete.push({ - name: "Artifacts", - path: path.join(workDirectory, './artifacts') - }, { - name: "Target", - path: path.join(workDirectory, './target') - }) - - for await (const folder of foldersToDelete) { - let resultSpinner = await this.spinner.runCommand( async () => this.deleteFolder(folder.path), - `Deleting the ${folder.name} folder` - ) - } - } else if (args.contractName) { - - foldersToDelete.push({ - name: "Artifacts", - contractName: args.contractName, - path: path.join(workDirectory, './artifacts/', args.contractName) - }, { - name: "Target", - path: path.join(workDirectory, './target') - }, { - name: "TestArtifacts", - contractName: args.contractName, - path: path.join(workDirectory, './tests/', args.contractName, "/artifacts") - }) - - for await (const folder of foldersToDelete) { - let resultSpinner = await this.spinner.runCommand( async () => this.deleteFolder(folder.path), - `Deleting the ${folder.name} folder` - ) - } + const foldersToDelete: Folder[] = flags.all ? + [ + { name: "Artifacts", path: path.join(workDirectory, "./artifacts") }, + { name: "Target", path: path.join(workDirectory, "./target") } + ] + : args.contractName ? + [ + { name: "Artifacts", contractName: args.contractName, path: path.join(workDirectory, "./artifacts/", args.contractName) }, + { name: "Target", path: path.join(workDirectory, "./target") }, + { name: "TestArtifacts", contractName: args.contractName, path: path.join(workDirectory, "./tests/", args.contractName, "/artifacts") } + ] + : []; + for (const folder of foldersToDelete) { + await this.spinner.runCommand(async () => this.deleteFolder(folder.path), + `Deleting the ${folder.name} folder ${folder.contractName ? `for ${folder.contractName} contract` : ""}`, + `Successfully deleted the ${folder.name} folder ${folder.contractName ? `for ${folder.contractName} contract` : ""}\n at ${folder.path}` + ); } } } \ No newline at end of file