Skip to content

Commit 8f5d27b

Browse files
committed
Start adding p5.sound parsing
1 parent d1cae09 commit 8f5d27b

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

src/scripts/parsers/reference.ts

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,49 @@ import type { ParsedLibraryReference } from "../../../types/parsers.interface";
99
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1010
// Local directory to clone the p5.js library
1111
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")
1414

1515
/**
1616
* Main function to clone the p5.js library and save the YUIDoc output to a file
1717
*/
1818
export const parseLibraryReference =
1919
async (): Promise<ParsedLibraryReference | null> => {
20+
// Clone p5.js
2021
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;
2447
};
2548

2649
/**
2750
* Gets the parsed YUIDoc output from the saved file and parses it as JSON
2851
* returns the parsed YUIDoc output
2952
*/
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");
3255
const destinationPath = path.join(__dirname, '../../../public/reference/data.json');
3356
await fs.copyFile(outputFilePath, destinationPath);
3457
};
@@ -37,8 +60,8 @@ const serveYuidocOutput = async (): Promise<void> => {
3760
* Gets the parsed YUIDoc output from the saved file and parses it as JSON
3861
* returns the parsed YUIDoc output
3962
*/
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');
4265
const output = await readFile(outputFilePath);
4366
if (output) {
4467
try {
@@ -53,12 +76,15 @@ const getYuidocOutput = async (): Promise<ParsedLibraryReference | null> => {
5376
/**
5477
* Parses the p5.js library using YUIDoc and captures the output
5578
*/
56-
export const saveYuidocOutput = async () => {
79+
export const saveYuidocOutput = async (inDirName: string, outDirName: string) => {
5780
console.log("Running YUIDoc command and capturing output...");
81+
const outputFilePath = path.join(yuidocOutputPath, outDirName);
5882
try {
5983
await fs.mkdir(yuidocOutputPath, { recursive: true });
84+
const inPath = path.join(__dirname, "in", inDirName);
85+
console.log(inPath)
6086
await new Promise((resolve, reject) => {
61-
exec(`yuidoc -p --outdir ${yuidocOutputPath}`, (error, stdout) => {
87+
exec(`yuidoc -p --outdir ${outputFilePath} .`, { cwd: inPath }, (error, stdout) => {
6288
if (error) {
6389
console.error(`Error running YUIDoc command: ${error}`);
6490
reject(error);
@@ -73,3 +99,42 @@ export const saveYuidocOutput = async () => {
7399
throw err;
74100
}
75101
};
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+
}

src/scripts/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ export const cloneLibraryRepo = async (
3131
localSavePath: string,
3232
repoUrl: string = p5RepoUrl,
3333
branch: string = latestRelease,
34+
{ shouldFixAbsolutePathInPreprocessor = true }: {
35+
shouldFixAbsolutePathInPreprocessor?: boolean
36+
} = {}
3437
) => {
3538
const git = simpleGit();
3639

@@ -57,7 +60,9 @@ export const cloneLibraryRepo = async (
5760
branch
5861
]);
5962
console.log("Repository cloned successfully.");
60-
await fixAbsolutePathInPreprocessor(localSavePath);
63+
if (shouldFixAbsolutePathInPreprocessor) {
64+
await fixAbsolutePathInPreprocessor(localSavePath);
65+
}
6166
} catch (err) {
6267
console.error(`Error cloning repo: ${err}`);
6368
throw err;

0 commit comments

Comments
 (0)