@@ -10,33 +10,40 @@ export function setInitCommand(program: Command): void {
1010 . argument ( '[output]' , 'Optional path used to output the configuration file' )
1111 . option ( '-d, --default' , 'Bypass prompts and select all defaults options' )
1212 . option ( '--js' , 'Forces the output to be a JavaScript file' )
13+ . option ( '--gill' , 'Forces the output to be a gill based JavaScript file' )
1314 . action ( doInit ) ;
1415}
1516
1617type InitOptions = {
1718 default ?: boolean ;
1819 js ?: boolean ;
20+ gill ?: boolean ;
1921} ;
2022
23+ type ConfigFileType = 'gill' | 'js' | 'json' ;
24+
2125async function doInit ( explicitOutput : string | undefined , options : InitOptions ) {
2226 const output = getOutputPath ( explicitOutput , options ) ;
23- const useJsFile = options . js || output . endsWith ( '.js' ) ;
27+ let configFileType : ConfigFileType = output . endsWith ( '.js' ) ? 'js' : 'json' ;
28+ if ( options . gill ) configFileType = 'gill' ;
29+ else if ( options . js ) configFileType = 'js' ;
30+
2431 if ( await canRead ( output ) ) {
2532 throw new Error ( `Configuration file already exists at "${ output } ".` ) ;
2633 }
2734
2835 logBanner ( ) ;
2936 const result = await getPromptResult ( options ) ;
30- const content = getContentFromPromptResult ( result , useJsFile ) ;
37+ const content = getContentFromPromptResult ( result , configFileType ) ;
3138 await writeFile ( output , content ) ;
3239 logSuccess ( `Configuration file created at "${ output } ".` ) ;
3340}
3441
35- function getOutputPath ( explicitOutput : string | undefined , options : Pick < InitOptions , 'js' > ) : string {
42+ function getOutputPath ( explicitOutput : string | undefined , options : Pick < InitOptions , 'gill' | ' js'> ) : string {
3643 if ( explicitOutput ) {
3744 return resolveRelativePath ( explicitOutput ) ;
3845 }
39- return resolveRelativePath ( options . js ? 'codama.js' : 'codama.json' ) ;
46+ return resolveRelativePath ( options . js || options . gill ? 'codama.js' : 'codama.json' ) ;
4047}
4148
4249type PromptResult = {
@@ -114,7 +121,7 @@ function getDefaultPromptResult(): PromptResult {
114121 } ;
115122}
116123
117- function getContentFromPromptResult ( result : PromptResult , useJsFile : boolean ) : string {
124+ function getContentFromPromptResult ( result : PromptResult , configFileType : ConfigFileType ) : string {
118125 const scripts : Record < ScriptName , ScriptConfig > = { } ;
119126 if ( result . scripts . includes ( 'js' ) ) {
120127 scripts . js = {
@@ -130,8 +137,16 @@ function getContentFromPromptResult(result: PromptResult, useJsFile: boolean): s
130137 }
131138 const content : Config = { idl : result . idlPath , before : [ ] , scripts } ;
132139
133- if ( ! useJsFile ) {
140+ if ( configFileType == 'json' ) {
134141 return JSON . stringify ( content , null , 4 ) ;
142+ } else if ( configFileType == 'gill' ) {
143+ return `import { createCodamaConfig } from "gill";\n\n` +
144+ `export default createCodamaConfig({ \n\t` +
145+ `idl: "${ result . idlPath } ", \n\t` +
146+ `clientJs: "${ result . jsPath } ", \n` +
147+ result . scripts . includes ( 'rust' )
148+ ? `clientRust: "${ result . rustPath } ", \n`
149+ : `` + `});` ;
135150 }
136151
137152 return (
0 commit comments