Skip to content

Commit

Permalink
cache / memoize walked packages to account for concurrency in greenwood
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Jan 13, 2025
1 parent ea10aa9 commit ad7ac8e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions packages/cli/src/lib/walker-package-ranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fs from 'fs';
const SUPPORTED_EXPORT_CONDITIONS = ['import', 'module-sync', 'default'];
const IMPORT_MAP_RESOLVED_PREFIX = '/~';
const importMap = new Map();
const walkedPackages = new Set();
const diagnostics = {};

function updateImportMap(key, value, resolvedRoot) {
Expand Down Expand Up @@ -71,7 +70,7 @@ function derivePackageRoot(resolved) {
return root;
}

// Helper function to convert export patterns to a regex (thanks ChatGPT :D)
// helper function to convert export patterns to a regex (thanks ChatGPT :D)
function globToRegex(pattern) {
// Escape special regex characters
pattern = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&');
Expand All @@ -86,7 +85,7 @@ function globToRegex(pattern) {
return new RegExp('^' + pattern + '$');
}

// convert path to its lowest common root
// helper function to convert path to its lowest common root
// e.g. ./img/path/*/index.js -> /img/path
// https://unpkg.com/browse/@uswds/[email protected]/package.json
function patternRoot(pattern) {
Expand Down Expand Up @@ -212,8 +211,9 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
}
}

// https://nodejs.org/api/packages.html#package-entry-points
async function walkPackageJson(packageJson = {}) {
// we recursively cache / memoize walkedPackages to account for scenarios where Greenwood can (pre)render concurrently
async function walkPackageJson(packageJson = {}, walkedPackages = new Set()) {

try {
const dependencies = Object.keys(packageJson.dependencies || {});

Expand All @@ -232,7 +232,7 @@ async function walkPackageJson(packageJson = {}) {
if (!walkedPackages.has(name)) {
walkedPackages.add(name);

await walkPackageJson(resolvedPackageJson);
await walkPackageJson(resolvedPackageJson, walkedPackages);
}
}
}
Expand Down

0 comments on commit ad7ac8e

Please sign in to comment.