gatsby-attainlabs-cms 1.0.48 → 1.1.0
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.
- package/README.md +21 -6
- package/gatsby-node.js +93 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -55,17 +55,27 @@ module.exports = {
|
|
|
55
55
|
{
|
|
56
56
|
resolve: "gatsby-attainlabs-cms",
|
|
57
57
|
options: {
|
|
58
|
-
brand: "Cash Money", // LendDirect | Cash Money | Heights Finance
|
|
58
|
+
brand: "Cash Money", // LendDirect | Cash Money | Heights Finance | Attain Finance
|
|
59
59
|
// personalAccessToken: "optional-fallback", // not recommended, but supported
|
|
60
|
-
environment: "production", // production | dev
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
environment: "production", // production | staging | dev
|
|
61
|
+
// azureBranch: "my-feature-branch", // Optional: Override branch for non-production environments
|
|
62
|
+
fetch: ["blogs", "disclaimers", "faqs"], // Array of data to fetch from database
|
|
63
|
+
debug: false, // Console logs blocks created
|
|
63
64
|
},
|
|
64
65
|
},
|
|
65
66
|
],
|
|
66
67
|
};
|
|
67
68
|
```
|
|
68
69
|
|
|
70
|
+
### Environment Options
|
|
71
|
+
|
|
72
|
+
- **`production`**: Forces content sync from the `master` branch.
|
|
73
|
+
- **`staging`**: Automatically fetches content from the **latest active Pull Request** branch in Azure DevOps. Falls back to `master` if no PR is active.
|
|
74
|
+
- **`dev`**: Skips component sync and data fetching. Useful for local development speed.
|
|
75
|
+
- **Default behavior**: If `environment` is unspecified or other values, it defaults to the `azureBranch` option or `master`.
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
|
|
69
79
|
---
|
|
70
80
|
|
|
71
81
|
## Behavior
|
|
@@ -79,7 +89,7 @@ module.exports = {
|
|
|
79
89
|
|
|
80
90
|
- If the PAT is missing, the plugin will **warn and skip execution** instead of failing the build.
|
|
81
91
|
|
|
82
|
-
- If the `brand` option is missing or invalid, the plugin will **throw an error** and stop the build.
|
|
92
|
+
- If the `brand` option is missing or invalid, the plugin will **throw an error** and stop the build.
|
|
83
93
|
Valid values are:
|
|
84
94
|
- `LendDirect`
|
|
85
95
|
- `Cash Money`
|
|
@@ -95,7 +105,9 @@ module.exports = {
|
|
|
95
105
|
If you see:
|
|
96
106
|
|
|
97
107
|
```
|
|
108
|
+
|
|
98
109
|
⚠️ [gatsby-attainlabs-cms] No PERSONAL_ACCESS_TOKEN found...
|
|
110
|
+
|
|
99
111
|
```
|
|
100
112
|
|
|
101
113
|
➡️ Double-check that `.env` exists and is loaded in `gatsby-config.js`.
|
|
@@ -107,8 +119,10 @@ If you see:
|
|
|
107
119
|
If you see:
|
|
108
120
|
|
|
109
121
|
```
|
|
122
|
+
|
|
110
123
|
[gatsby-attainlabs-cms] Invalid or missing "brand" option.
|
|
111
124
|
You must specify one of: LendDirect, Cash Money, Heights Finance
|
|
125
|
+
|
|
112
126
|
```
|
|
113
127
|
|
|
114
128
|
➡️ Make sure you pass a valid brand in `gatsby-config.js`.
|
|
@@ -117,5 +131,6 @@ You must specify one of: LendDirect, Cash Money, Heights Finance
|
|
|
117
131
|
|
|
118
132
|
### Token in client code
|
|
119
133
|
|
|
120
|
-
Don’t use `GATSBY_PERSONAL_ACCESS_TOKEN`. That will leak your secret into the browser bundle.
|
|
134
|
+
Don’t use `GATSBY_PERSONAL_ACCESS_TOKEN`. That will leak your secret into the browser bundle.
|
|
121
135
|
Always use `PERSONAL_ACCESS_TOKEN`.
|
|
136
|
+
```
|
package/gatsby-node.js
CHANGED
|
@@ -133,10 +133,102 @@ exports.onPreInit = async (_, pluginOptions) => {
|
|
|
133
133
|
);
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
// Helper to fetch latest PR branch
|
|
137
|
+
const getLatestPullRequestBranch = (org, project, repo, pat) => {
|
|
138
|
+
return new Promise((resolve, reject) => {
|
|
139
|
+
const options = {
|
|
140
|
+
headers: {
|
|
141
|
+
Authorization: `Basic ${Buffer.from(`:${pat}`).toString("base64")}`,
|
|
142
|
+
"User-Agent": "gatsby-attainlabs-cms",
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// Fetch top 10 active PRs to ensure we find the latest one
|
|
147
|
+
// Note: Azure DevOps usually sorts by ID desc (creation time), but fetching a batch is safer.
|
|
148
|
+
const url = `https://dev.azure.com/${org}/${project}/_apis/git/repositories/${encodeURIComponent(
|
|
149
|
+
repo
|
|
150
|
+
)}/pullrequests?searchCriteria.status=active&top=10&api-version=7.0`;
|
|
151
|
+
|
|
152
|
+
https
|
|
153
|
+
.get(url, options, (res) => {
|
|
154
|
+
let data = "";
|
|
155
|
+
res.on("data", (chunk) => (data += chunk));
|
|
156
|
+
res.on("end", () => {
|
|
157
|
+
if (res.statusCode !== 200) {
|
|
158
|
+
console.error(
|
|
159
|
+
`Failed to fetch PRs: ${res.statusCode} ${res.statusMessage}`
|
|
160
|
+
);
|
|
161
|
+
resolve(null);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const result = JSON.parse(data);
|
|
166
|
+
if (result.value && result.value.length > 0) {
|
|
167
|
+
// Sort by creationDate descending to get the latest created PR
|
|
168
|
+
// (Since we can't easily get 'last updated/pushed' without extra calls, creation is the standard proxy)
|
|
169
|
+
const latestPr = result.value.sort(
|
|
170
|
+
(a, b) =>
|
|
171
|
+
new Date(b.creationDate).getTime() -
|
|
172
|
+
new Date(a.creationDate).getTime()
|
|
173
|
+
)[0];
|
|
174
|
+
|
|
175
|
+
const sourceRef = latestPr.sourceRefName;
|
|
176
|
+
const branchName = sourceRef.replace("refs/heads/", "");
|
|
177
|
+
resolve(branchName);
|
|
178
|
+
} else {
|
|
179
|
+
resolve(null);
|
|
180
|
+
}
|
|
181
|
+
} catch (e) {
|
|
182
|
+
console.error("Error parsing PR response:", e);
|
|
183
|
+
resolve(null);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
})
|
|
187
|
+
.on("error", (e) => {
|
|
188
|
+
console.error("Request error fetching PRs:", e);
|
|
189
|
+
resolve(null);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
|
|
136
194
|
const org = "CuroFinTech";
|
|
137
195
|
const project = "Marketing";
|
|
138
196
|
const repo = "Attain Labs";
|
|
139
|
-
|
|
197
|
+
|
|
198
|
+
let branch = azureBranch || "master";
|
|
199
|
+
|
|
200
|
+
if (environment === "production") {
|
|
201
|
+
branch = "master";
|
|
202
|
+
} else if (environment === "staging") {
|
|
203
|
+
try {
|
|
204
|
+
console.log(
|
|
205
|
+
"ℹ️ [gatsby-attainlabs-cms] Staging mode: Checking for active PRs..."
|
|
206
|
+
);
|
|
207
|
+
const latestPrBranch = await getLatestPullRequestBranch(
|
|
208
|
+
org,
|
|
209
|
+
project,
|
|
210
|
+
repo,
|
|
211
|
+
pat
|
|
212
|
+
);
|
|
213
|
+
if (latestPrBranch) {
|
|
214
|
+
branch = latestPrBranch;
|
|
215
|
+
console.log(
|
|
216
|
+
`ℹ️ [gatsby-attainlabs-cms] Staging mode: Using latest PR branch '${branch}'`
|
|
217
|
+
);
|
|
218
|
+
} else {
|
|
219
|
+
console.warn(
|
|
220
|
+
"⚠️ [gatsby-attainlabs-cms] Staging mode: No active PRs found. Falling back to 'master'."
|
|
221
|
+
);
|
|
222
|
+
branch = "master";
|
|
223
|
+
}
|
|
224
|
+
} catch (e) {
|
|
225
|
+
console.error(
|
|
226
|
+
"⚠️ [gatsby-attainlabs-cms] Failed to fetch latest PR branch, falling back to 'master':",
|
|
227
|
+
e
|
|
228
|
+
);
|
|
229
|
+
branch = "master";
|
|
230
|
+
}
|
|
231
|
+
}
|
|
140
232
|
|
|
141
233
|
const localTargets = [
|
|
142
234
|
{
|