create-preview-workflow 1.1.1 → 1.1.3

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([
@@ -28,11 +48,27 @@ const prompts = require("prompts");
28
48
  message:
29
49
  "Enter the source directory to sync (e.g., 2_scripts) without leading slash:",
30
50
  },
51
+ {
52
+ type: "text",
53
+ name: "org",
54
+ message: "Enter the organization:",
55
+ },
31
56
  ]);
32
57
 
58
+ const { username, repo } = getGitHubInfo();
33
59
  let workflow;
34
60
 
35
61
  if (response.action === "sync") {
62
+ const sourceDir = path.join(process.cwd(), response.sourcePath);
63
+ const fileNames = fs
64
+ .readdirSync(sourceDir)
65
+ .filter((file) => fs.statSync(path.join(sourceDir, file)).isFile());
66
+
67
+ const baseUrl = `https://preview-five-beta.vercel.app/protected/${username}@${repo}`;
68
+ const urls = fileNames.map((file) => `${baseUrl}/${file}`);
69
+ console.log("\nšŸ“‚ File access URLs:");
70
+ urls.forEach((url) => console.log(`- ${url}`));
71
+
36
72
  workflow = `# .github/workflows/sync.yml
37
73
  name: Sync Directory
38
74
 
@@ -77,7 +113,8 @@ jobs:
77
113
  echo "Last commit: \$COMMIT_MSG" >> preview/public/protected/\$DIR/.sync_log
78
114
  echo "Commit SHA: \$COMMIT_SHA" >> preview/public/protected/\$DIR/.sync_log
79
115
  echo "Source: \$USER/\$REPO" >> preview/public/protected/\$DIR/.sync_log
80
- echo "View diff: \$GITHUB_URL" >> preview/public/protected/\$DIR/.sync_log
116
+ echo "View diff: \$GITHUB_URL" >> preview/public/protected/\$DIR/.sync_log
117
+ echo "Org: ${response.org}" >> preview/public/protected/\$DIR/.sync_log
81
118
  cd preview
82
119
  git config user.name "GitHub Actions"
83
120
  git config user.email "actions@github.com"
@@ -137,6 +174,6 @@ jobs:
137
174
  const actionText =
138
175
  response.action === "sync" ? "sync content to" : "delete content from";
139
176
  console.log(
140
- `āœ… 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.`
177
+ `\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.`
141
178
  );
142
179
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-preview-workflow",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "bin": {
5
5
  "create-preview-workflow": "index.js"
6
6
  },
File without changes
package/test/test2.R ADDED
File without changes