1+ import { mkdtempSync , writeFileSync } from 'node:fs'
2+ import { tmpdir } from 'node:os'
13import { join , resolve } from 'node:path'
24
35import { describe , expect , test } from 'vitest'
@@ -69,6 +71,101 @@ describe('getExtractOptions', () => {
6971 } )
7072 } )
7173
74+ test ( 'should append schema.json when path points to an existing directory' , ( ) => {
75+ const tempDir = mkdtempSync ( join ( tmpdir ( ) , 'schema-test-' ) )
76+ const projectRoot = {
77+ directory : tempDir ,
78+ path : join ( tempDir , 'sanity.config.ts' ) ,
79+ type : 'studio' as const ,
80+ }
81+
82+ const result = getExtractOptions ( {
83+ flags : {
84+ format : 'groq-type-nodes' ,
85+ path : '.' , // current dir - definitely exists
86+ watch : false ,
87+ 'watch-patterns' : undefined ,
88+ workspace : undefined ,
89+ } as ExtractSchemaCommand [ 'flags' ] ,
90+ projectRoot,
91+ schemaExtraction : undefined ,
92+ } )
93+
94+ expect ( result . outputPath ) . toEqual ( join ( tempDir , 'schema.json' ) )
95+ } )
96+
97+ test ( 'should treat path with .json extension as a file path' , ( ) => {
98+ const result = getExtractOptions ( {
99+ flags : {
100+ format : 'groq-type-nodes' ,
101+ path : './schema.json' ,
102+ watch : false ,
103+ 'watch-patterns' : undefined ,
104+ workspace : undefined ,
105+ } as ExtractSchemaCommand [ 'flags' ] ,
106+ projectRoot : mockProjectRoot ,
107+ schemaExtraction : undefined ,
108+ } )
109+
110+ expect ( result . outputPath ) . toEqual ( resolve ( join ( '/test/project' , 'schema.json' ) ) )
111+ } )
112+
113+ test ( 'should treat path with .json extension in subdirectory as a file path' , ( ) => {
114+ const result = getExtractOptions ( {
115+ flags : {
116+ format : 'groq-type-nodes' ,
117+ path : 'output/my-schema.json' ,
118+ watch : false ,
119+ 'watch-patterns' : undefined ,
120+ workspace : undefined ,
121+ } as ExtractSchemaCommand [ 'flags' ] ,
122+ projectRoot : mockProjectRoot ,
123+ schemaExtraction : undefined ,
124+ } )
125+
126+ expect ( result . outputPath ) . toEqual ( resolve ( join ( '/test/project' , 'output' , 'my-schema.json' ) ) )
127+ } )
128+
129+ test ( 'should treat path with non-.json extension as a file path' , ( ) => {
130+ const result = getExtractOptions ( {
131+ flags : {
132+ format : 'groq-type-nodes' ,
133+ path : 'output/my-schema.yaml' ,
134+ watch : false ,
135+ 'watch-patterns' : undefined ,
136+ workspace : undefined ,
137+ } as ExtractSchemaCommand [ 'flags' ] ,
138+ projectRoot : mockProjectRoot ,
139+ schemaExtraction : undefined ,
140+ } )
141+
142+ expect ( result . outputPath ) . toEqual ( resolve ( join ( '/test/project' , 'output' , 'my-schema.yaml' ) ) )
143+ } )
144+
145+ test ( 'should respect .json file path when file exists on disk' , ( ) => {
146+ const tempDir = mkdtempSync ( join ( tmpdir ( ) , 'schema-test-' ) )
147+ writeFileSync ( join ( tempDir , 'schema.json' ) , '{}' )
148+ const projectRoot = {
149+ directory : tempDir ,
150+ path : join ( tempDir , 'sanity.config.ts' ) ,
151+ type : 'studio' as const ,
152+ }
153+
154+ const result = getExtractOptions ( {
155+ flags : {
156+ format : 'groq-type-nodes' ,
157+ path : 'schema.json' ,
158+ watch : false ,
159+ 'watch-patterns' : undefined ,
160+ workspace : undefined ,
161+ } as ExtractSchemaCommand [ 'flags' ] ,
162+ projectRoot,
163+ schemaExtraction : undefined ,
164+ } )
165+
166+ expect ( result . outputPath ) . toEqual ( join ( tempDir , 'schema.json' ) )
167+ } )
168+
72169 test ( 'should use default values when neither flags nor CLI config are provided' , ( ) => {
73170 const result = getExtractOptions ( {
74171 flags : {
@@ -86,7 +183,7 @@ describe('getExtractOptions', () => {
86183 configPath : '/test/project/sanity.config.ts' ,
87184 enforceRequiredFields : false ,
88185 format : 'groq-type-nodes' ,
89- outputPath : join ( '/test/project' , 'schema.json' ) ,
186+ outputPath : resolve ( join ( '/test/project' , 'schema.json' ) ) ,
90187 watchPatterns : [ ] ,
91188 workspace : undefined ,
92189 } )
0 commit comments