-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.ts
77 lines (70 loc) · 2.42 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { FileError } from "../../lib/errors.js";
import fs from "fs-extra";
import path from "node:path";
import { Args, Flags } from "@oclif/core";
import { ensureContractNameOrAllFlagIsSet } from "../../lib/checks.js";
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);
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);
ensureContractNameOrAllFlagIsSet(args, flags);
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}`
);
}
}
}