From 3d40327d57ca8685a8c04232c59a66744fe20792 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:00:26 -0600 Subject: [PATCH 1/7] wip --- scripts/package.json | 1 + scripts/purge-jsdeliver-cache.js | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 scripts/purge-jsdeliver-cache.js diff --git a/scripts/package.json b/scripts/package.json index c68ec6d17..01851ce37 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -6,6 +6,7 @@ ".": "yarn run -T turbo run --filter=@internal/scripts", "lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'", "create-release-from-tags": "yarn ts-node-script --files create-release-from-tags/run.ts", + "purge-cdn-cache": "node purge-jsdeliver-cache", "test": "yarn jest", "tsc": "yarn run -T tsc", "eslint": "yarn run -T eslint", diff --git a/scripts/purge-jsdeliver-cache.js b/scripts/purge-jsdeliver-cache.js new file mode 100644 index 000000000..acfd472b2 --- /dev/null +++ b/scripts/purge-jsdeliver-cache.js @@ -0,0 +1,47 @@ +const fs = require('node:fs') +const path = require('node:path') + +/** + * Given a package name and a relative path to the dist folder -- it reads all the files and runs them through the jsdelivr cache purge API. + */ +const purgeJsDelivrCache = async (packageName, relativePath) => { + const gitRoot = path.resolve(__dirname, '..') + const fullPath = path.join(gitRoot, relativePath) + + if (!fs.existsSync(fullPath)) { + console.error(`Path does not exist: ${fullPath}`) + process.exit(1) + } + + const files = fs.readdirSync(fullPath) + + console.log( + `Purging files for ${packageName}: ${JSON.stringify(files, null, 2)}` + ) + for (const file of files) { + const filePath = path.join(relativePath, file) + const url = `https://purge.jsdelivr.net/npm/${packageName}/${filePath}` + + try { + const response = await fetch(url) + if (response.ok) { + console.log(`Purged: ${url} - Status: ${response.status}`) + } else { + console.error(`Failed to purge: ${url} - Status: ${response.status}`) + } + } catch (error) { + console.error(`Failed to purge: ${url} - Error: ${error.message}`) + } + } +} + +const [packageName, relativePath] = process.argv.slice(2) + +if (!packageName || !relativePath) { + console.error( + 'Usage: node purge-jsdeliver-cache-signals.js ' + ) + process.exit(1) +} + +purgeJsDelivrCache(packageName, relativePath) From 9ffb0cb59eac32bf4f41051d93149a3a32cdddd0 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:07:14 -0600 Subject: [PATCH 2/7] wip --- scripts/purge-jsdeliver-cache.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/scripts/purge-jsdeliver-cache.js b/scripts/purge-jsdeliver-cache.js index acfd472b2..473ba21c9 100644 --- a/scripts/purge-jsdeliver-cache.js +++ b/scripts/purge-jsdeliver-cache.js @@ -1,6 +1,6 @@ +const { execSync } = require('node:child_process') const fs = require('node:fs') const path = require('node:path') - /** * Given a package name and a relative path to the dist folder -- it reads all the files and runs them through the jsdelivr cache purge API. */ @@ -13,6 +13,24 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { process.exit(1) } + // Check if the current git HEAD has a tag containing the package name + try { + const tags = execSync('git tag --contains HEAD', { cwd: gitRoot }) + .toString() + .split('\n') + .filter((tag) => tag.includes(packageName)) + + if (tags.length === 0) { + console.log( + `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting script.` + ) + process.exit(0) + } + } catch (error) { + console.error(`Failed to check git tags: ${error.message}`) + process.exit(1) + } + const files = fs.readdirSync(fullPath) console.log( From 5db67bcf7398dae8b9b38fe0d4d847766b1dd31e Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:08:31 -0600 Subject: [PATCH 3/7] wip --- scripts/purge-jsdeliver-cache.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/purge-jsdeliver-cache.js b/scripts/purge-jsdeliver-cache.js index 473ba21c9..d70048320 100644 --- a/scripts/purge-jsdeliver-cache.js +++ b/scripts/purge-jsdeliver-cache.js @@ -15,12 +15,12 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { // Check if the current git HEAD has a tag containing the package name try { - const tags = execSync('git tag --contains HEAD', { cwd: gitRoot }) - .toString() - .split('\n') - .filter((tag) => tag.includes(packageName)) + const tags = execSync('git tag --contains HEAD', { + cwd: gitRoot, + }).toString() - if (tags.length === 0) { + const hasTags = tags.includes(packageName) + if (!hasTags) { console.log( `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting script.` ) From f05ac327ac7595d74eff7172dde887351e3ff747 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:08:51 -0600 Subject: [PATCH 4/7] wip --- scripts/purge-jsdeliver-cache.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/purge-jsdeliver-cache.js b/scripts/purge-jsdeliver-cache.js index d70048320..f065a4f81 100644 --- a/scripts/purge-jsdeliver-cache.js +++ b/scripts/purge-jsdeliver-cache.js @@ -19,8 +19,8 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { cwd: gitRoot, }).toString() - const hasTags = tags.includes(packageName) - if (!hasTags) { + const hasMatchingTag = tags.includes(packageName) + if (!hasMatchingTag) { console.log( `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting script.` ) From 2e2fe839ae75bb2704cc8fde070ca60b4a351ade Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:10:07 -0600 Subject: [PATCH 5/7] wip --- scripts/purge-jsdeliver-cache.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/purge-jsdeliver-cache.js b/scripts/purge-jsdeliver-cache.js index f065a4f81..7dba04fe1 100644 --- a/scripts/purge-jsdeliver-cache.js +++ b/scripts/purge-jsdeliver-cache.js @@ -3,6 +3,7 @@ const fs = require('node:fs') const path = require('node:path') /** * Given a package name and a relative path to the dist folder -- it reads all the files and runs them through the jsdelivr cache purge API. + * Will abort the API unless the current commit has a tag with the package name on it. */ const purgeJsDelivrCache = async (packageName, relativePath) => { const gitRoot = path.resolve(__dirname, '..') @@ -22,7 +23,7 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { const hasMatchingTag = tags.includes(packageName) if (!hasMatchingTag) { console.log( - `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting script.` + `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting cache purge.` ) process.exit(0) } From 3a9e02a2984deff83311557a79151a2fb87ae8e6 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:44:37 -0600 Subject: [PATCH 6/7] wip --- package.json | 2 +- scripts/package.json | 4 +- ...-jsdeliver-cache.js => purge-cdn-cache.js} | 38 +++++++++++-------- 3 files changed, 26 insertions(+), 18 deletions(-) rename scripts/{purge-jsdeliver-cache.js => purge-cdn-cache.js} (61%) diff --git a/package.json b/package.json index e3c4b30bf..c9e67ce50 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "postinstall": "husky install", "changeset": "changeset", "update-versions-and-changelogs": "changeset version && yarn version-run-all && bash scripts/update-lockfile.sh", - "release": "yarn clean && yarn build --force && changeset publish && git push origin HEAD:master --follow-tags --no-verify", + "release": "yarn clean && yarn build --force && changeset publish && git push origin HEAD:master --follow-tags --no-verify && yarn scripts purge-cdn-cache", "version-run-all": "yarn workspaces foreach -vpt --no-private run version", "core": "yarn workspace @segment/analytics-core", "browser": "yarn workspace @segment/analytics-next", diff --git a/scripts/package.json b/scripts/package.json index 01851ce37..c04a50d3a 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -6,7 +6,9 @@ ".": "yarn run -T turbo run --filter=@internal/scripts", "lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'", "create-release-from-tags": "yarn ts-node-script --files create-release-from-tags/run.ts", - "purge-cdn-cache": "node purge-jsdeliver-cache", + "purge-cdn-cache": "yarn concurrently 'yarn:purge-cdn-cache:*'", + "purge-cdn-cache:signals": "node purge-cdn-cache.js '@segment/analytics-signals' 'packages/signals/signals/dist/umd'", + "purge-cdn-cache:consent": "node purge-cdn-cache.js '@segment/analytics-consent-wrapper-onetrust' 'packages/consent/consent-wrapper-onetrust/dist/umd'", "test": "yarn jest", "tsc": "yarn run -T tsc", "eslint": "yarn run -T eslint", diff --git a/scripts/purge-jsdeliver-cache.js b/scripts/purge-cdn-cache.js similarity index 61% rename from scripts/purge-jsdeliver-cache.js rename to scripts/purge-cdn-cache.js index 7dba04fe1..5facd9a2e 100644 --- a/scripts/purge-jsdeliver-cache.js +++ b/scripts/purge-cdn-cache.js @@ -6,6 +6,7 @@ const path = require('node:path') * Will abort the API unless the current commit has a tag with the package name on it. */ const purgeJsDelivrCache = async (packageName, relativePath) => { + console.log(`\n\n`) const gitRoot = path.resolve(__dirname, '..') const fullPath = path.join(gitRoot, relativePath) @@ -14,22 +15,27 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { process.exit(1) } + // Only run this script if the given package has been published // Check if the current git HEAD has a tag containing the package name - try { - const tags = execSync('git tag --contains HEAD', { - cwd: gitRoot, - }).toString() + // This is a bit odd versus just having the cache purge on a prepublish hooks, but publish hooks like post/prepublish had been flaky on CI with changesets when I was setting this up. + if (process.env.CI) { + console.log('Searching for matching tag for package...') + try { + const tags = execSync('git tag --contains HEAD', { + cwd: gitRoot, + }).toString() - const hasMatchingTag = tags.includes(packageName) - if (!hasMatchingTag) { - console.log( - `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting cache purge.` - ) - process.exit(0) + const hasMatchingTag = tags.includes(packageName) + if (!hasMatchingTag) { + console.log( + `No tags containing the package name "${packageName}" found on the current git HEAD. Aborting cache purge.` + ) + process.exit(0) + } + } catch (error) { + console.error(`Failed to check git tags: ${error.message}`) + process.exit(1) } - } catch (error) { - console.error(`Failed to check git tags: ${error.message}`) - process.exit(1) } const files = fs.readdirSync(fullPath) @@ -39,19 +45,19 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { ) for (const file of files) { const filePath = path.join(relativePath, file) + console.log(`Purging cache: ${file}...`) const url = `https://purge.jsdelivr.net/npm/${packageName}/${filePath}` try { const response = await fetch(url) - if (response.ok) { - console.log(`Purged: ${url} - Status: ${response.status}`) - } else { + if (!response.ok) { console.error(`Failed to purge: ${url} - Status: ${response.status}`) } } catch (error) { console.error(`Failed to purge: ${url} - Error: ${error.message}`) } } + console.log(`\nPurge of ${packageName} finished.`) } const [packageName, relativePath] = process.argv.slice(2) From 6d3797dbba6f085373483d2ca9cc9f6c72d0d4a3 Mon Sep 17 00:00:00 2001 From: Seth Silesky <5115498+silesky@users.noreply.github.com> Date: Wed, 5 Mar 2025 12:15:59 -0600 Subject: [PATCH 7/7] update cdn cache --- scripts/purge-cdn-cache.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts/purge-cdn-cache.js b/scripts/purge-cdn-cache.js index 5facd9a2e..b13c0fa42 100644 --- a/scripts/purge-cdn-cache.js +++ b/scripts/purge-cdn-cache.js @@ -3,7 +3,7 @@ const fs = require('node:fs') const path = require('node:path') /** * Given a package name and a relative path to the dist folder -- it reads all the files and runs them through the jsdelivr cache purge API. - * Will abort the API unless the current commit has a tag with the package name on it. + * On CI, will abort the purge unless the current commit has a tag with the package name on it. */ const purgeJsDelivrCache = async (packageName, relativePath) => { console.log(`\n\n`) @@ -63,9 +63,7 @@ const purgeJsDelivrCache = async (packageName, relativePath) => { const [packageName, relativePath] = process.argv.slice(2) if (!packageName || !relativePath) { - console.error( - 'Usage: node purge-jsdeliver-cache-signals.js ' - ) + console.error('Usage: node purge-cdn-cache.js ') process.exit(1) }