11import fs from 'node:fs/promises' ;
22import path from 'node:path' ;
33import { Command , Option } from 'clipanion' ;
4- import { isNotNil } from 'es-toolkit' ;
54import micromatch from 'micromatch' ;
5+ import OpenAI from 'openai' ;
66import type { DeclarationReflection } from 'typedoc' ;
7+ import { type ReferenceDoc , renderReferenceDoc } from '../doc' ;
78import { findRootDir } from '../fs' ;
89import { LanguageOption } from '../lang' ;
9- import {
10- findCategory ,
11- genErrors ,
12- genExample ,
13- genParameters ,
14- genReturns ,
15- genSignature ,
16- genSummary ,
17- runTypedoc ,
18- } from '../typedoc' ;
10+ import { translate } from '../translate' ;
11+ import { findCategory , getReferenceDoc , runTypedoc } from '../typedoc' ;
1912
2013export class ReferenceCommand extends Command {
2114 static paths = [ [ 'reference' ] ] ;
2215
2316 readonly patterns = Option . Array ( '-p,--pattern' , [ ] ) ;
2417 readonly lang = LanguageOption ;
2518 readonly withOutput = Option . Boolean ( '--with-output' , true ) ;
19+ readonly translateAiToken = Option . String ( '--translate-ai-token' , {
20+ description :
21+ 'Translate documentation with OpenAI when the token is given. Only works when language options is not "en".' ,
22+ env : 'TRANSLATE_AI_TOKEN' ,
23+ } ) ;
24+ readonly translateAiModel = Option . String ( '--translate-ai-model' , {
25+ description : 'AI model for when translating documentation with OpenAI. Default model is "gpt-4o".' ,
26+ env : 'TRANSLATE_AI_MODEL' ,
27+ } ) ;
2628
2729 async execute ( ) {
2830 const rootDir = await findRootDir ( ) ;
@@ -36,7 +38,7 @@ export class ReferenceCommand extends Command {
3638 const references : Array < {
3739 name : string ;
3840 category : string [ ] ;
39- doc : string ;
41+ doc : ReferenceDoc ;
4042 } > = [ ] ;
4143 this . traverseReflections ( project . children ! , reflection => {
4244 const category = findCategory ( reflection ) ;
@@ -53,9 +55,13 @@ export class ReferenceCommand extends Command {
5355 references . push ( {
5456 name : reflection . name ,
5557 category,
56- doc : this . genReferenceDoc ( reflection ) ,
58+ doc : getReferenceDoc ( reflection ) ,
5759 } ) ;
5860 } ) ;
61+
62+ const ai =
63+ this . translateAiToken != null && this . lang !== 'en' ? new OpenAI ( { apiKey : this . translateAiToken } ) : null ;
64+
5965 for ( let i = 0 ; i < references . length ; i += 1 ) {
6066 const reference = references [ i ] ! ;
6167 const filename =
@@ -64,7 +70,11 @@ export class ReferenceCommand extends Command {
6470 : path . join ( this . lang , 'reference' , ...reference . category , `${ reference . name } .md` ) ;
6571 const filepath = path . join ( rootDir , 'docs' , filename ) ;
6672 await fs . mkdir ( path . dirname ( filepath ) , { recursive : true } ) ;
67- await fs . writeFile ( filepath , reference . doc , 'utf8' ) ;
73+ const doc =
74+ ai != null
75+ ? await translate ( ai , reference . doc , this . lang , { model : this . translateAiModel as OpenAI . ChatModel } )
76+ : reference . doc ;
77+ await fs . writeFile ( filepath , renderReferenceDoc ( doc , this . lang ) , 'utf8' ) ;
6878 console . log ( `[${ i + 1 } /${ references . length } ] ${ filename } generated` ) ;
6979 }
7080 }
@@ -83,23 +93,4 @@ export class ReferenceCommand extends Command {
8393 }
8494 }
8595 }
86-
87- private genReferenceDoc ( reflection : DeclarationReflection ) {
88- const sig = reflection . signatures ?. [ 0 ] ;
89- if ( sig == null ) {
90- throw new Error ( `Signature not found: ${ reflection . name } ` ) ;
91- }
92-
93- const summary = genSummary ( sig ) ;
94- const signature = genSignature ( sig , { lang : this . lang } ) ;
95- const parameters = genParameters ( sig , { lang : this . lang } ) ;
96- const returns = genReturns ( sig , { lang : this . lang } ) ;
97- const errors = genErrors ( sig , { lang : this . lang } ) ;
98- const example = genExample ( sig , { lang : this . lang } ) ;
99-
100- return [ `# ${ reflection . name } ` , summary , signature , parameters , returns , errors , example ]
101- . flat ( )
102- . filter ( isNotNil )
103- . join ( '\n\n' ) ;
104- }
10596}
0 commit comments