@@ -9,26 +9,49 @@ import type { ParsedLibraryReference } from "../../../types/parsers.interface";
9
9
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
10
10
// Local directory to clone the p5.js library
11
11
const localPath = path . join ( __dirname , "in" , "p5.js" ) ;
12
- // Local path to save the YUIDoc output
13
- const yuidocOutputPath = path . join ( __dirname , "out" , "data.json" ) ;
12
+ const localSoundPath = path . join ( __dirname , "in" , "p5.sound.js" ) ;
13
+ const yuidocOutputPath = path . join ( __dirname , "out" )
14
14
15
15
/**
16
16
* Main function to clone the p5.js library and save the YUIDoc output to a file
17
17
*/
18
18
export const parseLibraryReference =
19
19
async ( ) : Promise < ParsedLibraryReference | null > => {
20
+ // Clone p5.js
20
21
await cloneLibraryRepo ( localPath ) ;
21
- await saveYuidocOutput ( ) ;
22
- await serveYuidocOutput ( ) ;
23
- return getYuidocOutput ( ) ;
22
+ await saveYuidocOutput ( 'p5.js' , 'data-p5' ) ;
23
+ const p5Data = await getYuidocOutput ( 'data-p5' ) ;
24
+ if ( ! p5Data ) throw new Error ( 'Error generating p5 reference data!' ) ;
25
+
26
+ // Clone p5.sound.js
27
+ await cloneLibraryRepo (
28
+ localSoundPath ,
29
+ 'https://github.com/processing/p5.sound.js.git' ,
30
+ 'main' ,
31
+ { shouldFixAbsolutePathInPreprocessor : false }
32
+ ) ;
33
+ await saveYuidocOutput ( 'p5.sound.js' , 'data-sound' ) ;
34
+ const soundData = await getYuidocOutput ( 'data-sound' ) ;
35
+ if ( ! soundData ) throw new Error ( 'Error generating p5.sound reference data!' ) ;
36
+
37
+ const combined = await combineYuidocData (
38
+ [
39
+ p5Data ,
40
+ soundData ,
41
+ ] ,
42
+ 'data'
43
+ ) ;
44
+
45
+ await serveYuidocOutput ( 'data' ) ;
46
+ return combined ;
24
47
} ;
25
48
26
49
/**
27
50
* Gets the parsed YUIDoc output from the saved file and parses it as JSON
28
51
* returns the parsed YUIDoc output
29
52
*/
30
- const serveYuidocOutput = async ( ) : Promise < void > => {
31
- const outputFilePath = path . join ( yuidocOutputPath , "data.json" ) ;
53
+ const serveYuidocOutput = async ( outDirName : string ) : Promise < void > => {
54
+ const outputFilePath = path . join ( yuidocOutputPath , outDirName , "data.json" ) ;
32
55
const destinationPath = path . join ( __dirname , '../../../public/reference/data.json' ) ;
33
56
await fs . copyFile ( outputFilePath , destinationPath ) ;
34
57
} ;
@@ -37,8 +60,8 @@ const serveYuidocOutput = async (): Promise<void> => {
37
60
* Gets the parsed YUIDoc output from the saved file and parses it as JSON
38
61
* returns the parsed YUIDoc output
39
62
*/
40
- const getYuidocOutput = async ( ) : Promise < ParsedLibraryReference | null > => {
41
- const outputFilePath = path . join ( yuidocOutputPath , " data.json" ) ;
63
+ const getYuidocOutput = async ( outDirName : string ) : Promise < ParsedLibraryReference | null > => {
64
+ const outputFilePath = path . join ( yuidocOutputPath , outDirName , ' data.json' ) ;
42
65
const output = await readFile ( outputFilePath ) ;
43
66
if ( output ) {
44
67
try {
@@ -53,12 +76,15 @@ const getYuidocOutput = async (): Promise<ParsedLibraryReference | null> => {
53
76
/**
54
77
* Parses the p5.js library using YUIDoc and captures the output
55
78
*/
56
- export const saveYuidocOutput = async ( ) => {
79
+ export const saveYuidocOutput = async ( inDirName : string , outDirName : string ) => {
57
80
console . log ( "Running YUIDoc command and capturing output..." ) ;
81
+ const outputFilePath = path . join ( yuidocOutputPath , outDirName ) ;
58
82
try {
59
83
await fs . mkdir ( yuidocOutputPath , { recursive : true } ) ;
84
+ const inPath = path . join ( __dirname , "in" , inDirName ) ;
85
+ console . log ( inPath )
60
86
await new Promise ( ( resolve , reject ) => {
61
- exec ( `yuidoc -p --outdir ${ yuidocOutputPath } ` , ( error , stdout ) => {
87
+ exec ( `yuidoc -p --outdir ${ outputFilePath } .` , { cwd : inPath } , ( error , stdout ) => {
62
88
if ( error ) {
63
89
console . error ( `Error running YUIDoc command: ${ error } ` ) ;
64
90
reject ( error ) ;
@@ -73,3 +99,42 @@ export const saveYuidocOutput = async () => {
73
99
throw err ;
74
100
}
75
101
} ;
102
+
103
+ export async function combineYuidocData (
104
+ inputData : ParsedLibraryReference [ ] ,
105
+ outDirName : string
106
+ ) : Promise < ParsedLibraryReference > {
107
+ const result : ParsedLibraryReference = inputData . reduce (
108
+ ( acc , next ) => {
109
+ return {
110
+ project : acc . project ,
111
+ files : {
112
+ ...acc . files ,
113
+ ...next . files ,
114
+ } ,
115
+ modules : {
116
+ ...acc . modules ,
117
+ ...next . modules ,
118
+ } ,
119
+ classes : {
120
+ ...acc . classes ,
121
+ ...next . classes ,
122
+ } ,
123
+ classitems : [
124
+ ...acc . classitems ,
125
+ ...next . classitems ,
126
+ ] ,
127
+ consts : {
128
+ ...acc . consts ,
129
+ ...next . consts ,
130
+ }
131
+ }
132
+ }
133
+ ) ;
134
+ await fs . mkdir ( path . join ( yuidocOutputPath , outDirName ) , { recursive : true } ) ;
135
+ await fs . writeFile (
136
+ path . join ( yuidocOutputPath , outDirName , 'data.json' ) ,
137
+ JSON . stringify ( result , null , 2 )
138
+ ) ;
139
+ return result ;
140
+ }
0 commit comments