create-preview-workflow 1.0.5 → 1.0.6
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 +15 -70
- package/package.json +1 -1
package/index.js
CHANGED
@@ -1,121 +1,66 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
|
3
2
|
const fs = require("fs");
|
4
3
|
const path = require("path");
|
5
4
|
const prompts = require("prompts");
|
6
|
-
|
7
5
|
(async () => {
|
8
6
|
const response = await prompts([
|
9
7
|
{
|
10
|
-
type: "
|
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: "Delete - Remove content from shared directory",
|
20
|
-
value: "delete",
|
21
|
-
},
|
22
|
-
],
|
23
|
-
},
|
24
|
-
{
|
25
|
-
type: (prev) => (prev === "sync" ? "text" : null),
|
8
|
+
type: "text",
|
26
9
|
name: "sourcePath",
|
27
10
|
message:
|
28
11
|
"Enter the source directory to sync (e.g., 2_scripts) without leading slash:",
|
29
12
|
},
|
30
13
|
]);
|
31
|
-
|
32
|
-
let workflow;
|
33
|
-
|
34
|
-
if (response.action === "sync") {
|
35
|
-
workflow = `# .github/workflows/sync.yml
|
14
|
+
const workflow = `# .github/workflows/sync.yml
|
36
15
|
name: Sync Directory
|
37
|
-
|
38
16
|
on:
|
39
17
|
push:
|
40
|
-
|
41
18
|
jobs:
|
42
19
|
sync:
|
43
20
|
runs-on: ubuntu-latest
|
44
21
|
steps:
|
45
22
|
- uses: actions/checkout@v3
|
46
|
-
|
47
23
|
- name: Set variables
|
48
24
|
run: |
|
49
25
|
USER="\${{ github.actor }}"
|
50
26
|
REPO="\${{ github.event.repository.name }}"
|
51
27
|
DIR="\${USER}@\${REPO}"
|
28
|
+
COMMIT_MSG="\$(git log -1 --pretty=format:'%s')"
|
29
|
+
PUSH_TIME="\$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
|
52
30
|
echo "USER=\$USER" >> \$GITHUB_ENV
|
53
31
|
echo "REPO=\$REPO" >> \$GITHUB_ENV
|
54
32
|
echo "DIR=\$DIR" >> \$GITHUB_ENV
|
55
|
-
|
56
|
-
|
33
|
+
echo "COMMIT_MSG=\$COMMIT_MSG" >> \$GITHUB_ENV
|
34
|
+
echo "PUSH_TIME=\$PUSH_TIME" >> \$GITHUB_ENV
|
35
|
+
- name: Clone target repo
|
57
36
|
run: |
|
58
37
|
git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
|
59
38
|
rm -rf preview/public/protected/\$DIR
|
60
39
|
mkdir -p preview/public/protected/\$DIR
|
61
40
|
cp -r ${response.sourcePath}/. preview/public/protected/\$DIR/
|
62
|
-
|
63
|
-
git config user.name "GitHub Actions"
|
64
|
-
git config user.email "actions@github.com"
|
65
|
-
git add .
|
66
|
-
if ! git diff --cached --quiet; then
|
67
|
-
git commit -m "Sync content from source repo"
|
68
|
-
git push
|
69
|
-
else
|
70
|
-
echo "No changes to commit"
|
71
|
-
fi
|
72
|
-
`;
|
73
|
-
} else {
|
74
|
-
workflow = `# .github/workflows/sync.yml
|
75
|
-
name: Delete Directory
|
76
|
-
|
77
|
-
on:
|
78
|
-
push:
|
79
|
-
|
80
|
-
jobs:
|
81
|
-
delete:
|
82
|
-
runs-on: ubuntu-latest
|
83
|
-
steps:
|
84
|
-
- uses: actions/checkout@v3
|
85
|
-
|
86
|
-
- name: Set variables
|
41
|
+
- name: Create log file
|
87
42
|
run: |
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
echo "USER
|
92
|
-
|
93
|
-
echo "DIR=\$DIR" >> \$GITHUB_ENV
|
94
|
-
|
95
|
-
- name: Clone target repo and remove directory
|
43
|
+
cd preview/public/protected/\$DIR
|
44
|
+
echo "Last sync: \$PUSH_TIME" > .sync_log
|
45
|
+
echo "Last commit: \$COMMIT_MSG" >> .sync_log
|
46
|
+
echo "Source: \$USER/\$REPO" >> .sync_log
|
47
|
+
- name: Commit and push changes
|
96
48
|
run: |
|
97
|
-
git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
|
98
|
-
rm -rf preview/public/protected/\$DIR
|
99
49
|
cd preview
|
100
50
|
git config user.name "GitHub Actions"
|
101
51
|
git config user.email "actions@github.com"
|
102
52
|
git add .
|
103
53
|
if ! git diff --cached --quiet; then
|
104
|
-
git commit -m "
|
54
|
+
git commit -m "Sync from source repo"
|
105
55
|
git push
|
106
56
|
else
|
107
57
|
echo "No changes to commit"
|
108
58
|
fi
|
109
59
|
`;
|
110
|
-
}
|
111
|
-
|
112
60
|
const destDir = path.join(process.cwd(), ".github", "workflows");
|
113
61
|
fs.mkdirSync(destDir, { recursive: true });
|
114
62
|
fs.writeFileSync(path.join(destDir, "sync.yml"), workflow);
|
115
|
-
|
116
|
-
const actionText =
|
117
|
-
response.action === "sync" ? "sync content to" : "delete content from";
|
118
63
|
console.log(
|
119
|
-
|
64
|
+
"GitHub Actions workflow for Preview created at .github/workflows/sync.yml. Make sure to add the GH_SYNC_TOKEN secret in your repository settings."
|
120
65
|
);
|
121
66
|
})();
|