Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/clear command #189

Merged
merged 4 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ USAGE
* [`swanky node install`](#swanky-node-install)
* [`swanky node purge`](#swanky-node-purge)
* [`swanky node start`](#swanky-node-start)
* [`swanky clear CONTRACTNAME`](#)
* [`swanky plugins`](#swanky-plugins)
* [`swanky plugins:install PLUGIN...`](#swanky-pluginsinstall-plugin)
* [`swanky plugins:inspect PLUGIN...`](#swanky-pluginsinspect-plugin)
Expand Down Expand Up @@ -576,6 +577,25 @@ DESCRIPTION
Start a local node
```

## `swanky clear`

Clear the artifacts

```
USAGE
$ swanky clear [CONTRACTNAME] [-v] [-a]

ARGUMENTS
CONTRACTNAME Name of the contract artifact to clear

FLAGS
-a, --all Select all the project artifacts for delete
-v, --verbose Display more info in the result logs

DESCRIPTION
Clear the artifacts
```

## `swanky plugins`

List installed plugins.
Expand Down
71 changes: 71 additions & 0 deletions src/commands/clear/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ConfigError, FileError } from "../../lib/errors.js";
import fs from "fs-extra";
import path from "node:path";
import { Args, Flags } from "@oclif/core";

interface Folder {
name: string,
contractName?: string,
path: string
}

export default class Clear extends SwankyCommand<typeof Clear> {

static flags = {
all: Flags.boolean({
char: "a",
description: "Select all the project artifacts for delete",
}),
};

static args = {
contractName: Args.string({
name: "contractName",
required: false,
description: "Name of the contract artifact to clear",
}),
};

async deleteFolder(path: string): Promise<void> {
try {
await fs.remove(path);
Juminstock marked this conversation as resolved.
Show resolved Hide resolved
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<any> {

const { flags, args } = await this.parse(Clear);

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();
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}`
);
}
}
}
Loading