Skip to content

Commit 9071028

Browse files
committed
Add postpublish script for purging CDN cache
1 parent 2aedc48 commit 9071028

3 files changed

Lines changed: 55 additions & 11 deletions

File tree

RELEASE.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,3 @@ Set as latest release on GitHub:
2424
- Open https://github.com/rapideditor/country-coder/blob/main/CHANGELOG.md and copy the URL to the new release
2525
- Open https://github.com/rapideditor/country-coder/tags and pick the new tag you just pushed
2626
- There should be a link like "create a release from the tag", click that, and paste in the link to the changelog.
27-
28-
29-
### Purge JSDelivr CDN cache
30-
Include any URLs that anyone might request.
31-
32-
```bash
33-
curl 'https://purge.jsdelivr.net/npm/@rapideditor/country-coder@latest/dist/country-coder.iife.js'
34-
curl 'https://purge.jsdelivr.net/npm/@rapideditor/country-coder@latest/dist/country-coder.iife.min.js'
35-
curl 'https://purge.jsdelivr.net/npm/@rapideditor/country-coder@5/dist/country-coder.iife.js'
36-
curl 'https://purge.jsdelivr.net/npm/@rapideditor/country-coder@5/dist/country-coder.iife.min.js'
37-
```

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"clean": "bun ./scripts/clean.ts",
5353
"format": "bun ./scripts/format-json.js",
5454
"lint": "eslint ./scripts ./src ./test",
55+
"postpublish": "bun ./scripts/postpublish.ts",
5556
"start": "bun ./scripts/server.ts",
5657
"test": "bun test --dots --coverage ./test"
5758
},

scripts/postpublish.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Glob } from 'bun';
2+
import { styleText } from 'node:util';
3+
4+
const CDNRoot = 'https://purge.jsdelivr.net/npm/@rapideditor/country-coder';
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

Comments
 (0)