create-preview-workflow 1.0.5 → 1.0.7

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 +21 -70
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,121 +1,72 @@
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: "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: "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
+ COMMIT_SHA="\$(git log -1 --pretty=format:'%H')"
30
+ PUSH_TIME="\$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
31
+ GITHUB_URL="https://github.com/\${USER}/\${REPO}/commit/\${COMMIT_SHA}"
52
32
  echo "USER=\$USER" >> \$GITHUB_ENV
53
33
  echo "REPO=\$REPO" >> \$GITHUB_ENV
54
34
  echo "DIR=\$DIR" >> \$GITHUB_ENV
55
-
56
- - name: Clone target repo and sync content
35
+ echo "COMMIT_MSG=\$COMMIT_MSG" >> \$GITHUB_ENV
36
+ echo "COMMIT_SHA=\$COMMIT_SHA" >> \$GITHUB_ENV
37
+ echo "PUSH_TIME=\$PUSH_TIME" >> \$GITHUB_ENV
38
+ echo "GITHUB_URL=\$GITHUB_URL" >> \$GITHUB_ENV
39
+ - name: Clone target repo
57
40
  run: |
58
41
  git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
59
42
  rm -rf preview/public/protected/\$DIR
60
43
  mkdir -p preview/public/protected/\$DIR
61
44
  cp -r ${response.sourcePath}/. preview/public/protected/\$DIR/
62
- cd preview
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
45
+ - name: Create log file
87
46
  run: |
88
- USER="\${{ github.actor }}"
89
- REPO="\${{ github.event.repository.name }}"
90
- DIR="\${USER}@\${REPO}"
91
- echo "USER=\$USER" >> \$GITHUB_ENV
92
- echo "REPO=\$REPO" >> \$GITHUB_ENV
93
- echo "DIR=\$DIR" >> \$GITHUB_ENV
94
-
95
- - name: Clone target repo and remove directory
47
+ cd preview/public/protected/\$DIR
48
+ echo "Last sync: \$PUSH_TIME" > .sync_log
49
+ echo "Last commit: \$COMMIT_MSG" >> .sync_log
50
+ echo "Commit SHA: \$COMMIT_SHA" >> .sync_log
51
+ echo "Source: \$USER/\$REPO" >> .sync_log
52
+ echo "View diff: \$GITHUB_URL" >> .sync_log
53
+ - name: Commit and push changes
96
54
  run: |
97
- git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
98
- rm -rf preview/public/protected/\$DIR
99
55
  cd preview
100
56
  git config user.name "GitHub Actions"
101
57
  git config user.email "actions@github.com"
102
58
  git add .
103
59
  if ! git diff --cached --quiet; then
104
- git commit -m "Remove directory from shared repo"
60
+ git commit -m "Sync from source repo"
105
61
  git push
106
62
  else
107
63
  echo "No changes to commit"
108
64
  fi
109
65
  `;
110
- }
111
-
112
66
  const destDir = path.join(process.cwd(), ".github", "workflows");
113
67
  fs.mkdirSync(destDir, { recursive: true });
114
68
  fs.writeFileSync(path.join(destDir, "sync.yml"), workflow);
115
-
116
- const actionText =
117
- response.action === "sync" ? "sync content to" : "delete content from";
118
69
  console.log(
119
- `✅ 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.`
70
+ "✅ 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
71
  );
121
72
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-preview-workflow",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "bin": {
5
5
  "create-preview-workflow": "index.js"
6
6
  },