Skip to content

Commit 4e2057d

Browse files
committed
fix: resolve stale targets from workspace package context
1 parent 2f29aa2 commit 4e2057d

2 files changed

Lines changed: 145 additions & 2 deletions

File tree

packages/intent/src/cli-support.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { existsSync, readFileSync } from 'node:fs'
2-
import { dirname, join, relative } from 'node:path'
2+
import { dirname, join, relative, resolve } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import { fail } from './cli-error.js'
5+
import { resolveProjectContext } from './core/project-context.js'
56
import type { ScanResult, StalenessReport } from './types.js'
67

78
export function printWarnings(warnings: Array<string>): void {
@@ -47,10 +48,29 @@ export async function resolveStaleTargets(
4748
targetDir?: string,
4849
): Promise<{ reports: Array<StalenessReport> }> {
4950
const resolvedRoot = targetDir
50-
? join(process.cwd(), targetDir)
51+
? resolve(process.cwd(), targetDir)
5152
: process.cwd()
53+
const context = resolveProjectContext({
54+
cwd: process.cwd(),
55+
targetPath: targetDir,
56+
})
5257
const { checkStaleness } = await import('./staleness.js')
5358

59+
const targetsResolvedPackage =
60+
context.packageRoot !== null &&
61+
(context.targetSkillsDir !== null || resolvedRoot !== context.workspaceRoot)
62+
63+
if (targetsResolvedPackage && context.packageRoot) {
64+
return {
65+
reports: [
66+
await checkStaleness(
67+
context.packageRoot,
68+
readPackageName(context.packageRoot),
69+
),
70+
],
71+
}
72+
}
73+
5474
if (existsSync(join(resolvedRoot, 'skills'))) {
5575
return {
5676
reports: [

packages/intent/tests/cli.test.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,43 @@ describe('cli commands', () => {
229229
expect(output).toContain('Template variables applied:')
230230
})
231231

232+
it('copies github workflow templates to the workspace root', async () => {
233+
const root = mkdtempSync(join(realTmpdir, 'intent-cli-setup-gha-mono-'))
234+
tempDirs.push(root)
235+
236+
writeJson(join(root, 'package.json'), {
237+
private: true,
238+
workspaces: ['packages/*'],
239+
})
240+
writeJson(join(root, 'packages', 'router', 'package.json'), {
241+
name: '@tanstack/router',
242+
version: '1.0.0',
243+
intent: { version: 1, repo: 'TanStack/router', docs: 'docs/' },
244+
})
245+
writeSkillMd(join(root, 'packages', 'router', 'skills', 'routing'), {
246+
name: 'routing',
247+
description: 'Routing skill',
248+
})
249+
250+
process.chdir(join(root, 'packages', 'router'))
251+
252+
const exitCode = await main(['setup-github-actions'])
253+
const rootWorkflowsDir = join(root, '.github', 'workflows')
254+
const packageWorkflowsDir = join(
255+
root,
256+
'packages',
257+
'router',
258+
'.github',
259+
'workflows',
260+
)
261+
const output = logSpy.mock.calls.flat().join('\n')
262+
263+
expect(exitCode).toBe(0)
264+
expect(existsSync(rootWorkflowsDir)).toBe(true)
265+
expect(existsSync(packageWorkflowsDir)).toBe(false)
266+
expect(output).toContain('Mode: monorepo')
267+
})
268+
232269
it('lists installed intent packages as json', async () => {
233270
const root = mkdtempSync(join(realTmpdir, 'intent-cli-list-'))
234271
tempDirs.push(root)
@@ -484,6 +521,92 @@ describe('cli commands', () => {
484521

485522
fetchSpy.mockRestore()
486523
})
524+
525+
it('checks only the targeted workspace package for staleness', async () => {
526+
const root = mkdtempSync(join(realTmpdir, 'intent-cli-stale-target-'))
527+
tempDirs.push(root)
528+
529+
writeJson(join(root, 'package.json'), {
530+
private: true,
531+
workspaces: ['packages/*'],
532+
})
533+
writeJson(join(root, 'packages', 'router', 'package.json'), {
534+
name: '@tanstack/router',
535+
})
536+
writeJson(join(root, 'packages', 'query', 'package.json'), {
537+
name: '@tanstack/query',
538+
})
539+
writeSkillMd(join(root, 'packages', 'router', 'skills', 'routing'), {
540+
name: 'routing',
541+
description: 'Routing skill',
542+
library_version: '1.0.0',
543+
})
544+
writeSkillMd(join(root, 'packages', 'query', 'skills', 'cache'), {
545+
name: 'cache',
546+
description: 'Caching skill',
547+
library_version: '1.0.0',
548+
})
549+
550+
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
551+
ok: true,
552+
json: async () => ({ version: '1.0.0' }),
553+
} as Response)
554+
555+
process.chdir(root)
556+
557+
const exitCode = await main(['stale', 'packages/router/skills', '--json'])
558+
const output = logSpy.mock.calls.at(-1)?.[0]
559+
const reports = JSON.parse(String(output)) as Array<{ library: string }>
560+
561+
expect(exitCode).toBe(0)
562+
expect(reports).toHaveLength(1)
563+
expect(reports[0]!.library).toBe('@tanstack/router')
564+
565+
fetchSpy.mockRestore()
566+
})
567+
568+
it('checks the current workspace package for staleness from package cwd', async () => {
569+
const root = mkdtempSync(join(realTmpdir, 'intent-cli-stale-package-cwd-'))
570+
tempDirs.push(root)
571+
572+
writeJson(join(root, 'package.json'), {
573+
private: true,
574+
workspaces: ['packages/*'],
575+
})
576+
writeJson(join(root, 'packages', 'router', 'package.json'), {
577+
name: '@tanstack/router',
578+
})
579+
writeJson(join(root, 'packages', 'query', 'package.json'), {
580+
name: '@tanstack/query',
581+
})
582+
writeSkillMd(join(root, 'packages', 'router', 'skills', 'routing'), {
583+
name: 'routing',
584+
description: 'Routing skill',
585+
library_version: '1.0.0',
586+
})
587+
writeSkillMd(join(root, 'packages', 'query', 'skills', 'cache'), {
588+
name: 'cache',
589+
description: 'Caching skill',
590+
library_version: '1.0.0',
591+
})
592+
593+
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue({
594+
ok: true,
595+
json: async () => ({ version: '1.0.0' }),
596+
} as Response)
597+
598+
process.chdir(join(root, 'packages', 'router'))
599+
600+
const exitCode = await main(['stale', '--json'])
601+
const output = logSpy.mock.calls.at(-1)?.[0]
602+
const reports = JSON.parse(String(output)) as Array<{ library: string }>
603+
604+
expect(exitCode).toBe(0)
605+
expect(reports).toHaveLength(1)
606+
expect(reports[0]!.library).toBe('@tanstack/router')
607+
608+
fetchSpy.mockRestore()
609+
})
487610
})
488611

489612
describe('package metadata', () => {

0 commit comments

Comments
 (0)