Skip to content

Commit 6bd4357

Browse files
fix(intent): read local package.json version before npm registry fallback (#104)
1 parent a48b8b8 commit 6bd4357

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/intent': patch
3+
---
4+
5+
Read local package.json version before falling back to npm registry in `intent stale`. This fixes version drift detection for packages not published to public registry.npmjs.org (e.g. GitHub Packages, Artifactory, private registries).

packages/intent/src/staleness.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@ function classifyVersionDrift(
3434
}
3535

3636
// ---------------------------------------------------------------------------
37-
// npm version fetching
37+
// Version resolution
3838
// ---------------------------------------------------------------------------
3939

40+
function readLocalVersion(packageDir: string): string | null {
41+
try {
42+
const pkgJson = JSON.parse(
43+
readFileSync(join(packageDir, 'package.json'), 'utf8'),
44+
) as Record<string, unknown>
45+
return typeof pkgJson.version === 'string' ? pkgJson.version : null
46+
} catch {
47+
return null
48+
}
49+
}
50+
4051
async function fetchNpmVersion(packageName: string): Promise<string | null> {
4152
try {
4253
const res = await fetch(
@@ -50,6 +61,13 @@ async function fetchNpmVersion(packageName: string): Promise<string | null> {
5061
}
5162
}
5263

64+
async function fetchCurrentVersion(
65+
packageDir: string,
66+
packageName: string,
67+
): Promise<string | null> {
68+
return readLocalVersion(packageDir) ?? (await fetchNpmVersion(packageName))
69+
}
70+
5371
// ---------------------------------------------------------------------------
5472
// Sync state
5573
// ---------------------------------------------------------------------------
@@ -141,8 +159,8 @@ export async function checkStaleness(
141159
const skillVersion =
142160
skillMetas.find((s) => s.libraryVersion)?.libraryVersion ?? null
143161

144-
// Fetch current npm version
145-
const currentVersion = await fetchNpmVersion(library)
162+
// Resolve current version: prefer local package.json, fall back to npm registry
163+
const currentVersion = await fetchCurrentVersion(packageDir, library)
146164

147165
// Classify drift
148166
const versionDrift =

packages/intent/tests/staleness.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,47 @@ describe('checkStaleness', () => {
309309
expect(report.skills).toHaveLength(1)
310310
expect(requireFirstSkill(report).needsReview).toBe(false)
311311
})
312+
313+
it('reads version from local package.json when npm fetch fails', async () => {
314+
writeFileSync(
315+
join(tmpDir, 'package.json'),
316+
JSON.stringify({ name: '@private/lib', version: '2.5.0' }),
317+
)
318+
319+
writeSkill(tmpDir, 'core', {
320+
name: 'core',
321+
description: 'Core',
322+
library_version: '2.0.0',
323+
})
324+
325+
mockFetchNotOk()
326+
327+
const report = await checkStaleness(tmpDir, '@private/lib')
328+
expect(report.currentVersion).toBe('2.5.0')
329+
expect(report.versionDrift).toBe('minor')
330+
const skill = requireFirstSkill(report)
331+
expect(skill.needsReview).toBe(true)
332+
expect(skill.reasons[0]).toContain('version drift')
333+
})
334+
335+
it('prefers local package.json over npm registry', async () => {
336+
writeFileSync(
337+
join(tmpDir, 'package.json'),
338+
JSON.stringify({ name: '@example/lib', version: '3.0.0' }),
339+
)
340+
341+
writeSkill(tmpDir, 'core', {
342+
name: 'core',
343+
description: 'Core',
344+
library_version: '2.0.0',
345+
})
346+
347+
// npm returns an older published version
348+
mockFetchVersion('2.5.0')
349+
350+
const report = await checkStaleness(tmpDir, '@example/lib')
351+
// Local package.json should take precedence
352+
expect(report.currentVersion).toBe('3.0.0')
353+
expect(report.versionDrift).toBe('major')
354+
})
312355
})

0 commit comments

Comments
 (0)