-
Notifications
You must be signed in to change notification settings - Fork 19
/
getPackages.js
executable file
·64 lines (56 loc) · 1.69 KB
/
getPackages.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
60
61
62
63
64
const { exec } = require("child_process");
const { mkdir, readdir, rm, rmdir } = require("fs").promises;
const path = require("path");
const packagesPath = path.join(process.cwd(), "test", "packages");
async function hasPackages() {
const packages = await readdir(packagesPath);
return packages.length > 0;
}
async function isDir(potentialDirPath) {
return readdir(potentialDirPath).then(
() => true,
() => false
);
}
async function rmIfNotReactOrPackageFile(targetPath) {
if (targetPath.match(/(\.(t|j)sx$)|(package\.json)/)) {
return;
}
await rm(targetPath);
}
async function purge(targetPath) {
if (await isDir(targetPath)) {
const files = await readdir(targetPath);
const promises = files.map((file) => purge(path.join(targetPath, file)));
await Promise.all(promises).then(async () => {
const postPurgeFiles = await readdir(targetPath);
if (!postPurgeFiles.length) {
await rmdir(targetPath);
}
});
} else {
await rmIfNotReactOrPackageFile(targetPath);
}
}
async function getPackages() {
await mkdir(packagesPath, { recursive: true });
const packagesAlreadyDownloaded = await hasPackages();
if (packagesAlreadyDownloaded) {
console.log("packages already downloaded");
} else {
console.log("downloading test packages");
exec(
"cd test/packages && curl https://codeload.github.com/patternfly/patternfly-react/tar.gz/main | tar -xz --strip=2 patternfly-react-main/packages",
(err, stdOut, stdErr) => {
if (err) {
console.error("error", err);
} else {
console.log(stdErr);
console.log(stdOut);
purge(packagesPath);
}
}
);
}
}
getPackages();