@rpcbase/cli 0.115.0 → 0.116.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/cli",
3
- "version": "0.115.0",
3
+ "version": "0.116.0",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -0,0 +1,53 @@
1
+ export const purgeCloudflareCaches = async ({ env = process.env, logger = console } = {}) => {
2
+ const { CLOUDFLARE_ZONES, CLOUDFLARE_ZONE, CLOUDFLARE_TOKEN } = env
3
+
4
+ const zones = (CLOUDFLARE_ZONES || "")
5
+ .split(",")
6
+ .map((zone) => zone.trim())
7
+ .filter(Boolean)
8
+
9
+ if (!zones.length && CLOUDFLARE_ZONE) {
10
+ logger.warn("CLOUDFLARE_ZONE is deprecated; use CLOUDFLARE_ZONES as a comma-separated list of zones")
11
+ zones.push(CLOUDFLARE_ZONE)
12
+ }
13
+
14
+ logger.log("Cloudflare env", {
15
+ CLOUDFLARE_ZONES: JSON.stringify(CLOUDFLARE_ZONES),
16
+ CLOUDFLARE_ZONE: JSON.stringify(CLOUDFLARE_ZONE),
17
+ CLOUDFLARE_TOKEN: JSON.stringify(CLOUDFLARE_TOKEN),
18
+ })
19
+
20
+ if (!zones.length || !CLOUDFLARE_TOKEN) {
21
+ logger.warn("Cloudflare cache purge will not run: missing zones or CLOUDFLARE_TOKEN environment variable")
22
+ return
23
+ }
24
+
25
+ logger.log(`Purging Cloudflare cache for zone${zones.length > 1 ? "s" : ""}: ${zones.join(", ")}`)
26
+
27
+ const axios = (await import("axios")).default
28
+
29
+ for (const zone of zones) {
30
+ try {
31
+ const response = await axios.post(
32
+ `https://api.cloudflare.com/client/v4/zones/${zone}/purge_cache`,
33
+ { purge_everything: true },
34
+ {
35
+ headers: {
36
+ "Authorization": `Bearer ${CLOUDFLARE_TOKEN}`,
37
+ "Content-Type": "application/json",
38
+ },
39
+ },
40
+ )
41
+
42
+ if (response.data?.success) {
43
+ logger.log(`Successfully purged Cloudflare cache for zone: ${zone}`)
44
+ } else {
45
+ const errors = response.data?.errors
46
+ throw new Error(`Failed to purge Cloudflare cache for zone ${zone}${errors ? `: ${JSON.stringify(errors)}` : ""}`)
47
+ }
48
+ } catch (error) {
49
+ logger.error(`Error purging Cloudflare cache for zone ${zone}: ${error?.message || error}`)
50
+ throw error
51
+ }
52
+ }
53
+ }
package/src/cmd-deploy.js CHANGED
@@ -5,6 +5,8 @@ import os from "os"
5
5
 
6
6
  import validator from "validator"
7
7
 
8
+ import { purgeCloudflareCaches } from "./cloudflare-purge.js"
9
+
8
10
 
9
11
  export const deploy = async (argv) => {
10
12
  const log = (message) => {
@@ -139,41 +141,11 @@ export const deploy = async (argv) => {
139
141
  }
140
142
  }
141
143
 
142
- // Purge Cloudflare Cache if CLOUDFLARE_ZONE and CLOUDFLARE_TOKEN are set
143
- const { CLOUDFLARE_ZONE, CLOUDFLARE_TOKEN } = process.env
144
-
145
- console.log("Cloudflare env", {
146
- CLOUDFLARE_ZONE: JSON.stringify(CLOUDFLARE_ZONE),
147
- CLOUDFLARE_TOKEN: JSON.stringify(CLOUDFLARE_TOKEN),
148
- })
149
-
150
- if (!CLOUDFLARE_ZONE || !CLOUDFLARE_TOKEN) {
151
- console.warn("Cloudflare cache purge will not run: missing CLOUDFLARE_ZONE or CLOUDFLARE_TOKEN environment variables")
152
- } else {
153
- console.log("Purging Cloudflare cache...")
154
-
155
- try {
156
- const axios = (await import("axios")).default
157
- const response = await axios.post(
158
- `https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE}/purge_cache`,
159
- { purge_everything: true },
160
- {
161
- headers: {
162
- "Authorization": `Bearer ${CLOUDFLARE_TOKEN}`,
163
- "Content-Type": "application/json"
164
- }
165
- }
166
- )
167
-
168
- if (response.data.success) {
169
- console.log("Successfully purged Cloudflare cache for zone:", CLOUDFLARE_ZONE)
170
- } else {
171
- console.warn("Failed to purge Cloudflare cache:", response.data.errors)
172
- }
173
- } catch (error) {
174
- console.error("Error purging Cloudflare cache:", error.message)
175
- process.exit(1)
176
- }
144
+ try {
145
+ await purgeCloudflareCaches({ env: process.env, logger: console })
146
+ } catch (error) {
147
+ console.error(error?.message ? `Error purging Cloudflare cache: ${error.message}` : "Error purging Cloudflare cache")
148
+ process.exit(1)
177
149
  }
178
150
 
179
151
  console.log("Done.")