forked from unpkg/unpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpurge-cache.js
More file actions
72 lines (57 loc) · 1.6 KB
/
purge-cache.js
File metadata and controls
72 lines (57 loc) · 1.6 KB
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
71
72
const chalk = require('chalk');
const { getZone, purgeFiles } = require('./utils/cloudflare.js');
const { getFiles } = require('./utils/unpkg.js');
function groupBy(array, n) {
const groups = [];
while (array.length) groups.push(array.splice(0, n));
return groups;
}
async function purgeCache(packageName, version) {
if (packageName == null) {
console.error(
chalk.red(
'Missing <package-name>; use `node purge-cache.js <package-name> <version>`'
)
);
return 1;
}
if (version == null) {
console.error(
chalk.red(
`Missing <version>; use 'node purge-cache.js "${packageName}" <version>'`
)
);
return 1;
}
const files = await getFiles(packageName, version);
console.log(
chalk.yellow(
`Found ${files.length} files for package ${packageName}@${version}`
)
);
let urls = files.map(
file => `https://unpkg.com/${packageName}@${version}${file.path}`
);
if (version === 'latest') {
// Purge the URL w/out the "@latest" too.
urls = urls.concat(
files.map(file => `https://unpkg.com/${packageName}${file.path}`)
);
}
return getZone('unpkg.com').then(zone => {
let promise = Promise.resolve();
groupBy(urls, 30).forEach(group => {
promise = promise.then(() =>
purgeFiles(zone.id, group).then(() => {
group.forEach(url => console.log(chalk.green(`Purged ${url}`)));
})
);
});
return promise;
});
}
const packageName = process.argv[2];
const version = process.argv[3];
purgeCache(packageName, version).then(exitCode => {
process.exit(exitCode);
});