Skip to content

Commit 5bc629f

Browse files
authoredMar 20, 2024
fix(codegen): use relative path when globbing for files (#6083)
1 parent a08a82f commit 5bc629f

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed
 

‎packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,30 @@ import {describe, expect, test} from '@jest/globals'
66
import {findQueriesInPath} from '../findQueriesInPath'
77

88
describe('findQueriesInPath', () => {
9+
test('Can find queries in path', async () => {
10+
const stream = findQueriesInPath({
11+
path: path.join('**', 'typescript', '__tests__', 'fixtures', 'source1.ts'),
12+
})
13+
const res = []
14+
for await (const result of stream) {
15+
res.push(result)
16+
}
17+
expect(res.length).toBe(1)
18+
expect(res[0].type).toBe('queries')
19+
assert(res[0].type === 'queries') // workaround for TS
20+
expect(res[0].queries.length).toBe(1)
21+
// filename can be either of these two
22+
// depending on whether the test is run from the monorepo root or from the package root
23+
expect(
24+
res[0].filename === 'src/typescript/__tests__/fixtures/source1.ts' ||
25+
res[0].filename === 'packages/@sanity/codegen/src/typescript/__tests__/fixtures/source1.ts',
26+
).toBe(true)
27+
expect(res[0].queries[0].name).toBe('postQuery')
28+
expect(res[0].queries[0].result).toBe('*[_type == "author"]')
29+
})
930
test('should throw an error if the query name already exists', async () => {
1031
const stream = findQueriesInPath({
11-
path: path.join(__dirname, 'fixtures', '{source1,source2}.ts'),
32+
path: path.join('**', 'fixtures', '{source1,source2}.ts'),
1233
})
1334
await stream.next()
1435
const result = await stream.next()

‎packages/@sanity/codegen/src/typescript/findQueriesInPath.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ const defaultBabelOptions = {
1515
extends: join(__dirname, '..', '..', 'babel.config.json'),
1616
}
1717

18-
const queryNames = new Set()
19-
2018
type ResultQueries = {
2119
type: 'queries'
2220
filename: string
@@ -46,11 +44,12 @@ export async function* findQueriesInPath({
4644
babelOptions?: TransformOptions
4745
resolver?: NodeJS.RequireResolve
4846
}): AsyncGenerator<ResultQueries | ResultError> {
47+
const queryNames = new Set()
4948
// Holds all query names found in the source files
5049
debug(`Globing ${path}`)
5150

5251
const stream = glob.stream(path, {
53-
absolute: true,
52+
absolute: false,
5453
ignore: ['**/node_modules/**'], // we never want to look in node_modules
5554
onlyFiles: true,
5655
})

0 commit comments

Comments
 (0)
Please sign in to comment.