UNPKG

knip

Version:

Find unused files, dependencies and exports in your TypeScript and JavaScript projects

41 lines (40 loc) 1.52 kB
import fg from 'fast-glob'; import { partition } from './array.js'; import { debugLog } from './debug.js'; import { ConfigurationError } from './errors.js'; import { getPackageName } from './package-name.js'; import { join } from './path.js'; import { _require } from './require.js'; export default async function mapWorkspaces(cwd, workspaces) { const [negatedPatterns, patterns] = partition(workspaces, p => p.match(/^!/)); const packages = new Map(); const wsPkgNames = new Set(); if (patterns.length === 0 && negatedPatterns.length === 0) return [packages, wsPkgNames]; const matches = await fg.glob(patterns, { cwd, onlyDirectories: true, ignore: ['**/node_modules/**', ...negatedPatterns], }); for (const name of matches) { const dir = join(cwd, name); const filePath = join(dir, 'package.json'); try { const manifest = _require(filePath); const pkgName = getPackageName(manifest, dir); const pkg = { dir, name, pkgName, manifest }; packages.set(name, pkg); if (pkgName) wsPkgNames.add(pkgName); else throw new ConfigurationError(`Missing package name in ${filePath}`); } catch (error) { if (error?.code === 'MODULE_NOT_FOUND') debugLog('*', `Unable to load package.json for ${name}`); else throw error; } } return [packages, wsPkgNames]; }