import fs from 'fs' ; import path from 'path' ; import { SanityCodegenConfig } from './types' ; import generateTypes from './generate-types' ; import register , { revert } from '@babel/register' ; const jsdomDefaultHtml = `<!doctype html>
<html>
  <head><meta charset="utf-8"></head>
  <body></body>
</html>` ; export const defaultBabelOptions = { extensions : [ '.js' , '.ts' , '.tsx' , '.mjs' ] , babelrc : false , configFile : false , compact : true , presets : [ [ '@babel/preset-env' , { targets : { node : true } } ] , '@babel/preset-typescript' , '@babel/preset-react' , ] , plugins : [ [ 'module-resolver' , { root : [ '.' ] , alias : { 'part:@sanity/base/schema-creator' : 'sanity-codegen/schema-creator-shim' , 'all:part:@sanity/base/schema-type' : 'sanity-codegen/schema-type-shim' , 'part:@sanity/base/schema-type' : 'sanity-codegen/schema-type-shim' , '^part:.*' : 'sanity-codegen/no-op' , '^config:.*' : 'sanity-codegen/no-op' , '^all:part:.*' : 'sanity-codegen/no-op' , } , } , ] , 'css-modules-transform' , '@babel/plugin-proposal-class-properties' , '@babel/plugin-proposal-numeric-separator' , '@babel/plugin-proposal-optional-chaining' , '@babel/plugin-proposal-nullish-coalescing-operator' , ] , } ; register ( defaultBabelOptions ) ; async function cli ( ) { const resolveConfig = require ( './resolve-config' ) . default ; const configPath = await resolveConfig ( ) ; if ( ! configPath ) { throw new Error ( 'Could not find a sanity-codegen.config.ts or sanity-codegen.config.js file.' ) ; } const config = require ( configPath ) . default || require ( configPath ) ; revert ( ) ; register ( { ... defaultBabelOptions , ... config . babelOptions , } ) ; require ( 'jsdom-global' ) ( jsdomDefaultHtml , { url : 'http://localhost:3333/' , } ) ; if ( ! global . requestAnimationFrame ) { global . requestAnimationFrame = ( cb ) => setTimeout ( cb , 0 ) ; } if ( ! global . cancelAnimationFrame ) { global . cancelAnimationFrame = ( timer ) => clearTimeout ( timer ) ; } if ( ! global . InputEvent ) { global . InputEvent = window . InputEvent ; } if ( ! config . schemaPath ) { throw new Error ( `Sanity Codegen config found at "${ configPath }" was missing the required option "schemaPath"` ) ; } if ( ! config . outputPath ) { throw new Error ( `Sanity Codegen config found at "${ configPath }" was missing the required option "outputPath"` ) ; } const types = require ( path . resolve ( config . schemaPath ) ) . default || require ( path . resolve ( config . schemaPath ) ) ; const result = await generateTypes ( { types , ... config } ) ; const outputPath = path . resolve ( config . outputPath || './schema.ts' ) ; await fs . promises . writeFile ( outputPath , result ) ; console . info ( `[SanityCodeGen]: types written out to ${ outputPath })` ) ; } cli ( ) . catch ( ( e ) => { console . error ( e ) ; process . exit ( 1 ) ; } ) ;