|
| 1 | +import { Glob } from 'bun'; |
| 2 | +import { styleText } from 'node:util'; |
| 3 | + |
| 4 | +const CDNRoot = 'https://purge.jsdelivr.net/npm/@rapideditor/location-conflation'; |
| 5 | +const packageJSON = await Bun.file('./package.json').json(); |
| 6 | + |
| 7 | +// Gather versions that need cache invalidation |
| 8 | +const versions = ['@latest']; |
| 9 | +const match = packageJSON.version.match(/^(\d+)\.(\d+)/); |
| 10 | +if (match[1]) { |
| 11 | + versions.push(`@${match[1]}`); // major |
| 12 | + if (match[2]) { |
| 13 | + versions.push(`@${match[1]}.${match[2]}`); // minor |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +postpublish(); |
| 19 | + |
| 20 | +async function postpublish() { |
| 21 | + const START = '🏗 ' + styleText('yellow', 'Running postpublish…'); |
| 22 | + const END = '👍 ' + styleText('green', 'postpublish done'); |
| 23 | + |
| 24 | + console.log(''); |
| 25 | + console.log(START); |
| 26 | + |
| 27 | + console.log(styleText('blueBright', 'Pausing 10 seconds…')); |
| 28 | + await Bun.sleep(10000); |
| 29 | + |
| 30 | + // Purge the JSDelivr CDN caches. |
| 31 | + // https://www.jsdelivr.com/documentation#id-purge-cache |
| 32 | + console.log(''); |
| 33 | + console.log(styleText('blueBright', 'Purging JSDelivr caches…')); |
| 34 | + |
| 35 | + const promises = []; |
| 36 | + const glob = new Glob('./dist/**/*'); |
| 37 | + for (const filepath of glob.scanSync()) { |
| 38 | + // Keep just the end part of the path without extension, e.g. `dist/json/file.json` |
| 39 | + const path = filepath.replace(/(.*)(\/dist.*)/i, '$2'); |
| 40 | + |
| 41 | + for (const version of versions) { |
| 42 | + const url = `${CDNRoot}${version}${path}`; |
| 43 | + const promise = fetch(url) |
| 44 | + .then(response => response.json()) |
| 45 | + .then(json => console.log(styleText('greenBright', `'${url}' → ${json.status}`))) |
| 46 | + .catch(err => console.log(styleText('redBright', `'${url}' → ${err}`))); |
| 47 | + |
| 48 | + promises.push(promise); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + await Promise.all(promises); |
| 53 | + console.log(END); |
| 54 | +} |
0 commit comments