Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-laws-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/intent': patch
---

- Added Deno monorepo setup coverage so setup-github-actions writes workflows at the workspace root and preserves monorepo-aware path substitutions.
104 changes: 102 additions & 2 deletions packages/intent/src/workspace-patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,76 @@ function hasPackageJson(dir: string): boolean {
return existsSync(join(dir, 'package.json'))
}

function stripJsonCommentsAndTrailingCommas(source: string): string {
let result = ''
let inString = false
let escaped = false

for (let index = 0; index < source.length; index += 1) {
const char = source[index]!
const next = source[index + 1]

if (inString) {
result += char
if (escaped) {
escaped = false
} else if (char === '\\') {
escaped = true
} else if (char === '"') {
inString = false
}
continue
}

if (char === '"') {
inString = true
result += char
continue
}

if (char === '/' && next === '/') {
while (index < source.length && source[index] !== '\n') {
index += 1
}
if (index < source.length) {
result += source[index]!
}
continue
}

if (char === '/' && next === '*') {
index += 2
while (
index < source.length &&
!(source[index] === '*' && source[index + 1] === '/')
) {
index += 1
}
index += 1
continue
}

if (char === ',') {
let lookahead = index + 1
while (lookahead < source.length && /\s/.test(source[lookahead]!)) {
lookahead += 1
}
if (source[lookahead] === '}' || source[lookahead] === ']') {
continue
}
}

result += char
}

return result
}

function readJsonFile(path: string, jsonc = false): unknown {
const source = readFileSync(path, 'utf8')
return JSON.parse(jsonc ? stripJsonCommentsAndTrailingCommas(source) : source)
}

export function readWorkspacePatterns(root: string): Array<string> | null {
const pnpmWs = join(root, 'pnpm-workspace.yaml')
if (existsSync(pnpmWs)) {
Expand All @@ -49,10 +119,16 @@ export function readWorkspacePatterns(root: string): Array<string> | null {
const pkgPath = join(root, 'package.json')
if (existsSync(pkgPath)) {
try {
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'))
const pkg = readJsonFile(pkgPath) as {
workspaces?: unknown | { packages?: unknown }
}
const patterns =
parseWorkspacePatterns(pkg.workspaces) ??
parseWorkspacePatterns(pkg.workspaces?.packages)
parseWorkspacePatterns(
typeof pkg.workspaces === 'object' && pkg.workspaces !== null
? (pkg.workspaces as Record<string, unknown>).packages
: undefined,
)
if (patterns) {
return patterns
}
Expand All @@ -63,6 +139,30 @@ export function readWorkspacePatterns(root: string): Array<string> | null {
}
}

for (const denoConfigName of ['deno.json', 'deno.jsonc']) {
const denoConfigPath = join(root, denoConfigName)
if (!existsSync(denoConfigPath)) {
continue
}

try {
const denoConfig = readJsonFile(
denoConfigPath,
denoConfigName.endsWith('.jsonc'),
) as {
workspace?: unknown
}
const patterns = parseWorkspacePatterns(denoConfig.workspace)
if (patterns) {
return patterns
}
} catch (err: unknown) {
console.error(
`Warning: failed to parse ${denoConfigPath}: ${err instanceof Error ? err.message : err}`,
)
}
}

return null
}

Expand Down
27 changes: 27 additions & 0 deletions packages/intent/tests/project-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,31 @@ describe('resolveProjectContext', () => {
expect(context.isMonorepo).toBe(true)
expect(context.packageRoot).toBe(packageRoot)
})

it('detects Deno workspaces from a workspace package cwd', () => {
const root = createRoot()
writeJson(join(root, 'package.json'), { name: 'repo-root', private: true })
writeFileSync(
join(root, 'deno.jsonc'),
`{
"workspace": [
"packages/*",
],
}
`,
)
const packageRoot = createWorkspacePackage(root, 'router')

const context = resolveProjectContext({ cwd: packageRoot })

expect(context).toEqual({
cwd: packageRoot,
workspaceRoot: root,
packageRoot,
isMonorepo: true,
workspacePatterns: ['packages/*'],
targetPackageJsonPath: join(packageRoot, 'package.json'),
targetSkillsDir: join(packageRoot, 'skills'),
})
})
})
66 changes: 66 additions & 0 deletions packages/intent/tests/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,72 @@ describe('runSetupGithubActions', () => {

rmSync(monoRoot, { recursive: true, force: true })
})

it('writes workflows to the Deno workspace root from a workspace package', () => {
const monoRoot = createMonorepo({
usePackageJsonWorkspaces: true,
packages: [
{ name: 'router', hasSkills: true },
{ name: 'start', hasSkills: true },
],
})

writeFileSync(
join(monoRoot, 'package.json'),
JSON.stringify({ name: 'root', private: true }, null, 2),
)
writeFileSync(
join(monoRoot, 'deno.jsonc'),
`{
// Deno workspace config should be used for monorepo resolution.
"workspace": [
"packages/*",
],
}
`,
)
writeFileSync(
join(monoRoot, 'packages', 'router', 'package.json'),
JSON.stringify(
{
name: '@tanstack/react-router',
intent: { repo: 'TanStack/router', docs: 'docs/' },
},
null,
2,
),
)
mkdirSync(join(monoRoot, 'packages', 'router', 'src'), { recursive: true })
mkdirSync(join(monoRoot, 'packages', 'router', 'docs'), { recursive: true })
mkdirSync(join(monoRoot, 'packages', 'start', 'src'), { recursive: true })

const result = runSetupGithubActions(
join(monoRoot, 'packages', 'router'),
metaDir,
)

expect(result.workflows).toEqual(
expect.arrayContaining([
join(monoRoot, '.github', 'workflows', 'notify-intent.yml'),
join(monoRoot, '.github', 'workflows', 'check-skills.yml'),
]),
)
expect(
existsSync(join(monoRoot, 'packages', 'router', '.github', 'workflows')),
).toBe(false)

const notifyContent = readFileSync(
join(monoRoot, '.github', 'workflows', 'notify-intent.yml'),
'utf8',
)
expect(notifyContent).toContain('package: @tanstack/router')
expect(notifyContent).toContain('repo: TanStack/router')
expect(notifyContent).toContain("- 'packages/router/docs/**'")
expect(notifyContent).toContain("- 'packages/router/src/**'")
expect(notifyContent).toContain("- 'packages/start/src/**'")

rmSync(monoRoot, { recursive: true, force: true })
})
})

