Skip to content

Commit

Permalink
refactor diagnostics to be a set and apply output formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Jan 23, 2025
1 parent 61cc519 commit 86e04ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
11 changes: 5 additions & 6 deletions packages/cli/src/lib/walker-package-ranger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { isBuiltin } from 'node:module';
const SUPPORTED_EXPORT_CONDITIONS = ['import', 'module-sync', 'default'];
const IMPORT_MAP_RESOLVED_PREFIX = '/~';
const importMap = new Map();
// TODO make diagnostics a Map
const diagnostics = {};
const diagnostics = new Map();

function updateImportMap(key, value, resolvedRoot) {
importMap.set(
Expand All @@ -25,7 +24,7 @@ function resolveBareSpecifier(specifier) {
try {
resolvedPath = import.meta.resolve(specifier);
} catch (e) {
diagnostics[specifier] = `ERROR (${e.code}): unable to resolve specifier => \`${specifier}\` \n${e.message}`;
diagnostics.set(specifier, `ERROR (${e.code}): unable to resolve specifier => \`${specifier}\`\n${e.message}\n`);
}

return resolvedPath;
Expand Down Expand Up @@ -190,7 +189,7 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {

if (!matched) {
// ex. https://unpkg.com/browse/[email protected]/package.json
diagnostics[dependency] = `no supported export conditions (\`${SUPPORTED_EXPORT_CONDITIONS.join(', ')}\`) for dependency => \`${dependency}\``;
diagnostics.set(dependency, `no supported export conditions (\`${SUPPORTED_EXPORT_CONDITIONS.join(', ')}\`) for dependency => \`${dependency}\``);
}
} else {
// handle (unconditional) subpath exports
Expand All @@ -212,7 +211,7 @@ async function walkPackageForExports(dependency, packageJson, resolvedRoot) {
updateImportMap(dependency, 'index.js', resolvedRoot);
} else {
// ex: https://unpkg.com/browse/[email protected]/package.json
diagnostics[dependency] = `WARNING: No supported entry point detected for => \`${dependency}\``;
diagnostics.set(dependency, `WARNING: No supported entry point detected for => \`${dependency}\``);
}
}

Expand Down Expand Up @@ -244,7 +243,7 @@ async function walkPackageJson(packageJson = {}, walkedPackages = new Set()) {
// https://github.com/nodejs/node/issues/56652
// https://nodejs.org/api/modules.html#built-in-modules
if (!isBuiltin(resolved)) {
diagnostics[dependency] = `WARNING: No package.json resolved for => \`${dependency}\`, resolved to \`${resolved}\``;
diagnostics.set(dependency, `WARNING: No package.json resolved for => \`${dependency}\`, resolved to \`${resolved}\``);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/plugins/resource/plugin-node-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ class NodeModulesResource extends ResourceInterface {
console.log('Generating import map from project dependencies...');
const { importMap, diagnostics } = await walkPackageJson(userPackageJson);

if (Object.keys(diagnostics).length > 0) {
if (diagnostics.size > 0) {
console.log('****************************************************************************');

Object.keys(diagnostics).forEach((diagnostic) => {
console.warn(`- ${diagnostics[diagnostic]}\n`);
diagnostics.forEach((value) => {
console.warn(`- ${value}`);
});

console.log('>>> Learn more about these warnings at https://greenwoodjs.dev/docs/introduction/web-standards/#import-maps <<<');
console.log('\n>>> Some issue were detected, learn more about these warnings at https://greenwoodjs.dev/docs/introduction/web-standards/#import-maps');
console.log('****************************************************************************');
}

Expand Down

0 comments on commit 86e04ac

Please sign in to comment.