@@ -3,19 +3,28 @@ import path from 'node:path';
33import { dump , load } from 'js-yaml' ;
44import { type DocumentationEntry , type Page , verbose } from './util' ;
55
6- export async function readDocs ( ) : Promise < Record < string , DocumentationEntry > > {
6+ export async function readDocs ( folder : string ) : Promise < Record < string , DocumentationEntry > > {
77 const docs : Record < string , DocumentationEntry > = { } ;
8- const files = await fs . readdir ( path . join ( process . cwd ( ) , 'docs/documentation' ) ) ;
8+ const folderPath = path . join ( process . cwd ( ) , folder ) ;
99
10- console . log ( `📄 Read from docs/documentation/*.yml (${ files . length } )` ) ;
10+ try {
11+ await fs . access ( folderPath ) ;
12+ } catch {
13+ console . log ( `📄 Read from ${ folder } /*.yml (0 - folder does not exist)` ) ;
14+ return docs ;
15+ }
16+
17+ const files = await fs . readdir ( folderPath ) ;
18+
19+ console . log ( `📄 Read from ${ folder } /*.yml (${ files . length } )` ) ;
1120
1221 for ( let i = 0 ; i < files . length ; i ++ ) {
1322 if ( files [ i ] . endsWith ( '.yml' ) ) {
1423 if ( verbose ) {
1524 console . log ( ` ${ files [ i ] } ` ) ;
1625 }
1726
18- const content = await fs . readFile ( path . join ( process . cwd ( ) , 'docs/documentation' , files [ i ] ) , {
27+ const content = await fs . readFile ( path . join ( folderPath , files [ i ] ) , {
1928 encoding : 'utf8' ,
2029 } ) ;
2130
@@ -33,40 +42,61 @@ export async function readDocs(): Promise<Record<string, DocumentationEntry>> {
3342 return docs ;
3443}
3544
36- export async function moveOldDocs ( gidsInUse : Set < string > ) : Promise < void > {
37- const allFiles = await fs . readdir ( path . join ( process . cwd ( ) , 'docs/documentation' ) ) ;
45+ export async function moveOldDocs ( folder : string , gidsInUse : Set < string > ) : Promise < void > {
46+ const folderPath = path . join ( process . cwd ( ) , folder ) ;
47+ const unusedFolder = `${ folder } -unused` ;
48+ const unusedPath = path . join ( process . cwd ( ) , unusedFolder ) ;
49+
50+ try {
51+ await fs . access ( folderPath ) ;
52+ } catch {
53+ return ;
54+ }
55+
56+ const allFiles = await fs . readdir ( folderPath ) ;
3857 const files = allFiles . filter (
3958 ( file ) => file . endsWith ( '.yml' ) && ! gidsInUse . has ( file . replace ( / \. y m l $ / , '' ) )
4059 ) ;
4160
42- await fs . mkdir ( path . join ( process . cwd ( ) , 'docs/documentation-unused' ) , { recursive : true } ) ;
61+ if ( files . length === 0 ) {
62+ return ;
63+ }
64+
65+ await fs . mkdir ( unusedPath , { recursive : true } ) ;
4366
44- console . log ( `🚚 Move to docs/documentation-unused /*.yml (${ files . length } )` ) ;
67+ console . log ( `🚚 Move to ${ unusedFolder } /*.yml (${ files . length } )` ) ;
4568
4669 for ( let i = 0 ; i < files . length ; i ++ ) {
4770 if ( verbose ) {
4871 console . log ( ` ${ files [ i ] } ` ) ;
4972 }
5073
51- await fs . rename (
52- path . join ( process . cwd ( ) , 'docs/documentation' , files [ i ] ) ,
53- path . join ( process . cwd ( ) , 'docs/documentation-unused' , files [ i ] )
54- ) ;
74+ await fs . rename ( path . join ( folderPath , files [ i ] ) , path . join ( unusedPath , files [ i ] ) ) ;
5575 }
5676}
5777
58- export async function writeNewDocs ( gids : Set < string > , pages : Record < string , Page > ) : Promise < void > {
78+ export async function writeNewDocs (
79+ folder : string ,
80+ gids : Set < string > ,
81+ pages : Record < string , Page >
82+ ) : Promise < void > {
83+ const folderPath = path . join ( process . cwd ( ) , folder ) ;
5984 const pageFiles : any [ ] = [ ] ;
6085
61- const files = await fs . readdir ( path . join ( process . cwd ( ) , 'docs/documentation' ) ) ;
62- const existingGids = files . reduce ( ( memo : Record < string , boolean > , file ) => {
63- memo [ file . replace ( / \. y m l $ / , '' ) ] = true ;
64- return memo ;
65- } , { } ) ;
86+ await fs . mkdir ( folderPath , { recursive : true } ) ;
87+
88+ let existingGids : Record < string , boolean > = { } ;
89+ try {
90+ const files = await fs . readdir ( folderPath ) ;
91+ existingGids = files . reduce ( ( memo : Record < string , boolean > , file ) => {
92+ memo [ file . replace ( / \. y m l $ / , '' ) ] = true ;
93+ return memo ;
94+ } , { } ) ;
95+ } catch { }
6696
6797 let newCount = 0 ;
6898
69- console . log ( `📝 Write to docs/documentation /*.yml (${ gids . size } )` ) ;
99+ console . log ( `📝 Write to ${ folder } /*.yml (${ gids . size } )` ) ;
70100
71101 gids . forEach ( ( gid ) => {
72102 if ( ! existingGids [ gid ] ) {
@@ -85,8 +115,6 @@ export async function writeNewDocs(gids: Set<string>, pages: Record<string, Page
85115 } ) ;
86116 } ) ;
87117
88- await fs . mkdir ( path . join ( process . cwd ( ) , 'docs/documentation' ) , { recursive : true } ) ;
89-
90118 for ( let i = 0 ; i < pageFiles . length ; i ++ ) {
91119 const pageFile = pageFiles [ i ] ;
92120 const filename = `${ pageFile . gid } .yml` ;
@@ -95,10 +123,7 @@ export async function writeNewDocs(gids: Set<string>, pages: Record<string, Page
95123 console . log ( ` ${ filename } ` ) ;
96124 }
97125
98- await fs . writeFile (
99- path . join ( process . cwd ( ) , 'docs/documentation' , filename ) ,
100- dump ( pageFile , { noRefs : true } )
101- ) ;
126+ await fs . writeFile ( path . join ( folderPath , filename ) , dump ( pageFile , { noRefs : true } ) ) ;
102127 }
103128
104129 console . log ( ` 🆕 New (${ newCount } )` ) ;
0 commit comments