|
| 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 | + |
1 | 32 | export default function friendsPlugin(context, options) {
|
2 | 33 | return {
|
3 | 34 | name: 'react-navigation-versions',
|
@@ -26,10 +57,7 @@ export default function friendsPlugin(context, options) {
|
26 | 57 | Object.entries(queries).map(async ([version, { tag, packages }]) => {
|
27 | 58 | const items = await Promise.all(
|
28 | 59 | 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); |
33 | 61 | const peers = Object.fromEntries(
|
34 | 62 | Object.entries(pkg.peerDependencies || {}).map(([name]) => [
|
35 | 63 | name,
|
|
0 commit comments