create-preview-workflow 1.0.8 → 1.0.9

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.
Files changed (2) hide show
  1. package/index.js +79 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,27 +1,54 @@
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: "text",
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
- const workflow = `# .github/workflows/sync.yml
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
- - '${response.sourcePath}/**'
42
+ - "${response.sourcePath}/**"
43
+
20
44
  jobs:
21
45
  sync:
22
46
  runs-on: ubuntu-latest
23
47
  steps:
24
48
  - uses: actions/checkout@v3
49
+ with:
50
+ fetch-depth: 2
51
+
25
52
  - name: Set variables
26
53
  run: |
27
54
  USER="\${{ github.actor }}"
@@ -38,37 +65,75 @@ jobs:
38
65
  echo "COMMIT_SHA=\$COMMIT_SHA" >> \$GITHUB_ENV
39
66
  echo "PUSH_TIME=\$PUSH_TIME" >> \$GITHUB_ENV
40
67
  echo "GITHUB_URL=\$GITHUB_URL" >> \$GITHUB_ENV
41
- - name: Clone target repo
68
+
69
+ - name: Clone target repo and sync content
42
70
  run: |
43
71
  git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
44
72
  rm -rf preview/public/protected/\$DIR
45
73
  mkdir -p preview/public/protected/\$DIR
46
74
  cp -r ${response.sourcePath}/. preview/public/protected/\$DIR/
47
- - name: Create log file
75
+ echo "Last sync: \$PUSH_TIME" > preview/public/protected/\$DIR/.sync_log
76
+ echo "Last commit: \$COMMIT_MSG" >> preview/public/protected/\$DIR/.sync_log
77
+ echo "Commit SHA: \$COMMIT_SHA" >> preview/public/protected/\$DIR/.sync_log
78
+ echo "Source: \$USER/\$REPO" >> preview/public/protected/\$DIR/.sync_log
79
+ echo "View diff: \$GITHUB_URL" >> preview/public/protected/\$DIR/.sync_log
80
+ cd preview
81
+ git config user.name "GitHub Actions"
82
+ git config user.email "actions@github.com"
83
+ git add .
84
+ if ! git diff --cached --quiet; then
85
+ git commit -m "Sync content from source repo"
86
+ git push
87
+ else
88
+ echo "No changes to commit"
89
+ fi
90
+ `;
91
+ } else {
92
+ workflow = `# .github/workflows/sync.yml
93
+ name: Delete Directory
94
+
95
+ on:
96
+ push:
97
+
98
+ jobs:
99
+ delete:
100
+ runs-on: ubuntu-latest
101
+ steps:
102
+ - uses: actions/checkout@v3
103
+
104
+ - name: Set variables
48
105
  run: |
49
- cd preview/public/protected/\$DIR
50
- echo "Last sync: \$PUSH_TIME" > .sync_log
51
- echo "Last commit: \$COMMIT_MSG" >> .sync_log
52
- echo "Commit SHA: \$COMMIT_SHA" >> .sync_log
53
- echo "Source: \$USER/\$REPO" >> .sync_log
54
- echo "View diff: \$GITHUB_URL" >> .sync_log
55
- - name: Commit and push changes
106
+ USER="\${{ github.actor }}"
107
+ REPO="\${{ github.event.repository.name }}"
108
+ DIR="\${USER}@\${REPO}"
109
+ echo "USER=\$USER" >> \$GITHUB_ENV
110
+ echo "REPO=\$REPO" >> \$GITHUB_ENV
111
+ echo "DIR=\$DIR" >> \$GITHUB_ENV
112
+
113
+ - name: Clone target repo and remove directory
56
114
  run: |
115
+ git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
116
+ rm -rf preview/public/protected/\$DIR
57
117
  cd preview
58
118
  git config user.name "GitHub Actions"
59
119
  git config user.email "actions@github.com"
60
120
  git add .
61
121
  if ! git diff --cached --quiet; then
62
- git commit -m "Sync from source repo"
122
+ git commit -m "Remove directory from shared repo"
63
123
  git push
64
124
  else
65
125
  echo "No changes to commit"
66
126
  fi
67
127
  `;
128
+ }
129
+
68
130
  const destDir = path.join(process.cwd(), ".github", "workflows");
69
131
  fs.mkdirSync(destDir, { recursive: true });
70
132
  fs.writeFileSync(path.join(destDir, "sync.yml"), workflow);
133
+
134
+ const actionText =
135
+ response.action === "sync" ? "sync content to" : "delete content from";
71
136
  console.log(
72
- "✅ GitHub Actions workflow for Preview created at .github/workflows/sync.yml. Make sure to add the GH_SYNC_TOKEN secret in your repository settings."
137
+ `✅ 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
138
  );
74
139
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-preview-workflow",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "bin": {
5
5
  "create-preview-workflow": "index.js"
6
6
  },