|
1 | 1 | /*
|
2 | 2 | * Fetches all branches and deletes all review-branches in github pages
|
3 | 3 | */
|
4 |
| -import FS from 'node:fs'; |
| 4 | +import FS from "node:fs"; |
5 | 5 |
|
6 |
| -const TAG = 'cleanup-gh-pages:'; |
| 6 | +const TAG = "cleanup-gh-pages:"; |
7 | 7 |
|
8 | 8 | const removeOldFromPath = (isTag, data) => {
|
9 |
| - const path = `public/${isTag ? 'version' : 'review'}`; |
10 |
| - if ( |
11 |
| - FS.existsSync(path) && |
12 |
| - data?.filter((branch) => branch.name).length > 0 |
13 |
| - ) { |
14 |
| - const dirsToDelete = FS.readdirSync(path) |
15 |
| - .filter((file) => !data.find((branch) => branch.name === file)) |
16 |
| - // Let's not clean up specific folders |
17 |
| - .filter((file) => !['main', 'latest'].includes(file)); |
18 |
| - if (dirsToDelete?.length > 0) { |
19 |
| - console.log( |
20 |
| - TAG, |
21 |
| - `Start removing ${isTag ? 'tags' : 'branches'} from gh-pages` |
22 |
| - ); |
23 |
| - console.log(TAG, dirsToDelete); |
24 |
| - for (const dir of dirsToDelete) { |
25 |
| - FS.rmSync(`${path}/${dir}`, { |
26 |
| - recursive: true, |
27 |
| - force: true |
28 |
| - }); |
29 |
| - console.log(TAG, `deleted ${isTag ? 'tag' : 'branch'} ${dir}`); |
30 |
| - } |
31 |
| - |
32 |
| - return true; |
33 |
| - } |
34 |
| - |
35 |
| - console.log(TAG, `All ${isTag ? 'tags' : 'branches'} are up to date`); |
36 |
| - } |
37 |
| - |
38 |
| - return false; |
| 9 | + const path = `public/${isTag ? "version" : "review"}`; |
| 10 | + if (FS.existsSync(path) && data?.filter((branch) => branch.name).length > 0) { |
| 11 | + const dirsToDelete = FS.readdirSync(path) |
| 12 | + .filter((file) => !data.find((branch) => branch.name === file)) |
| 13 | + // Let's not clean up specific folders |
| 14 | + .filter((file) => !["main", "latest"].includes(file)); |
| 15 | + if (dirsToDelete?.length > 0) { |
| 16 | + console.log( |
| 17 | + TAG, |
| 18 | + `Start removing ${isTag ? "tags" : "branches"} from gh-pages`, |
| 19 | + ); |
| 20 | + console.log(TAG, dirsToDelete); |
| 21 | + for (const dir of dirsToDelete) { |
| 22 | + FS.rmSync(`${path}/${dir}`, { |
| 23 | + recursive: true, |
| 24 | + force: true, |
| 25 | + }); |
| 26 | + console.log(TAG, `deleted ${isTag ? "tag" : "branch"} ${dir}`); |
| 27 | + } |
| 28 | + |
| 29 | + return true; |
| 30 | + } |
| 31 | + |
| 32 | + console.log(TAG, `All ${isTag ? "tags" : "branches"} are up to date`); |
| 33 | + } |
| 34 | + |
| 35 | + return false; |
39 | 36 | };
|
40 | 37 |
|
41 | 38 | const cleanUpPages = async ({ github, context }) => {
|
42 |
| - const { repo, owner } = context.repo; |
43 |
| - const branches = await github.rest.repos.listBranches({ |
44 |
| - owner, |
45 |
| - repo |
46 |
| - }); |
47 |
| - const tags = await github.rest.repos.listTags({ |
48 |
| - owner, |
49 |
| - repo |
50 |
| - }); |
51 |
| - |
52 |
| - return { |
53 |
| - deploy: |
54 |
| - removeOldFromPath(false, branches.data) || |
55 |
| - removeOldFromPath(true, tags.data) |
56 |
| - }; |
| 39 | + const { repo, owner } = context.repo; |
| 40 | + const branches = await github.rest.repos.listBranches({ |
| 41 | + owner, |
| 42 | + repo, |
| 43 | + }); |
| 44 | + const tags = await github.rest.repos.listTags({ |
| 45 | + owner, |
| 46 | + repo, |
| 47 | + }); |
| 48 | + |
| 49 | + return { |
| 50 | + deploy: |
| 51 | + removeOldFromPath(false, branches.data) || |
| 52 | + removeOldFromPath(true, tags.data), |
| 53 | + }; |
57 | 54 | };
|
58 | 55 |
|
59 | 56 | export default cleanUpPages;
|
0 commit comments