Skip to content

Commit b63caf9

Browse files
devvaannshabose
authored andcommitted
feat: use jsdoc to markdown as library instead of cli
1 parent 8fdfec1 commit b63caf9

1 file changed

Lines changed: 21 additions & 77 deletions

File tree

build/api-docs-generator.js

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ const path = require('path');
33
const glob = require('glob');
44
const util = require('util');
55
const crypto = require('crypto');
6-
const exec = util.promisify(require('child_process').exec);
6+
const jsdoc2md = require('jsdoc-to-markdown');
77

88
// Promisify the glob function to enable async/await usage
99
const globPromise = util.promisify(glob);
1010

1111
// Constants
1212
const SRC_DIR = './src';
13-
const BUILD_DIR = './build';
14-
const TEMP_DIR = path.join(BUILD_DIR, 'temp');
1513
const MD_FILES_DIR = path.join('./docs', 'API-Reference');
16-
const TEMP_CHECK_DIR = path.join(BUILD_DIR, 'check_copy');
1714
const BATCH_SIZE = 12;
1815

1916

@@ -26,14 +23,6 @@ async function createDir(dirPath) {
2623
}
2724

2825

29-
/**
30-
* Remove directory
31-
* @param {string} dirPath - The path to the directory to remove
32-
*/
33-
async function removeDir(dirPath) {
34-
await fs.rm(dirPath, { recursive: true, force: true });
35-
}
36-
3726
/**
3827
* Responsible to extract file names with their parent dir
3928
* Ex :- docs/API-Reference/worker/WorkerComm -> worker/WorkerComm
@@ -201,25 +190,6 @@ function normalizeLineEndings(content) {
201190
}
202191

203192

204-
/**
205-
* Compare two files based on their MD5 hash values
206-
* @param {string} file1 - Path to the first file
207-
* @param {string} file2 - Path to the second file
208-
* @returns {Promise<boolean>} - True if files are different, false otherwise
209-
*/
210-
async function areFilesDifferent(file1, file2) {
211-
const [content1, content2] = await Promise.all([
212-
fs.readFile(file1, 'utf-8'),
213-
fs.readFile(file2, 'utf-8')
214-
]);
215-
216-
const hash1 = crypto.createHash('md5').update(content1).digest('hex');
217-
const hash2 = crypto.createHash('md5').update(content2).digest('hex');
218-
219-
return hash1 !== hash2;
220-
}
221-
222-
223193
/**
224194
* Generates markdown documentation for a given JavaScript file
225195
* @param {string} file Path to the JavaScript file
@@ -230,54 +200,40 @@ async function generateMarkdown(file, relativePath) {
230200
const fileName = path.basename(file, '.js');
231201

232202
const modifiedContent = modifyJs(content, fileName);
233-
await fs.writeFile(file, modifiedContent, 'utf-8');
203+
204+
// Generate markdown using jsdoc-to-markdown as a library
205+
const markdownContent = await jsdoc2md.render({ source: modifiedContent });
206+
const newContent = normalizeLineEndings(
207+
modifyMarkdown(markdownContent, path.join(relativePath, fileName))
208+
);
234209

235210
const outputDir = path.join(MD_FILES_DIR, relativePath);
236211
await createDir(outputDir);
237212

238213
const outputFileName = path.join(outputDir, `${fileName}.md`);
239-
const tempOutputFileName = path.join(
240-
TEMP_CHECK_DIR, `${fileName}_temp.md`
241-
);
242-
243-
await createDir(TEMP_CHECK_DIR);
244214

245-
// Generate markdown to a temporary file
246-
await exec(`npx jsdoc-to-markdown ${file} > ${tempOutputFileName}`);
247-
248-
let markdownContent = await fs.readFile(tempOutputFileName, 'utf-8');
249-
const updatedMarkdownContent = modifyMarkdown(
250-
markdownContent, path.join(relativePath, fileName)
251-
);
252-
253-
await fs.writeFile(tempOutputFileName, normalizeLineEndings(updatedMarkdownContent), 'utf-8');
254-
255-
const fileExists = await fs.access(outputFileName).then(() => true).catch(
256-
() => false
257-
);
215+
// Compare with existing file in memory to avoid unnecessary writes
216+
let existingContent = null;
217+
try {
218+
existingContent = await fs.readFile(outputFileName, 'utf-8');
219+
} catch (e) {
220+
// File doesn't exist yet
221+
}
258222

259-
const shouldUpdate = !fileExists || await areFilesDifferent(
260-
outputFileName, tempOutputFileName
261-
);
223+
const newHash = crypto.createHash('md5').update(newContent).digest('hex');
224+
const existingHash = existingContent
225+
? crypto.createHash('md5').update(existingContent).digest('hex')
226+
: null;
262227

263-
if (shouldUpdate) {
264-
await fs.rename(tempOutputFileName, outputFileName);
228+
if (newHash !== existingHash) {
229+
await fs.writeFile(outputFileName, newContent, 'utf-8');
265230
console.log(`Updated ${outputFileName}`);
266231
} else {
267-
await fs.unlink(tempOutputFileName);
268232
console.log(`No changes in ${outputFileName}`);
269233
}
270234
}
271235

272236

273-
/**
274-
* Cleans up temp directories
275-
*/
276-
async function cleanupTempDir() {
277-
await removeDir(TEMP_CHECK_DIR);
278-
}
279-
280-
281237
/**
282238
* Driver function
283239
*/
@@ -288,7 +244,6 @@ async function driver() {
288244
await getExistingMarkdownFiles(jsFiles);
289245

290246
console.log(`Found ${jsFiles.length} files to process`);
291-
await createDir(TEMP_DIR);
292247
await createDir(MD_FILES_DIR);
293248

294249
for (let i = 0; i < jsFiles.length; i += BATCH_SIZE) {
@@ -297,25 +252,14 @@ async function driver() {
297252
const relativePath = path.relative(
298253
SRC_DIR, path.dirname(file)
299254
);
300-
const tempDirPath = path.join(TEMP_DIR, relativePath);
301-
await createDir(tempDirPath);
302-
303-
const fileName = path.basename(file);
304-
const destPath = path.join(tempDirPath, fileName);
305-
await fs.copyFile(file, destPath);
306-
307-
await generateMarkdown(destPath, relativePath);
255+
await generateMarkdown(file, relativePath);
308256
console.log(`Processed ${file}`);
309257
}));
310258
}
311259

312-
await removeDir(TEMP_DIR);
313-
await cleanupTempDir();
314260
console.log("All files processed successfully!");
315261
} catch (error) {
316262
console.error("An error occurred:", error);
317-
await removeDir(TEMP_DIR).catch(() => { });
318-
await cleanupTempDir().catch(() => { });
319263
}
320264
}
321265

0 commit comments

Comments
 (0)