create-preview-workflow 1.0.8 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +80 -14
- package/package.json +1 -1
package/index.js
CHANGED
@@ -1,27 +1,55 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
+
|
2
3
|
const fs = require("fs");
|
3
4
|
const path = require("path");
|
4
5
|
const prompts = require("prompts");
|
6
|
+
|
5
7
|
(async () => {
|
6
8
|
const response = await prompts([
|
7
9
|
{
|
8
|
-
type: "
|
10
|
+
type: "select",
|
11
|
+
name: "action",
|
12
|
+
message: "What would you like to do with the shared directory?",
|
13
|
+
choices: [
|
14
|
+
{
|
15
|
+
title: "Create/Update - Sync content to shared directory",
|
16
|
+
value: "sync",
|
17
|
+
},
|
18
|
+
{
|
19
|
+
title:
|
20
|
+
"Delete - Remove the remote connection to the shared directory",
|
21
|
+
value: "delete",
|
22
|
+
},
|
23
|
+
],
|
24
|
+
},
|
25
|
+
{
|
26
|
+
type: (prev) => (prev === "sync" ? "text" : null),
|
9
27
|
name: "sourcePath",
|
10
28
|
message:
|
11
29
|
"Enter the source directory to sync (e.g., 2_scripts) without leading slash:",
|
12
30
|
},
|
13
31
|
]);
|
14
|
-
|
32
|
+
|
33
|
+
let workflow;
|
34
|
+
|
35
|
+
if (response.action === "sync") {
|
36
|
+
workflow = `# .github/workflows/sync.yml
|
15
37
|
name: Sync Directory
|
38
|
+
|
16
39
|
on:
|
17
40
|
push:
|
18
41
|
paths:
|
19
|
-
-
|
42
|
+
- "${response.sourcePath}/**"
|
43
|
+
- ".github/workflows/sync.yml"
|
44
|
+
|
20
45
|
jobs:
|
21
46
|
sync:
|
22
47
|
runs-on: ubuntu-latest
|
23
48
|
steps:
|
24
49
|
- uses: actions/checkout@v3
|
50
|
+
with:
|
51
|
+
fetch-depth: 2
|
52
|
+
|
25
53
|
- name: Set variables
|
26
54
|
run: |
|
27
55
|
USER="\${{ github.actor }}"
|
@@ -38,37 +66,75 @@ jobs:
|
|
38
66
|
echo "COMMIT_SHA=\$COMMIT_SHA" >> \$GITHUB_ENV
|
39
67
|
echo "PUSH_TIME=\$PUSH_TIME" >> \$GITHUB_ENV
|
40
68
|
echo "GITHUB_URL=\$GITHUB_URL" >> \$GITHUB_ENV
|
41
|
-
|
69
|
+
|
70
|
+
- name: Clone target repo and sync content
|
42
71
|
run: |
|
43
72
|
git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
|
44
73
|
rm -rf preview/public/protected/\$DIR
|
45
74
|
mkdir -p preview/public/protected/\$DIR
|
46
75
|
cp -r ${response.sourcePath}/. preview/public/protected/\$DIR/
|
47
|
-
|
76
|
+
echo "Last sync: \$PUSH_TIME" > preview/public/protected/\$DIR/.sync_log
|
77
|
+
echo "Last commit: \$COMMIT_MSG" >> preview/public/protected/\$DIR/.sync_log
|
78
|
+
echo "Commit SHA: \$COMMIT_SHA" >> preview/public/protected/\$DIR/.sync_log
|
79
|
+
echo "Source: \$USER/\$REPO" >> preview/public/protected/\$DIR/.sync_log
|
80
|
+
echo "View diff: \$GITHUB_URL" >> preview/public/protected/\$DIR/.sync_log
|
81
|
+
cd preview
|
82
|
+
git config user.name "GitHub Actions"
|
83
|
+
git config user.email "actions@github.com"
|
84
|
+
git add .
|
85
|
+
if ! git diff --cached --quiet; then
|
86
|
+
git commit -m "Sync content from source repo"
|
87
|
+
git push
|
88
|
+
else
|
89
|
+
echo "No changes to commit"
|
90
|
+
fi
|
91
|
+
`;
|
92
|
+
} else {
|
93
|
+
workflow = `# .github/workflows/sync.yml
|
94
|
+
name: Delete Directory
|
95
|
+
|
96
|
+
on:
|
97
|
+
push:
|
98
|
+
|
99
|
+
jobs:
|
100
|
+
delete:
|
101
|
+
runs-on: ubuntu-latest
|
102
|
+
steps:
|
103
|
+
- uses: actions/checkout@v3
|
104
|
+
|
105
|
+
- name: Set variables
|
48
106
|
run: |
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
echo "
|
53
|
-
echo "
|
54
|
-
echo "
|
55
|
-
|
107
|
+
USER="\${{ github.actor }}"
|
108
|
+
REPO="\${{ github.event.repository.name }}"
|
109
|
+
DIR="\${USER}@\${REPO}"
|
110
|
+
echo "USER=\$USER" >> \$GITHUB_ENV
|
111
|
+
echo "REPO=\$REPO" >> \$GITHUB_ENV
|
112
|
+
echo "DIR=\$DIR" >> \$GITHUB_ENV
|
113
|
+
|
114
|
+
- name: Clone target repo and remove directory
|
56
115
|
run: |
|
116
|
+
git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
|
117
|
+
rm -rf preview/public/protected/\$DIR
|
57
118
|
cd preview
|
58
119
|
git config user.name "GitHub Actions"
|
59
120
|
git config user.email "actions@github.com"
|
60
121
|
git add .
|
61
122
|
if ! git diff --cached --quiet; then
|
62
|
-
git commit -m "
|
123
|
+
git commit -m "Remove directory from shared repo"
|
63
124
|
git push
|
64
125
|
else
|
65
126
|
echo "No changes to commit"
|
66
127
|
fi
|
67
128
|
`;
|
129
|
+
}
|
130
|
+
|
68
131
|
const destDir = path.join(process.cwd(), ".github", "workflows");
|
69
132
|
fs.mkdirSync(destDir, { recursive: true });
|
70
133
|
fs.writeFileSync(path.join(destDir, "sync.yml"), workflow);
|
134
|
+
|
135
|
+
const actionText =
|
136
|
+
response.action === "sync" ? "sync content to" : "delete content from";
|
71
137
|
console.log(
|
72
|
-
|
138
|
+
`✅ GitHub Actions workflow to ${actionText} shared directory created at .github/workflows/sync.yml. Make sure to add the GH_SYNC_TOKEN secret in your repository settings.`
|
73
139
|
);
|
74
140
|
})();
|