Skip to content

Commit 3a9e02a

Browse files
committed
wip
1 parent 2e2fe83 commit 3a9e02a

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"postinstall": "husky install",
2525
"changeset": "changeset",
2626
"update-versions-and-changelogs": "changeset version && yarn version-run-all && bash scripts/update-lockfile.sh",
27-
"release": "yarn clean && yarn build --force && changeset publish && git push origin HEAD:master --follow-tags --no-verify",
27+
"release": "yarn clean && yarn build --force && changeset publish && git push origin HEAD:master --follow-tags --no-verify && yarn scripts purge-cdn-cache",
2828
"version-run-all": "yarn workspaces foreach -vpt --no-private run version",
2929
"core": "yarn workspace @segment/analytics-core",
3030
"browser": "yarn workspace @segment/analytics-next",

scripts/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
".": "yarn run -T turbo run --filter=@internal/scripts",
77
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
88
"create-release-from-tags": "yarn ts-node-script --files create-release-from-tags/run.ts",
9-
"purge-cdn-cache": "node purge-jsdeliver-cache",
9+
"purge-cdn-cache": "yarn concurrently 'yarn:purge-cdn-cache:*'",
10+
"purge-cdn-cache:signals": "node purge-cdn-cache.js '@segment/analytics-signals' 'packages/signals/signals/dist/umd'",
11+
"purge-cdn-cache:consent": "node purge-cdn-cache.js '@segment/analytics-consent-wrapper-onetrust' 'packages/consent/consent-wrapper-onetrust/dist/umd'",
1012
"test": "yarn jest",
1113
"tsc": "yarn run -T tsc",
1214
"eslint": "yarn run -T eslint",

scripts/purge-jsdeliver-cache.js renamed to scripts/purge-cdn-cache.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const path = require('node:path')
66
* Will abort the API unless the current commit has a tag with the package name on it.
77
*/
88
const purgeJsDelivrCache = async (packageName, relativePath) => {
9+
console.log(`\n\n`)
910
const gitRoot = path.resolve(__dirname, '..')
1011
const fullPath = path.join(gitRoot, relativePath)
1112

@@ -14,22 +15,27 @@ const purgeJsDelivrCache = async (packageName, relativePath) => {
1415
process.exit(1)
1516
}
1617

18+
// Only run this script if the given package has been published
1719
// Check if the current git HEAD has a tag containing the package name
18-
try {
19-
const tags = execSync('git tag --contains HEAD', {
20-
cwd: gitRoot,
21-
}).toString()
20+
// 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.
21+
if (process.env.CI) {
22+
console.log('Searching for matching tag for package...')
23+
try {
24+
const tags = execSync('git tag --contains HEAD', {
25+
cwd: gitRoot,
26+
}).toString()
2227

23-
const hasMatchingTag = tags.includes(packageName)
24-
if (!hasMatchingTag) {
25-
console.log(
26-
`No tags containing the package name "${packageName}" found on the current git HEAD. Aborting cache purge.`
27-
)
28-
process.exit(0)
28+
const hasMatchingTag = tags.includes(packageName)
29+
if (!hasMatchingTag) {
30+
console.log(
31+
`No tags containing the package name "${packageName}" found on the current git HEAD. Aborting cache purge.`
32+
)
33+
process.exit(0)
34+
}
35+
} catch (error) {
36+
console.error(`Failed to check git tags: ${error.message}`)
37+
process.exit(1)
2938
}
30-
} catch (error) {
31-
console.error(`Failed to check git tags: ${error.message}`)
32-
process.exit(1)
3339
}
3440

3541
const files = fs.readdirSync(fullPath)
@@ -39,19 +45,19 @@ const purgeJsDelivrCache = async (packageName, relativePath) => {
3945
)
4046
for (const file of files) {
4147
const filePath = path.join(relativePath, file)
48+
console.log(`Purging cache: ${file}...`)
4249
const url = `https://purge.jsdelivr.net/npm/${packageName}/${filePath}`
4350

4451
try {
4552
const response = await fetch(url)
46-
if (response.ok) {
47-
console.log(`Purged: ${url} - Status: ${response.status}`)
48-
} else {
53+
if (!response.ok) {
4954
console.error(`Failed to purge: ${url} - Status: ${response.status}`)
5055
}
5156
} catch (error) {
5257
console.error(`Failed to purge: ${url} - Error: ${error.message}`)
5358
}
5459
}
60+
console.log(`\nPurge of ${packageName} finished.`)
5561
}
5662

5763
const [packageName, relativePath] = process.argv.slice(2)

0 commit comments

Comments
 (0)