forked from secretlint/secretlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-tests.js
59 lines (55 loc) · 1.85 KB
/
import-tests.js
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
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf-8"));
const ruleDependencies = Object.keys(pkg.devDependencies).filter((name) => {
return name.startsWith("@secretlint/secretlint-rule-");
});
const packagesDir = path.join(__dirname, "../");
const ruleDirs = fs
.readdirSync(packagesDir, {
withFileTypes: true,
})
.filter((dirent) => {
return dirent.isDirectory() && ruleDependencies.includes(`@secretlint/${dirent.name}`);
});
/**
*
* @param {Dirent} ruleDir
*/
const copyTestFiles = (ruleDir) => {
const ruleDirName = ruleDir.name;
const snapshotsDir = path.join(packagesDir, ruleDir.name, "test", "snapshots");
if (fs.existsSync(snapshotsDir) === false) {
return;
}
console.log(`Copy test files: ${ruleDirName}`);
const testDirs = fs
.readdirSync(snapshotsDir, {
withFileTypes: true,
})
.filter((dirent) => {
return dirent.isDirectory();
});
testDirs.forEach((dir) => {
const src = path.join(snapshotsDir, dir.name);
// test/snapshots/<rule>/...
const dest = path.join("test", "snapshots", ruleDirName, dir.name);
fs.cpSync(src, dest, {
recursive: true,
});
});
};
const rmCurrentSnapshots = () => {
console.log(`Remove current snapshots`);
const currentSnapshotsDir = path.join(__dirname, "test", "snapshots");
return fs.rmSync(currentSnapshotsDir, {
recursive: true,
});
};
await rmCurrentSnapshots();
ruleDirs.forEach(copyTestFiles);
console.log("Need to update snapshots:");
console.log("$ npm run updateSnapshots");
console.log("Copied snapshot is a bit different from preset.");