@@ -5,6 +5,7 @@ import path from "path";
5
5
import { generateSchemaDetails } from "./helpers/generateSchemaDetails.js" ;
6
6
import { writeIndexFile } from "./helpers/writeIndexFile.js" ;
7
7
import { writeSchemaFile } from "./helpers/writeSchemaFile.js" ;
8
+ import { ContractSchemaType , SchemaDetails } from "./types.js" ;
8
9
9
10
//Note: contract file name must include term 'Contract' to be parsed
10
11
const getContractFileNames = async (
@@ -15,27 +16,65 @@ const getContractFileNames = async (
15
16
return files . filter ( ( fileName ) => fileName . includes ( "Contract" ) ) ;
16
17
} ;
17
18
18
- const getRecordOfNewestVersions = (
19
- newestVersions : Record < string , number > ,
20
- detailType : string ,
21
- detailVersion : number
22
- ) : Record < string , number > => {
23
- if ( "detailType" in newestVersions ) {
24
- if ( newestVersions [ detailType ] < detailVersion ) {
25
- newestVersions [ detailType ] = detailVersion ;
19
+ const getRecordOfNewestContractVersions = (
20
+ newestContractVersions : Record <
21
+ string ,
22
+ { detailVersion : number ; schema : ContractSchemaType }
23
+ > ,
24
+ schemaDetails : SchemaDetails
25
+ ) : Record < string , { detailVersion : number ; schema : ContractSchemaType } > => {
26
+ const { detailType, detailVersion, schema } = schemaDetails ;
27
+
28
+ if ( "detailType" in newestContractVersions ) {
29
+ if ( newestContractVersions [ detailType ] . detailVersion < detailVersion ) {
30
+ newestContractVersions [ detailType ] = {
31
+ detailVersion,
32
+ schema,
33
+ } ;
26
34
}
27
35
} else {
28
- newestVersions [ detailType ] = detailVersion ;
36
+ newestContractVersions [ detailType ] = {
37
+ detailVersion,
38
+ schema,
39
+ } ;
40
+ }
41
+ return newestContractVersions ;
42
+ } ;
43
+
44
+ const writeNewestContractVersionDocs = async (
45
+ newestContractVersions : Record <
46
+ string ,
47
+ { detailVersion : number ; schema : ContractSchemaType }
48
+ > ,
49
+ pathToDocumentationFolder : string
50
+ ) => {
51
+ for ( const detailType in newestContractVersions ) {
52
+ const detailVersion = newestContractVersions [ detailType ] . detailVersion ;
53
+ const pathToContractDocumentationFolder = path . join (
54
+ `${ pathToDocumentationFolder } /${ detailType } `
55
+ ) ;
56
+ await writeIndexFile (
57
+ pathToContractDocumentationFolder ,
58
+ detailType ,
59
+ detailVersion
60
+ ) ;
61
+
62
+ await writeSchemaFile (
63
+ pathToContractDocumentationFolder ,
64
+ newestContractVersions [ detailType ] . schema
65
+ ) ;
29
66
}
30
- return newestVersions ;
31
67
} ;
32
68
33
69
export const generateDocumentation = async (
34
70
pathToContractsFolder : string ,
35
71
pathToDocumentationFolder : string
36
72
) : Promise < void > => {
37
73
const contractFileNames = await getContractFileNames ( pathToContractsFolder ) ;
38
- let newestVersions : Record < string , number > = { } ;
74
+ let newestContractVersions : Record <
75
+ string ,
76
+ { detailVersion : number ; schema : ContractSchemaType }
77
+ > = { } ;
39
78
40
79
for ( const contractFileName of contractFileNames ) {
41
80
const { detailType, detailVersion, schema } = generateSchemaDetails (
@@ -57,13 +96,16 @@ export const generateDocumentation = async (
57
96
58
97
await writeSchemaFile ( pathToContractDocumentationFolder , schema ) ;
59
98
60
- newestVersions = getRecordOfNewestVersions (
61
- newestVersions ,
62
- detailType ,
63
- detailVersion
99
+ newestContractVersions = getRecordOfNewestContractVersions (
100
+ newestContractVersions ,
101
+ { detailType, detailVersion, schema }
64
102
) ;
65
103
66
- console . log ( `Created docs for ${ contractFileName } ` ) ;
67
- console . log ( newestVersions ) ;
104
+ console . log ( newestContractVersions ) ;
68
105
}
106
+
107
+ writeNewestContractVersionDocs (
108
+ newestContractVersions ,
109
+ pathToDocumentationFolder
110
+ ) ;
69
111
} ;
0 commit comments