|
| 1 | +const { execSync } = require('node:child_process') |
| 2 | +const fs = require('node:fs') |
| 3 | +const path = require('node:path') |
| 4 | +/** |
| 5 | + * 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. |
| 6 | + * On CI, will abort the purge unless the current commit has a tag with the package name on it. |
| 7 | + */ |
| 8 | +const purgeJsDelivrCache = async (packageName, relativePath) => { |
| 9 | + console.log(`\n\n`) |
| 10 | + const gitRoot = path.resolve(__dirname, '..') |
| 11 | + const fullPath = path.join(gitRoot, relativePath) |
| 12 | + |
| 13 | + if (!fs.existsSync(fullPath)) { |
| 14 | + console.error(`Path does not exist: ${fullPath}`) |
| 15 | + process.exit(1) |
| 16 | + } |
| 17 | + |
| 18 | + // Only run this script if the given package has been published |
| 19 | + // Check if the current git HEAD has a tag containing the package name |
| 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() |
| 27 | + |
| 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) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const files = fs.readdirSync(fullPath) |
| 42 | + |
| 43 | + console.log( |
| 44 | + `Purging files for ${packageName}: ${JSON.stringify(files, null, 2)}` |
| 45 | + ) |
| 46 | + for (const file of files) { |
| 47 | + const filePath = path.join(relativePath, file) |
| 48 | + console.log(`Purging cache: ${file}...`) |
| 49 | + const url = `https://purge.jsdelivr.net/npm/${packageName}/${filePath}` |
| 50 | + |
| 51 | + try { |
| 52 | + const response = await fetch(url) |
| 53 | + if (!response.ok) { |
| 54 | + console.error(`Failed to purge: ${url} - Status: ${response.status}`) |
| 55 | + } |
| 56 | + } catch (error) { |
| 57 | + console.error(`Failed to purge: ${url} - Error: ${error.message}`) |
| 58 | + } |
| 59 | + } |
| 60 | + console.log(`\nPurge of ${packageName} finished.`) |
| 61 | +} |
| 62 | + |
| 63 | +const [packageName, relativePath] = process.argv.slice(2) |
| 64 | + |
| 65 | +if (!packageName || !relativePath) { |
| 66 | + console.error('Usage: node purge-cdn-cache.js <package-name> <relative-path>') |
| 67 | + process.exit(1) |
| 68 | +} |
| 69 | + |
| 70 | +purgeJsDelivrCache(packageName, relativePath) |
0 commit comments