-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathpurge-cdn-cache.js
70 lines (61 loc) · 2.42 KB
/
purge-cdn-cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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.
* 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`)
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)
}
// Only run this script if the given package has been published
// Check if the current git HEAD has a tag containing the package name
// 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)
}
} catch (error) {
console.error(`Failed to check git tags: ${error.message}`)
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)
console.log(`Purging cache: ${file}...`)
const url = `https://purge.jsdelivr.net/npm/${packageName}/${filePath}`
try {
const response = await fetch(url)
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)
if (!packageName || !relativePath) {
console.error('Usage: node purge-cdn-cache.js <package-name> <relative-path>')
process.exit(1)
}
purgeJsDelivrCache(packageName, relativePath)