Skip to content

Commit

Permalink
fix(codegen): use relative path when globbing for files (#6083)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgulseth authored Mar 20, 2024
1 parent a08a82f commit 5bc629f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@ import {describe, expect, test} from '@jest/globals'
import {findQueriesInPath} from '../findQueriesInPath'

describe('findQueriesInPath', () => {
test('Can find queries in path', async () => {
const stream = findQueriesInPath({
path: path.join('**', 'typescript', '__tests__', 'fixtures', 'source1.ts'),
})
const res = []
for await (const result of stream) {
res.push(result)
}
expect(res.length).toBe(1)
expect(res[0].type).toBe('queries')
assert(res[0].type === 'queries') // workaround for TS
expect(res[0].queries.length).toBe(1)
// filename can be either of these two
// depending on whether the test is run from the monorepo root or from the package root
expect(
res[0].filename === 'src/typescript/__tests__/fixtures/source1.ts' ||
res[0].filename === 'packages/@sanity/codegen/src/typescript/__tests__/fixtures/source1.ts',
).toBe(true)
expect(res[0].queries[0].name).toBe('postQuery')
expect(res[0].queries[0].result).toBe('*[_type == "author"]')
})
test('should throw an error if the query name already exists', async () => {
const stream = findQueriesInPath({
path: path.join(__dirname, 'fixtures', '{source1,source2}.ts'),
path: path.join('**', 'fixtures', '{source1,source2}.ts'),
})
await stream.next()
const result = await stream.next()
Expand Down
5 changes: 2 additions & 3 deletions packages/@sanity/codegen/src/typescript/findQueriesInPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const defaultBabelOptions = {
extends: join(__dirname, '..', '..', 'babel.config.json'),
}

const queryNames = new Set()

type ResultQueries = {
type: 'queries'
filename: string
Expand Down Expand Up @@ -46,11 +44,12 @@ export async function* findQueriesInPath({
babelOptions?: TransformOptions
resolver?: NodeJS.RequireResolve
}): AsyncGenerator<ResultQueries | ResultError> {
const queryNames = new Set()
// Holds all query names found in the source files
debug(`Globing ${path}`)

const stream = glob.stream(path, {
absolute: true,
absolute: false,
ignore: ['**/node_modules/**'], // we never want to look in node_modules
onlyFiles: true,
})
Expand Down

0 comments on commit 5bc629f

Please sign in to comment.