1- import {
2- copyFileSync ,
3- cpSync ,
4- existsSync ,
5- mkdirSync ,
6- readFileSync ,
7- rmSync ,
8- writeFileSync ,
9- } from 'node:fs'
10- import { dirname } from 'node:path'
1+ import { cp , rm } from 'node:fs/promises'
112import * as typedoc from 'typedoc'
123
4+ const LANGUAGES = [ 'zh-CN' ]
5+
136/**
147 * Run TypeDoc with the specified tsconfig
158 */
16- async function runTypedoc ( tsconfigPath : string ) : Promise < void > {
9+ async function runTypedoc ( tsconfig : string ) : Promise < void > {
1710 // Bootstrap TypeDoc with plugins
1811 const app = await typedoc . Application . bootstrapWithPlugins ( {
19- tsconfig : tsconfigPath ,
12+ tsconfig,
2013 } )
2114
2215 // May be undefined if errors are encountered.
@@ -30,199 +23,6 @@ async function runTypedoc(tsconfigPath: string): Promise<void> {
3023 }
3124}
3225
33- /**
34- * Type definitions for file operations
35- */
36- type FileOperation = {
37- type : 'delete-lines' | 'replace' | 'prepend' | 'append'
38- lineStart ?: number
39- lineEnd ?: number
40- pattern ?: string | RegExp
41- replacement ?: string
42- }
43-
44- type FileMapping =
45- | {
46- type : 'file'
47- source : string
48- destination : string
49- modifications ?: FileOperation [ ]
50- }
51- | {
52- type : 'folder'
53- sourceDir : string
54- destDir : string
55- files : string [ ]
56- modifications ?: FileOperation [ ]
57- }
58-
59- /**
60- * Class to handle file operations based on mapping configurations
61- */
62- class FileMapper {
63- private mappings : FileMapping [ ]
64-
65- constructor ( mappings : FileMapping [ ] ) {
66- this . mappings = mappings
67- }
68-
69- /**
70- * Modify file content with various operations
71- */
72- private modifyFile ( filePath : string , operations : FileOperation [ ] ) : void {
73- let content = readFileSync ( filePath , 'utf-8' )
74-
75- for ( const op of operations ) {
76- if (
77- op . type === 'delete-lines' &&
78- op . lineStart !== undefined &&
79- op . lineEnd !== undefined
80- ) {
81- const lines = content . split ( '\n' )
82- const newLines = [
83- ...lines . slice ( 0 , op . lineStart - 1 ) ,
84- ...lines . slice ( op . lineEnd ) ,
85- ]
86- content = newLines . join ( '\n' )
87- } else if (
88- op . type === 'replace' &&
89- op . pattern &&
90- op . replacement !== undefined
91- ) {
92- content = content . replaceAll (
93- new RegExp ( op . pattern , 'g' ) ,
94- op . replacement ,
95- )
96- } else if ( op . type === 'prepend' && op . replacement !== undefined ) {
97- content = `${ op . replacement } \n${ content } `
98- } else if ( op . type === 'append' && op . replacement !== undefined ) {
99- content = `${ content } \n${ op . replacement } \n`
100- }
101- }
102-
103- writeFileSync ( filePath , content )
104- }
105-
106- /**
107- * Ensure a directory exists, creating it if necessary
108- */
109- private ensureDir ( dirPath : string ) : void {
110- if ( ! existsSync ( dirPath ) ) {
111- mkdirSync ( dirPath , { recursive : true } )
112- }
113- }
114-
115- /**
116- * Remove a file or directory if it exists
117- */
118- private remove (
119- path : string ,
120- options = { recursive : true , force : true } ,
121- ) : void {
122- if ( existsSync ( path ) ) {
123- rmSync ( path , options )
124- }
125- }
126-
127- /**
128- * Move a file from source to destination
129- * Creates destination directory if it doesn't exist
130- */
131- private moveFile ( source : string , destination : string ) : void {
132- this . ensureDir ( dirname ( destination ) )
133- copyFileSync ( source , destination )
134- }
135-
136- /**
137- * Copy a directory recursively
138- */
139- private copyDir ( source : string , destination : string ) : void {
140- this . remove ( destination )
141- this . ensureDir ( destination )
142- cpSync ( source , destination , { recursive : true } )
143- }
144-
145- /**
146- * Apply standard modifications to markdown files
147- */
148- private standardizeMarkdown (
149- filePath : string ,
150- additionalOps : FileOperation [ ] = [ ] ,
151- ) : void {
152- // Common operation: remove first 6 lines
153- const operations : FileOperation [ ] = [
154- { type : 'delete-lines' , lineStart : 1 , lineEnd : 6 } ,
155- {
156- type : 'prepend' ,
157- replacement : '<!-- prettier-ignore-start -->' ,
158- } ,
159- {
160- type : 'append' ,
161- replacement : '<!-- prettier-ignore-end -->' ,
162- } ,
163- ...additionalOps ,
164- ]
165- this . modifyFile ( filePath , operations )
166- }
167-
168- /**
169- * Process a single file mapping
170- */
171- private processFileMapping ( mapping : FileMapping ) : void {
172- if ( mapping . type === 'file' ) {
173- this . moveFile ( mapping . source , mapping . destination )
174- this . standardizeMarkdown ( mapping . destination , mapping . modifications )
175- } else if ( mapping . type === 'folder' ) {
176- this . ensureDir ( mapping . destDir )
177- for ( const file of mapping . files ) {
178- const source = `${ mapping . sourceDir } /${ file } .md`
179- const destination = `${ mapping . destDir } /${ file } .md`
180- this . moveFile ( source , destination )
181- this . standardizeMarkdown ( destination , mapping . modifications )
182- }
183- }
184- }
185-
186- /**
187- * Copy all processed files to locale directories
188- */
189- private copyToLocales ( locales : string [ ] ) : void {
190- for ( const locale of locales ) {
191- const localeRefDir = `./docs/${ locale } /reference`
192- this . ensureDir ( localeRefDir )
193-
194- // Copy all files to each locale
195- for ( const mapping of this . mappings ) {
196- if ( mapping . type === 'file' ) {
197- const destFile = `${ localeRefDir } /${ mapping . destination . split ( '/' ) . pop ( ) } `
198- this . moveFile ( mapping . destination , destFile )
199- } else if ( mapping . type === 'folder' ) {
200- const localeDestDir = `${ localeRefDir } /${ mapping . destDir . split ( '/' ) . pop ( ) } `
201- this . copyDir ( mapping . destDir , localeDestDir )
202- }
203- }
204- }
205- }
206-
207- /**
208- * Process all file mappings
209- */
210- process ( locales : string [ ] = [ ] ) : void {
211- // Process each mapping
212- for ( const mapping of this . mappings ) {
213- this . processFileMapping ( mapping )
214- }
215-
216- // Remove the original api folder
217- this . remove ( './docs/reference/api' )
218-
219- // Copy to locales if specified
220- if ( locales . length > 0 ) {
221- this . copyToLocales ( locales )
222- }
223- }
224- }
225-
22626/**
22727 * Main function to generate API reference documentation
22828 */
@@ -234,43 +34,15 @@ async function generateApiReference() {
23434 console . log ( '✅ Reference generated successfully!' )
23535 console . log ( '📚 Beautifying reference structure...' )
23636
237- // Define file mappings
238- const fileMappings : FileMapping [ ] = [
239- {
240- type : 'file' ,
241- source : './docs/reference/api/interfaces/Options.md' ,
242- destination : './docs/reference/config-options.md' ,
243- modifications : [
244- {
245- type : 'replace' ,
246- pattern : String . raw `\.\.\/type-aliases` ,
247- replacement : './type-aliases' ,
248- } ,
249- {
250- type : 'replace' ,
251- pattern : `Workspace.md` ,
252- replacement : 'workspace.md' ,
253- } ,
254- ] ,
255- } ,
256- {
257- type : 'file' ,
258- source : './docs/reference/api/interfaces/Workspace.md' ,
259- destination : './docs/reference/workspace.md' ,
260- } ,
261- {
262- type : 'folder' ,
263- sourceDir : './docs/reference/api/type-aliases' ,
264- destDir : './docs/reference/type-aliases' ,
265- files : [ 'Sourcemap' , 'Format' , 'ModuleTypes' ] ,
266- } ,
267- ]
37+ await rm ( 'docs/reference/api/index.md' , { force : true } )
38+ await rm ( 'docs/reference/api/_media' , { recursive : true , force : true } )
26839
269- // Create a file mapper and process all mappings
270- const fileMapper = new FileMapper ( fileMappings )
271- fileMapper . process ( [ 'zh-CN' ] )
272-
273- console . log ( '✅ Reference structure beautified successfully!' )
40+ for ( const language of LANGUAGES ) {
41+ await cp ( `docs/reference/api` , `docs/${ language } /reference/api` , {
42+ recursive : true ,
43+ force : true ,
44+ } )
45+ }
27446}
27547
27648// Execute the main function
0 commit comments