-
-
Notifications
You must be signed in to change notification settings - Fork 7
fix(intent): make workspace package resolution glob-aware and reusable #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d6344f4
feat: implement workspace patterns management and package resolution
LadyBluenotes 9147184
feat: refactor workspace patterns handling and improve package resolu…
LadyBluenotes 6317916
changeset
LadyBluenotes 29fa591
Update packages/intent/src/workspace-patterns.ts
LadyBluenotes 087f114
feat: enhance workspace patterns handling with exclusion support and …
LadyBluenotes 23232a0
Merge branch 'updates' of https://github.com/TanStack/intent into upd…
LadyBluenotes 5f38aa7
fix: address review findings for workspace patterns extraction
KyleAMathews File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/intent': patch | ||
| --- | ||
|
|
||
| Fix workspace package discovery for nested glob patterns, including support for `*` and `**`. Workspace patterns and resolved package roots are now normalized, deduped, and sorted, and the shared resolver has been extracted for reuse by internal workspace scanning. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import { existsSync, readFileSync, readdirSync } from 'node:fs' | ||
| import type { Dirent } from 'node:fs' | ||
| import { isAbsolute, join } from 'node:path' | ||
| import { parse as parseYaml } from 'yaml' | ||
| import { findSkillFiles } from './utils.js' | ||
|
|
||
| function normalizeWorkspacePattern(pattern: string): string { | ||
| return pattern.replace(/\\/g, '/').replace(/^\.\//, '').replace(/\/+$/, '') | ||
| } | ||
|
|
||
| function normalizeWorkspacePatterns(patterns: Array<string>): Array<string> { | ||
| return [ | ||
| ...new Set(patterns.map(normalizeWorkspacePattern).filter(Boolean)), | ||
| ].sort((a, b) => a.localeCompare(b)) | ||
| } | ||
|
|
||
| function parseWorkspacePatterns(value: unknown): Array<string> | null { | ||
| if (!Array.isArray(value)) { | ||
| return null | ||
| } | ||
|
|
||
| const normalized = normalizeWorkspacePatterns( | ||
| value.filter((pattern): pattern is string => typeof pattern === 'string'), | ||
| ) | ||
| return normalized.length > 0 ? normalized : null | ||
| } | ||
|
|
||
| function hasPackageJson(dir: string): boolean { | ||
| return existsSync(join(dir, 'package.json')) | ||
| } | ||
|
|
||
| /** Reads workspace patterns from pnpm-workspace.yaml (preferred) or package.json workspaces. Returns null if no workspace config is found. */ | ||
| export function readWorkspacePatterns(root: string): Array<string> | null { | ||
| const pnpmWs = join(root, 'pnpm-workspace.yaml') | ||
| if (existsSync(pnpmWs)) { | ||
| try { | ||
| const config = parseYaml(readFileSync(pnpmWs, 'utf8')) as Record< | ||
| string, | ||
| unknown | ||
| > | ||
| const patterns = parseWorkspacePatterns(config.packages) | ||
| if (patterns) { | ||
| return patterns | ||
| } | ||
| } catch (err: unknown) { | ||
| console.error( | ||
| `Warning: failed to parse ${pnpmWs}: ${err instanceof Error ? err.message : err}`, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const pkgPath = join(root, 'package.json') | ||
| if (existsSync(pkgPath)) { | ||
| try { | ||
| const pkg = JSON.parse(readFileSync(pkgPath, 'utf8')) | ||
| const workspaces = parseWorkspacePatterns(pkg.workspaces) | ||
| if (workspaces) { | ||
| return workspaces | ||
| } | ||
|
|
||
| const workspacePackages = parseWorkspacePatterns(pkg.workspaces?.packages) | ||
| if (workspacePackages) { | ||
| return workspacePackages | ||
| } | ||
| } catch (err: unknown) { | ||
| console.error( | ||
| `Warning: failed to parse ${pkgPath}: ${err instanceof Error ? err.message : err}`, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| /** Resolves workspace glob patterns to package directories. Supports `*`, `**`, and `!` exclusion patterns. */ | ||
| export function resolveWorkspacePackages( | ||
| root: string, | ||
| patterns: Array<string>, | ||
| ): Array<string> { | ||
| const includedDirs = new Set<string>() | ||
| const excludedDirs = new Set<string>() | ||
|
|
||
| for (const pattern of normalizeWorkspacePatterns(patterns)) { | ||
| if (pattern.startsWith('!')) { | ||
| resolveWorkspacePatternSegments( | ||
| root, | ||
| pattern.slice(1).split('/'), | ||
| excludedDirs, | ||
| ) | ||
| continue | ||
| } | ||
|
|
||
| resolveWorkspacePatternSegments(root, pattern.split('/'), includedDirs) | ||
| } | ||
|
|
||
| return [...includedDirs] | ||
| .filter((dir) => !excludedDirs.has(dir)) | ||
| .sort((a, b) => a.localeCompare(b)) | ||
| } | ||
|
|
||
| function resolveWorkspacePatternSegments( | ||
| dir: string, | ||
| segments: Array<string>, | ||
| result: Set<string>, | ||
| ): void { | ||
| if (segments.length === 0) { | ||
| if (hasPackageJson(dir)) { | ||
| result.add(dir) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| const segment = segments[0]! | ||
| const remainingSegments = segments.slice(1) | ||
|
|
||
| if (segment === '**') { | ||
| resolveWorkspacePatternSegments(dir, remainingSegments, result) | ||
| for (const childDir of readChildDirectories(dir)) { | ||
| resolveWorkspacePatternSegments(childDir, segments, result) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (segment === '*') { | ||
| for (const childDir of readChildDirectories(dir)) { | ||
| resolveWorkspacePatternSegments(childDir, remainingSegments, result) | ||
| } | ||
| return | ||
| } | ||
|
|
||
| const nextDir = join(dir, segment) | ||
| if (!existsSync(nextDir)) { | ||
| return | ||
| } | ||
|
|
||
| resolveWorkspacePatternSegments(nextDir, remainingSegments, result) | ||
| } | ||
|
|
||
| function readChildDirectories(dir: string): Array<string> { | ||
| let entries: Array<Dirent> | ||
| try { | ||
| entries = readdirSync(dir, { withFileTypes: true }) | ||
| } catch (err: unknown) { | ||
| console.error( | ||
| `Warning: could not read directory ${dir}: ${err instanceof Error ? err.message : err}`, | ||
| ) | ||
| return [] | ||
| } | ||
|
|
||
| return entries | ||
| .filter( | ||
| (entry) => | ||
| entry.isDirectory() && | ||
| entry.name !== 'node_modules' && | ||
| !entry.name.startsWith('.'), | ||
| ) | ||
| .map((entry) => join(dir, entry.name)) | ||
| } | ||
|
|
||
| /** Walks up from `start` to find the nearest directory with a workspace config. */ | ||
| export function findWorkspaceRoot(start: string): string | null { | ||
| if (!isAbsolute(start)) { | ||
| throw new Error(`findWorkspaceRoot requires an absolute path, got: ${start}`) | ||
| } | ||
| let dir = start | ||
|
|
||
| while (true) { | ||
| if (readWorkspacePatterns(dir)) { | ||
| return dir | ||
| } | ||
|
|
||
| const next = join(dir, '..') | ||
| if (next === dir) return null | ||
| dir = next | ||
| } | ||
| } | ||
|
|
||
| /** Finds workspace packages that contain at least one SKILL.md file. */ | ||
| export function findPackagesWithSkills(root: string): Array<string> { | ||
| const patterns = readWorkspacePatterns(root) | ||
| if (!patterns) return [] | ||
|
|
||
| return resolveWorkspacePackages(root, patterns).filter((dir) => { | ||
| const skillsDir = join(dir, 'skills') | ||
| return existsSync(skillsDir) && findSkillFiles(skillsDir).length > 0 | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.