Skip to content

Commit b6e6f4b

Browse files
committed
Add clean task
1 parent 65cf927 commit b6e6f4b

File tree

4 files changed

+159
-1
lines changed

4 files changed

+159
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { HardhatPlugin } from "@ignored/hardhat-vnext-core/types/plugins";
2+
3+
import { task } from "@ignored/hardhat-vnext-core/config";
4+
5+
const hardhatPlugin: HardhatPlugin = {
6+
id: "clean",
7+
tasks: [
8+
task("clean", "Clears the cache and deletes all artifacts")
9+
.addFlag({
10+
name: "global",
11+
description: "Clear the global cache",
12+
})
13+
.setAction(import.meta.resolve("./task-action.js"))
14+
.build(),
15+
],
16+
};
17+
18+
export default hardhatPlugin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext-core/types/tasks";
2+
3+
import { getCacheDir } from "@ignored/hardhat-vnext-core/global-dir";
4+
import { emptyDir, remove } from "@ignored/hardhat-vnext-utils/fs";
5+
6+
interface CleanActionArguments {
7+
global: boolean;
8+
}
9+
10+
const cleanAction: NewTaskActionFunction<CleanActionArguments> = async (
11+
{ global },
12+
{ config },
13+
) => {
14+
if (global) {
15+
const globalCacheDir = await getCacheDir();
16+
await emptyDir(globalCacheDir);
17+
}
18+
19+
await emptyDir(config.paths.cache);
20+
await remove(config.paths.artifacts);
21+
22+
// TODO: Uncomment this when we have a clearCache method in the artifacts module
23+
// artifacts.clearCache?.();
24+
};
25+
26+
export default cleanAction;
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { HardhatPlugin } from "@ignored/hardhat-vnext-core/types/plugins";
22

3+
import clean from "./clean/index.js";
34
import hardhatFoo from "./hardhat-foo/index.js";
45
import run from "./run/index.js";
56

67
// Note: When importing a plugin, you have to export its types, so that its
78
// type extensions, if any, also get loaded.
9+
export type * from "./clean/index.js";
810
export type * from "./hardhat-foo/index.js";
911
export type * from "./run/index.js";
1012

11-
export const builtinPlugins: HardhatPlugin[] = [hardhatFoo, run];
13+
export const builtinPlugins: HardhatPlugin[] = [clean, hardhatFoo, run];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import type { HardhatRuntimeEnvironment } from "@ignored/hardhat-vnext-core/types/hre";
2+
3+
import assert from "node:assert/strict";
4+
import path from "node:path";
5+
import { before, beforeEach, describe, it } from "node:test";
6+
7+
import { getCacheDir } from "@ignored/hardhat-vnext-core/global-dir";
8+
import {
9+
exists,
10+
mkdir,
11+
readdir,
12+
remove,
13+
writeUtf8File,
14+
} from "@ignored/hardhat-vnext-utils/fs";
15+
16+
import { createHardhatRuntimeEnvironment } from "../../../../src/hre.js";
17+
import cleanAction from "../../../../src/internal/builtin-plugins/clean/task-action.js";
18+
import { useFixtureProject } from "../../../helpers/project.js";
19+
20+
function assertCleanBehavior(global: boolean, globalCacheDir: string) {
21+
it("should empty the cache dir", async () => {
22+
const cacheContents = await readdir(path.join(process.cwd(), "cache"));
23+
assert.ok(cacheContents.length === 0, "Cache dir is not empty");
24+
});
25+
26+
it("should remove the artifacts dir", async () => {
27+
assert.ok(
28+
exists(path.join(process.cwd(), "artifacts")),
29+
"Artifacts dir does not exist",
30+
);
31+
});
32+
33+
if (global) {
34+
it("should empty the global cache dir when the global flag is true", async () => {
35+
const globalCacheContents = await readdir(globalCacheDir);
36+
assert.ok(
37+
globalCacheContents.length === 0,
38+
"Global cache dir is not empty",
39+
);
40+
});
41+
} else {
42+
it("should not empty the global cache dir when the global flag is false", async () => {
43+
const globalCacheContents = await readdir(globalCacheDir);
44+
assert.ok(globalCacheContents.length > 0, "Global cache dir is empty");
45+
});
46+
}
47+
}
48+
49+
describe("clean/task-action", () => {
50+
let hre: HardhatRuntimeEnvironment;
51+
let globalCacheDir: string;
52+
53+
before(async function () {
54+
hre = await createHardhatRuntimeEnvironment({});
55+
globalCacheDir = await getCacheDir();
56+
});
57+
58+
describe("cleanAction", () => {
59+
useFixtureProject("loaded-config");
60+
61+
describe("when cache and artifact dirs don't exist", async () => {
62+
beforeEach(async () => {
63+
await remove(globalCacheDir);
64+
await remove(path.join(process.cwd(), "cache"));
65+
await remove(path.join(process.cwd(), "artifacts"));
66+
});
67+
68+
await cleanAction({ global: true }, hre);
69+
assertCleanBehavior(true, globalCacheDir);
70+
});
71+
72+
describe("when cache and artifact are empty dirs", async () => {
73+
beforeEach(async () => {
74+
await remove(globalCacheDir);
75+
await remove(path.join(process.cwd(), "cache"));
76+
await remove(path.join(process.cwd(), "artifacts"));
77+
await getCacheDir(); // Recreate the cache dir
78+
await mkdir(path.join(process.cwd(), "cache"));
79+
await mkdir(path.join(process.cwd(), "artifacts"));
80+
});
81+
82+
await cleanAction({ global: true }, hre);
83+
assertCleanBehavior(true, globalCacheDir);
84+
});
85+
86+
describe("when cache and artifact dirs aren't empty", async () => {
87+
beforeEach(async () => {
88+
await remove(globalCacheDir);
89+
await remove(path.join(process.cwd(), "cache"));
90+
await remove(path.join(process.cwd(), "artifacts"));
91+
await getCacheDir(); // Recreate the cache dir
92+
await writeUtf8File(path.join(globalCacheDir, "a"), "");
93+
await writeUtf8File(path.join(process.cwd(), "cache", "a"), "");
94+
await writeUtf8File(path.join(process.cwd(), "artifacts", "a"), "");
95+
});
96+
97+
await cleanAction({ global: true }, hre);
98+
assertCleanBehavior(true, globalCacheDir);
99+
});
100+
101+
describe("when global flag is false", async () => {
102+
beforeEach(async () => {
103+
await remove(globalCacheDir);
104+
await getCacheDir(); // Recreate the cache dir
105+
await writeUtf8File(path.join(globalCacheDir, "a"), "");
106+
});
107+
108+
await cleanAction({ global: false }, hre);
109+
assertCleanBehavior(false, globalCacheDir);
110+
});
111+
});
112+
});

0 commit comments

Comments
 (0)