create-preview-workflow 1.1.3 → 1.1.4

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 +71 -43
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -46,7 +46,7 @@ function getGitHubInfo() {
46
46
  type: (prev) => (prev === "sync" ? "text" : null),
47
47
  name: "sourcePath",
48
48
  message:
49
- "Enter the source directory to sync (e.g., 2_scripts) without leading slash:",
49
+ "Enter the source directory to sync (e.g., qmd) without leading slash:",
50
50
  },
51
51
  {
52
52
  type: "text",
@@ -60,14 +60,22 @@ function getGitHubInfo() {
60
60
 
61
61
  if (response.action === "sync") {
62
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
63
 
67
- const baseUrl = `https://preview-five-beta.vercel.app/protected/${username}@${repo}`;
64
+ // Check if source directory exists and get file names for URL display
65
+ let fileNames = [];
66
+ if (fs.existsSync(sourceDir)) {
67
+ fileNames = fs
68
+ .readdirSync(sourceDir)
69
+ .filter((file) => fs.statSync(path.join(sourceDir, file)).isFile());
70
+ }
71
+
72
+ const baseUrl = `http://preview.neurasense.io:30010/${username}@${repo}`;
68
73
  const urls = fileNames.map((file) => `${baseUrl}/${file}`);
69
- console.log("\nšŸ“‚ File access URLs:");
70
- urls.forEach((url) => console.log(`- ${url}`));
74
+
75
+ if (urls.length > 0) {
76
+ console.log("\nšŸ“‚ File access URLs:");
77
+ urls.forEach((url) => console.log(`- ${url}`));
78
+ }
71
79
 
72
80
  workflow = `# .github/workflows/sync.yml
73
81
  name: Sync Directory
@@ -79,7 +87,7 @@ on:
79
87
  - ".github/workflows/sync.yml"
80
88
 
81
89
  jobs:
82
- sync:
90
+ notify:
83
91
  runs-on: ubuntu-latest
84
92
  steps:
85
93
  - uses: actions/checkout@v3
@@ -103,28 +111,41 @@ jobs:
103
111
  echo "PUSH_TIME=\$PUSH_TIME" >> \$GITHUB_ENV
104
112
  echo "GITHUB_URL=\$GITHUB_URL" >> \$GITHUB_ENV
105
113
 
106
- - name: Clone target repo and sync content
114
+ - name: Call Preview Webhook
107
115
  run: |
108
- git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
109
- rm -rf preview/public/protected/\$DIR
110
- mkdir -p preview/public/protected/\$DIR
111
- cp -r ${response.sourcePath}/. preview/public/protected/\$DIR/
112
- echo "Last sync: \$PUSH_TIME" > preview/public/protected/\$DIR/.sync_log
113
- echo "Last commit: \$COMMIT_MSG" >> preview/public/protected/\$DIR/.sync_log
114
- echo "Commit SHA: \$COMMIT_SHA" >> preview/public/protected/\$DIR/.sync_log
115
- echo "Source: \$USER/\$REPO" >> 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
118
- cd preview
119
- git config user.name "GitHub Actions"
120
- git config user.email "actions@github.com"
121
- git add .
122
- if ! git diff --cached --quiet; then
123
- git commit -m "Sync content from source repo"
124
- git push
125
- else
126
- echo "No changes to commit"
127
- fi
116
+ curl -X POST http://preview.neurasense.io:30010/deploy \\
117
+ -H "Content-Type: application/json" \\
118
+ -d "\$(jq -n \\
119
+ --arg repo "\${{ github.repository }}" \\
120
+ --arg actor "\${{ github.actor }}" \\
121
+ --arg ref "\${{ github.ref }}" \\
122
+ --arg sha "\${{ github.sha }}" \\
123
+ --arg org "${response.org}" \\
124
+ --arg user "\$USER" \\
125
+ --arg repo_name "\$REPO" \\
126
+ --arg dir "\$DIR" \\
127
+ --arg commit_msg "\$COMMIT_MSG" \\
128
+ --arg commit_sha "\$COMMIT_SHA" \\
129
+ --arg push_time "\$PUSH_TIME" \\
130
+ --arg github_url "\$GITHUB_URL" \\
131
+ --arg source_path "${response.sourcePath}" \\
132
+ '{
133
+ repository: \$repo,
134
+ actor: \$actor,
135
+ ref: \$ref,
136
+ sha: \$sha,
137
+ org: \$org,
138
+ source_path: \$source_path,
139
+ sync_log: {
140
+ "Last sync": \$push_time,
141
+ "Last commit": \$commit_msg,
142
+ "Commit SHA": \$commit_sha,
143
+ "Source": (\$user + "/" + \$repo_name),
144
+ "View diff": \$github_url,
145
+ "Org": \$org,
146
+ directory: \$dir
147
+ }
148
+ }')"
128
149
  `;
129
150
  } else {
130
151
  workflow = `# .github/workflows/sync.yml
@@ -150,20 +171,21 @@ jobs:
150
171
  echo "REPO=\$REPO" >> \$GITHUB_ENV
151
172
  echo "DIR=\$DIR" >> \$GITHUB_ENV
152
173
 
153
- - name: Clone target repo and remove directory
174
+ - name: Call Delete Webhook
154
175
  run: |
155
- git clone https://\${{ secrets.GH_SYNC_TOKEN }}@github.com/ssm123ssm/preview.git
156
- rm -rf preview/public/protected/\$DIR
157
- cd preview
158
- git config user.name "GitHub Actions"
159
- git config user.email "actions@github.com"
160
- git add .
161
- if ! git diff --cached --quiet; then
162
- git commit -m "Remove directory from shared repo"
163
- git push
164
- else
165
- echo "No changes to commit"
166
- fi
176
+ curl -X POST http://preview.neurasense.io:30010/delete \\
177
+ -H "Content-Type: application/json" \\
178
+ -d "\$(jq -n \\
179
+ --arg repo "\${{ github.repository }}" \\
180
+ --arg actor "\${{ github.actor }}" \\
181
+ --arg user "\$USER" \\
182
+ --arg repo_name "\$REPO" \\
183
+ --arg dir "\$DIR" \\
184
+ '{
185
+ repository: \$repo,
186
+ actor: \$actor,
187
+ directory: \$dir
188
+ }')"
167
189
  `;
168
190
  }
169
191
 
@@ -174,6 +196,12 @@ jobs:
174
196
  const actionText =
175
197
  response.action === "sync" ? "sync content to" : "delete content from";
176
198
  console.log(
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.`
199
+ `\nāœ… GitHub Actions workflow to ${actionText} shared directory created at .github/workflows/sync.yml.`
178
200
  );
201
+
202
+ if (response.action === "sync") {
203
+ console.log(
204
+ `\nšŸ“” The workflow will call the webhook at http://preview.neurasense.io:30010/deploy with the source directory "${response.sourcePath}" included in the payload.`
205
+ );
206
+ }
179
207
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-preview-workflow",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "bin": {
5
5
  "create-preview-workflow": "index.js"
6
6
  },