// ---------------------------------------------------------------------------
Expand Down
66 changes: 65 additions & 1 deletion packages/intent/tests/workspace-patterns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { afterEach, describe, expect, it } from 'vitest'
import { afterEach, describe, expect, it, vi } from 'vitest'
import {
findPackagesWithSkills,
findWorkspaceRoot,
Expand Down Expand Up @@ -78,6 +78,70 @@ describe('readWorkspacePatterns', () => {
'packages/*',
])
})

it('reads workspace patterns from deno.json', () => {
const root = createRoot()

writeFileSync(
join(root, 'deno.json'),
JSON.stringify({
workspace: ['', './apps/*/', 'packages\\*', 'apps/*'],
}),
)

expect(readWorkspacePatterns(root)).toEqual(['apps/*', 'packages/*'])
})

it('reads workspace patterns from deno.jsonc', () => {
const root = createRoot()

writeFileSync(
join(root, 'deno.jsonc'),
`{
// Deno supports JSONC config files.
"workspace": [
"./packages/*/",
"apps/*",
],
}
`,
)

expect(readWorkspacePatterns(root)).toEqual(['apps/*', 'packages/*'])
})

it('prefers package.json workspaces over Deno workspace config', () => {
const root = createRoot()

writeFileSync(
join(root, 'package.json'),
JSON.stringify({ workspaces: ['packages/*'] }),
)
writeFileSync(
join(root, 'deno.json'),
JSON.stringify({ workspace: ['apps/*'] }),
)

expect(readWorkspacePatterns(root)).toEqual(['packages/*'])
})

it('warns and returns null for invalid Deno config', () => {
const root = createRoot()
const consoleErrorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => undefined)

writeFileSync(join(root, 'deno.jsonc'), '{ invalid jsonc')

expect(readWorkspacePatterns(root)).toBeNull()
expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining(
`Warning: failed to parse ${join(root, 'deno.jsonc')}`,
),
)

consoleErrorSpy.mockRestore()
})
})

describe('resolveWorkspacePackages', () => {
Expand Down
Loading