|
| 1 | +import { PgpmPackage } from '@pgpmjs/core'; |
| 2 | +import { existsSync, readFileSync } from 'fs'; |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +export interface PackageAliasMap { |
| 6 | + [npmName: string]: string; |
| 7 | +} |
| 8 | + |
| 9 | +/** |
| 10 | + * Build a map of npm package names to control file names (extension names). |
| 11 | + * This allows users to reference packages by their npm name (e.g., @scope/my-module) |
| 12 | + * instead of the control file name (e.g., my-module). |
| 13 | + */ |
| 14 | +export function buildPackageAliasMap(cwd: string): PackageAliasMap { |
| 15 | + const aliasMap: PackageAliasMap = {}; |
| 16 | + |
| 17 | + try { |
| 18 | + const pkg = new PgpmPackage(cwd); |
| 19 | + const workspacePath = pkg.getWorkspacePath(); |
| 20 | + |
| 21 | + if (!workspacePath) { |
| 22 | + return aliasMap; |
| 23 | + } |
| 24 | + |
| 25 | + const modules = pkg.getModuleMap(); |
| 26 | + |
| 27 | + for (const [controlName, moduleInfo] of Object.entries(modules)) { |
| 28 | + const modulePath = join(workspacePath, moduleInfo.path); |
| 29 | + const packageJsonPath = join(modulePath, 'package.json'); |
| 30 | + |
| 31 | + if (existsSync(packageJsonPath)) { |
| 32 | + try { |
| 33 | + const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); |
| 34 | + const npmName = packageJson.name; |
| 35 | + |
| 36 | + if (npmName && npmName !== controlName) { |
| 37 | + aliasMap[npmName] = controlName; |
| 38 | + } |
| 39 | + } catch { |
| 40 | + // Skip modules with invalid package.json |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } catch { |
| 45 | + // Return empty map if we can't access workspace |
| 46 | + } |
| 47 | + |
| 48 | + return aliasMap; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Resolve a package name that might be an npm alias to its control file name. |
| 53 | + * If the input is already a control file name or not found in aliases, returns as-is. |
| 54 | + * |
| 55 | + * @param input - The package name (could be npm name like @scope/pkg or control name) |
| 56 | + * @param cwd - The current working directory |
| 57 | + * @returns The resolved control file name |
| 58 | + */ |
| 59 | +export function resolvePackageAlias(input: string, cwd: string): string { |
| 60 | + if (!input) { |
| 61 | + return input; |
| 62 | + } |
| 63 | + |
| 64 | + const aliasMap = buildPackageAliasMap(cwd); |
| 65 | + return aliasMap[input] ?? input; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Get the npm package name for a given control file name, if available. |
| 70 | + * Returns undefined if no npm alias exists. |
| 71 | + * |
| 72 | + * @param controlName - The control file name (extension name) |
| 73 | + * @param cwd - The current working directory |
| 74 | + * @returns The npm package name or undefined |
| 75 | + */ |
| 76 | +export function getNpmNameForControl(controlName: string, cwd: string): string | undefined { |
| 77 | + const aliasMap = buildPackageAliasMap(cwd); |
| 78 | + |
| 79 | + for (const [npmName, ctrlName] of Object.entries(aliasMap)) { |
| 80 | + if (ctrlName === controlName) { |
| 81 | + return npmName; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return undefined; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Format a module name for display, showing both control name and npm alias if available. |
| 90 | + * Example: "my-module (@scope/my-module)" or just "my-module" if no alias |
| 91 | + * |
| 92 | + * @param controlName - The control file name |
| 93 | + * @param cwd - The current working directory |
| 94 | + * @returns Formatted display string |
| 95 | + */ |
| 96 | +export function formatModuleNameWithAlias(controlName: string, cwd: string): string { |
| 97 | + const npmName = getNpmNameForControl(controlName, cwd); |
| 98 | + |
| 99 | + if (npmName) { |
| 100 | + return `${controlName} (${npmName})`; |
| 101 | + } |
| 102 | + |
| 103 | + return controlName; |
| 104 | +} |
0 commit comments