-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types(groq-builder): added
ts-simplify-sanity-schema`
- Loading branch information
scottrippey
committed
Oct 5, 2023
1 parent
934e5b1
commit b5a855c
Showing
3 changed files
with
81 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { compileTypes, simplifyTypes } from "../src/simplify-types"; | ||
import { unindent } from "../src/utils/unindent"; | ||
|
||
declare const process: { argv: string[] }; | ||
const [_, __, sanityConfig, outputFile] = process.argv; | ||
const outputText = compileSanityTypes({ sanityConfigFile: sanityConfig }); | ||
console.log(outputText); | ||
|
||
function compileSanityTypes(config: { sanityConfigFile: string }) { | ||
return compileTypes({ | ||
sourceFile: config.sanityConfigFile, | ||
transform({}) { | ||
return unindent(` | ||
import sanityConfig from './${config.sanityConfigFile}'; | ||
import { InferSchemaValues } from '@sanity-typed/types'; | ||
type SimplifyDeep<T> = T extends object | ||
? T extends infer O | ||
? { [K in keyof O]: SimplifyDeep<O[K]> } | ||
: never | ||
: T; | ||
export type SanitySchema = SimplifyDeep<InferSchemaValues<typeof sanityConfig>>; | ||
`); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function unindent(str: string) { | ||
const lines = str.split("\n"); | ||
const indent = lines[1].match(/^\s*/)![0]; | ||
return lines.map((line) => line.replace(indent, "")).join("\n"); | ||
} |