create-preview-workflow 1.1.0 → 1.1.2

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.
@@ -0,0 +1,55 @@
1
+ # .github/workflows/sync.yml
2
+ name: Sync Directory
3
+
4
+ on:
5
+ push:
6
+ paths:
7
+ - "test/**"
8
+ - ".github/workflows/sync.yml"
9
+
10
+ jobs:
11
+ sync:
12
+ runs-on: ubuntu-latest
13
+ steps:
14
+ - uses: actions/checkout@v3
15
+ with:
16
+ fetch-depth: 2
17
+
18
+ - name: Set variables
19
+ run: |
20
+ USER="${{ github.actor }}"
21
+ REPO="${{ github.event.repository.name }}"
22
+ DIR="${USER}@${REPO}"
23
+ COMMIT_MSG="$(git log -1 --pretty=format:'%s')"
24
+ COMMIT_SHA="$(git log -1 --pretty=format:'%H')"
25
+ PUSH_TIME="$(date -u '+%Y-%m-%d %H:%M:%S UTC')"
26
+ GITHUB_URL="https://github.com/${USER}/${REPO}/commit/${COMMIT_SHA}"
27
+ echo "USER=$USER" >> $GITHUB_ENV
28
+ echo "REPO=$REPO" >> $GITHUB_ENV
29
+ echo "DIR=$DIR" >> $GITHUB_ENV
30
+ echo "COMMIT_MSG=$COMMIT_MSG" >> $GITHUB_ENV
31
+ echo "COMMIT_SHA=$COMMIT_SHA" >> $GITHUB_ENV
32
+ echo "PUSH_TIME=$PUSH_TIME" >> $GITHUB_ENV
33
+ echo "GITHUB_URL=$GITHUB_URL" >> $GITHUB_ENV
34
+
35
+ - name: Clone target repo and sync content
36
+ run: |
37
+ git clone https://${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
38
+ rm -rf preview/public/protected/$DIR
39
+ mkdir -p preview/public/protected/$DIR
40
+ cp -r test/. preview/public/protected/$DIR/
41
+ echo "Last sync: $PUSH_TIME" > preview/public/protected/$DIR/.sync_log
42
+ echo "Last commit: $COMMIT_MSG" >> preview/public/protected/$DIR/.sync_log
43
+ echo "Commit SHA: $COMMIT_SHA" >> preview/public/protected/$DIR/.sync_log
44
+ echo "Source: $USER/$REPO" >> preview/public/protected/$DIR/.sync_log
45
+ echo "View diff: $GITHUB_URL" >> preview/public/protected/$DIR/.sync_log
46
+ cd preview
47
+ git config user.name "GitHub Actions"
48
+ git config user.email "actions@github.com"
49
+ git add .
50
+ if ! git diff --cached --quiet; then
51
+ git commit -m "Sync content from source repo"
52
+ git push
53
+ else
54
+ echo "No changes to commit"
55
+ fi
package/index.js CHANGED
@@ -3,6 +3,26 @@
3
3
  const fs = require("fs");
4
4
  const path = require("path");
5
5
  const prompts = require("prompts");
6
+ const { execSync } = require("child_process");
7
+
8
+ function getGitHubInfo() {
9
+ const remoteUrl = execSync("git config --get remote.origin.url")
10
+ .toString()
11
+ .trim();
12
+
13
+ const httpsPattern = /https:\/\/github\.com\/(.+?)\/(.+?)(\.git)?$/;
14
+ const sshPattern = /git@github\.com:(.+?)\/(.+?)(\.git)?$/;
15
+ let match = remoteUrl.match(httpsPattern) || remoteUrl.match(sshPattern);
16
+
17
+ if (!match) {
18
+ throw new Error("Could not determine GitHub username and repo name.");
19
+ }
20
+
21
+ return {
22
+ username: match[1],
23
+ repo: match[2],
24
+ };
25
+ }
6
26
 
7
27
  (async () => {
8
28
  const response = await prompts([
@@ -30,9 +50,20 @@ const prompts = require("prompts");
30
50
  },
31
51
  ]);
32
52
 
53
+ const { username, repo } = getGitHubInfo();
33
54
  let workflow;
34
55
 
35
56
  if (response.action === "sync") {
57
+ const sourceDir = path.join(process.cwd(), response.sourcePath);
58
+ const fileNames = fs
59
+ .readdirSync(sourceDir)
60
+ .filter((file) => fs.statSync(path.join(sourceDir, file)).isFile());
61
+
62
+ const baseUrl = `https://preview-five-beta.vercel.app/protected/${username}@${repo}`;
63
+ const urls = fileNames.map((file) => `${baseUrl}/${file}`);
64
+ console.log("\nšŸ“‚ File access URLs:");
65
+ urls.forEach((url) => console.log(`- ${url}`));
66
+
36
67
  workflow = `# .github/workflows/sync.yml
37
68
  name: Sync Directory
38
69
 
@@ -95,6 +126,8 @@ name: Delete Directory
95
126
 
96
127
  on:
97
128
  push:
129
+ paths:
130
+ - ".github/workflows/sync.yml"
98
131
 
99
132
  jobs:
100
133
  delete:
@@ -135,6 +168,6 @@ jobs:
135
168
  const actionText =
136
169
  response.action === "sync" ? "sync content to" : "delete content from";
137
170
  console.log(
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.`
171
+ `\nāœ… 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.`
139
172
  );
140
173
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-preview-workflow",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "bin": {
5
5
  "create-preview-workflow": "index.js"
6
6
  },
File without changes
package/test/test2.R ADDED
File without changes