Skip to content

Commit c65db83

Browse files
committed
Cache version info for offline work
1 parent f1d9266 commit c65db83

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/plugins/react-navigation-versions.mjs

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
2+
import { dirname, join } from 'node:path';
3+
4+
const CACHE_DIR = join(
5+
process.cwd(),
6+
'node_modules',
7+
'.cache',
8+
'react-navigation-versions'
9+
);
10+
11+
const query = async (name, tag) => {
12+
const cached = join(CACHE_DIR, `${name}-${tag}.json`);
13+
14+
let pkg;
15+
16+
try {
17+
pkg = await fetch(`https://registry.npmjs.org/${name}/${tag}`).then((res) =>
18+
res.json()
19+
);
20+
21+
await mkdir(dirname(cached), { recursive: true });
22+
await writeFile(cached, JSON.stringify(pkg));
23+
} catch (e) {
24+
const data = await readFile(cached, 'utf-8');
25+
26+
pkg = JSON.parse(data);
27+
}
28+
29+
return pkg;
30+
};
31+
132
export default function friendsPlugin(context, options) {
233
return {
334
name: 'react-navigation-versions',
@@ -26,10 +57,7 @@ export default function friendsPlugin(context, options) {
2657
Object.entries(queries).map(async ([version, { tag, packages }]) => {
2758
const items = await Promise.all(
2859
packages.map(async (name) => {
29-
const pkg = await fetch(
30-
`https://registry.npmjs.org/${name}/${tag}`
31-
).then((res) => res.json());
32-
60+
const pkg = await query(name, tag);
3361
const peers = Object.fromEntries(
3462
Object.entries(pkg.peerDependencies || {}).map(([name]) => [
3563
name,

0 commit comments

Comments
 (0)