Skip to content

Commit 7f0ccfd

Browse files
committed
refactor: mdx doc generation scripts
1 parent e74ef8c commit 7f0ccfd

File tree

4 files changed

+60
-143
lines changed

4 files changed

+60
-143
lines changed

build/api-docs-generator.js

Lines changed: 59 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ const path = require('path');
33
const { execSync } = require('child_process');
44

55
// Define the input and output directories
6-
const inputDir = path.join(__dirname, '../src');
6+
const BUILD_DIR = __dirname;
7+
const ROOT_DIR = path.join(BUILD_DIR, '../');
8+
const SRC_DIR = path.join(ROOT_DIR, 'src');
79

810
// DONOT MODIFY THIS
9-
const outputDir = path.join(__dirname, './dev-temp'); // this directory will be automatically removed (required for automative processes, the final output will be in API directory).
11+
// this directory will be automatically removed (required for automative processes, the final output will
12+
// be in API directory).
13+
const outputDir = path.join(BUILD_DIR, './dev-temp');
1014

1115
// Set up paths for the build process
12-
const buildDir = __dirname;
13-
const jsdocFile = path.join(buildDir, 'jsdoc.json');
14-
const configFile = path.join(buildDir, 'config.json');
15-
const sourceDir = path.join(buildDir, 'dev-temp');
16-
const devApiDir = path.join(buildDir, 'dev-api');
16+
const GENERATED_DOCS_FOLDER = path.join(SRC_DIR, '../', 'docs', 'Generated API Reference');
17+
const JSDOC_FILE = path.join(BUILD_DIR, 'jsdoc.json');
18+
const CONFIG_FILE = path.join(BUILD_DIR, 'config.json');
19+
const MDX_API_DIR = path.join(BUILD_DIR, 'api');
20+
const sourceDir = path.join(BUILD_DIR, 'dev-temp');
21+
const devApiDir = path.join(BUILD_DIR, 'dev-api');
1722
const tempDir = path.join(devApiDir, 'temp');
18-
const mdxApiDir = path.join(__dirname, 'api');
1923

2024

2125
/**
@@ -164,7 +168,7 @@ function removeEmptyDirectories(dir) {
164168
});
165169
}
166170

167-
processFilesRecursively(inputDir, inputDir);
171+
processFilesRecursively(SRC_DIR, SRC_DIR);
168172
console.log("All script files for the API documentation have been generated!");
169173

170174
removeEmptyDirectories(outputDir);
@@ -177,6 +181,29 @@ console.log("Successfully removed redundant JS code");
177181

178182
// Conversion of MDX from JS files starts here
179183

184+
const JSDOC_JSON_TEMPLATE = {
185+
"source": {
186+
"include": [
187+
"dev-api\\temp"
188+
]
189+
},
190+
"plugins": [
191+
"plugins/markdown"
192+
],
193+
"opts": {
194+
"destination": "../api",
195+
"recurse": true
196+
}
197+
};
198+
199+
const CONFIG_JSON_TEMPLATE = {
200+
"locales": [
201+
"en"
202+
],
203+
"outDir": "",
204+
"jsdoc": "./jsdoc.json",
205+
"bulma": false
206+
};
180207

181208
/**
182209
* Generates MDX files from JS files using jsdoc-to-mdx.
@@ -187,19 +214,19 @@ function generateMdxFiles() {
187214
// Create necessary directories (all these directories will be removed automatically
188215
// after the task is completed. Finally after the whole execution we'll be left with
189216
// just the API directory i.e. mdxApiDir)
190-
[devApiDir, tempDir, mdxApiDir].forEach(dir => {
217+
[devApiDir, tempDir, MDX_API_DIR].forEach(dir => {
191218
if (!fs.existsSync(dir)) {
192219
fs.mkdirSync(dir, { recursive: true });
193220
}
194221
});
195222

196223
// Read JSDoc and config files
197-
let jsdocConfig = readJsdocFile();
198-
let config = readConfigFile();
224+
let jsdocConfig = JSDOC_JSON_TEMPLATE;
225+
let config = CONFIG_JSON_TEMPLATE;
199226

200227
// Modify JSDoc config to use temp directory
201-
jsdocConfig.source.include = [path.relative(buildDir, tempDir)];
202-
fs.writeFileSync(jsdocFile, JSON.stringify(jsdocConfig, null, 2));
228+
jsdocConfig.source.include = [path.relative(BUILD_DIR, tempDir)];
229+
fs.writeFileSync(JSDOC_FILE, JSON.stringify(jsdocConfig, null, 2));
203230

204231
// Copy JS files to dev-api directory
205232
copyJsFiles(sourceDir, devApiDir);
@@ -212,10 +239,10 @@ function generateMdxFiles() {
212239

213240
// After processing all files, set outDir to empty string
214241
config.outDir = "";
215-
fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
242+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
216243

217244
// Post-processing of MDX files
218-
processMdxFiles(mdxApiDir);
245+
processMdxFiles(MDX_API_DIR);
219246

220247
// Clean up temporary files and directories
221248
cleanupTempFiles();
@@ -226,30 +253,6 @@ function generateMdxFiles() {
226253
}
227254
}
228255

229-
/**
230-
* Reads JSDoc configuration file.
231-
* @returns {Object} JSDoc configuration object
232-
*/
233-
function readJsdocFile() {
234-
if (fs.existsSync(jsdocFile)) {
235-
return JSON.parse(fs.readFileSync(jsdocFile, 'utf-8'));
236-
}
237-
console.error("Jsdoc.json not found! Make sure it is inside the same directory as of the script!");
238-
239-
}
240-
241-
/**
242-
* Reads the configuration file.
243-
* @returns {Object} Configuration object
244-
*/
245-
function readConfigFile() {
246-
if (fs.existsSync(configFile)) {
247-
return JSON.parse(fs.readFileSync(configFile, 'utf-8'));
248-
}
249-
console.error("Config.json not found! Make sure it is inside the same directory as of the script!");
250-
251-
}
252-
253256
/**
254257
* Copies JS files from source to destination directory.
255258
*
@@ -312,17 +315,17 @@ function processJsFile(file, config) {
312315
fs.copyFileSync(file, path.join(tempDir, path.basename(file)));
313316

314317
// Set unique outDir for this file
315-
const outDir = path.join(mdxApiDir, relativeDir, fileName);
316-
config.outDir = path.relative(__dirname, outDir);
317-
fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
318+
const outDir = path.join(MDX_API_DIR, relativeDir, fileName);
319+
config.outDir = path.relative(BUILD_DIR, outDir);
320+
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
318321

319322
// Run jsdoc-to-mdx
320-
execSync(`npx jsdoc-to-mdx -c ${path.relative(__dirname, configFile)}`, { cwd: __dirname });
323+
execSync(`npx jsdoc-to-mdx -c ${path.relative(BUILD_DIR, CONFIG_FILE)}`, { cwd: BUILD_DIR });
321324

322325
console.log(`${file} is successfully converted to MDX`);
323326

324327
// Merge generated MDX files
325-
mergeMdxFiles(outDir, `${fileName}.mdx`, path.join(mdxApiDir, relativeDir));
328+
mergeMdxFiles(outDir, `${fileName}.mdx`, path.join(MDX_API_DIR, relativeDir));
326329

327330
// Clean up temp file
328331
fs.unlinkSync(path.join(tempDir, path.basename(file)));
@@ -498,20 +501,9 @@ function removeBackticksAndCleanup(content) {
498501
* This leaves us with just the API directory
499502
*/
500503
function cleanupTempFiles() {
501-
// Remove temp directory
502-
if (fs.existsSync(tempDir)) {
503-
fs.rmSync(tempDir, { recursive: true, force: true });
504-
}
505-
506-
// Remove dev-api directory
507-
if (fs.existsSync(devApiDir)) {
508-
fs.rmSync(devApiDir, { recursive: true, force: true });
509-
}
510-
511-
// Remove source directory
512-
if (fs.existsSync(sourceDir)) {
513-
fs.rmSync(sourceDir, { recursive: true, force: true });
514-
}
504+
fs.rmSync(tempDir, { recursive: true, force: true });
505+
fs.rmSync(devApiDir, { recursive: true, force: true });
506+
fs.rmSync(sourceDir, { recursive: true, force: true });
515507
}
516508

517509

@@ -560,65 +552,11 @@ function moveToApiReference(sourceDir, destDir) {
560552
}
561553
}
562554

563-
const apiReferenceDir = path.join(__dirname, 'api', 'API reference');
564-
moveToApiReference(path.join(__dirname, 'api'), apiReferenceDir);
565-
console.log("All MDX files have been moved to the API reference folder");
566-
567-
568-
/**
569-
* Delete the docs/generatedApiDocs directory and all its content, to add the new generated content there
570-
*/
571-
function removeGeneratedApiDocs() {
572-
const generatedApiDocsDir = './docs/generatedApiDocs';
573-
try {
574-
fs.rmSync(generatedApiDocsDir, { recursive: true, force: true });
575-
} catch (err) {
576-
console.error(`Error while deleting the generatedApiDocs directory: ${err}`);
577-
}
578-
}
579-
580-
removeGeneratedApiDocs();
581-
582-
583-
/**
584-
* Move the API reference directory from the 'build' directory to the 'docs' directory
585-
* @param {string} srcDir Takes the input dir address
586-
* @param {string} destDir Takes the output dir address
587-
*/
588-
function moveApiReferenceDir(srcDir, destDir) {
589-
590-
// check if API Reference dir already exists in destination. If yes, remove it
591-
if(fs.existsSync(path.join(destDir, 'API Reference'))) {
592-
fs.rmSync(path.join(destDir, 'API Reference'), { recursive: true, force: true });
593-
}
594-
595-
// add full paths
596-
srcDir = path.join(srcDir, 'API Reference');
597-
destDir = path.join(destDir, 'API Reference');
598-
599-
// Move the directory
600-
try {
601-
fs.renameSync(srcDir, destDir);
602-
} catch (err) {
603-
console.error(`Error while moving the directory from build to docs: ${err}`);
604-
}
605-
}
606-
607-
moveApiReferenceDir(mdxApiDir, './docs');
608-
609-
/**
610-
* Delete the API directory i.e. inside the 'build' directory as all its contents are now moved to docs directory
611-
* @param {string} dirToRemove Path of the api dir
612-
*/
613-
function removeApiDir(dirToRemove) {
614-
try {
615-
fs.rmSync(dirToRemove, { recursive: true, force: true });
616-
} catch (err) {
617-
console.error(`Error while deleting the Api directory present inside build directory: ${err}`);
618-
}
619-
}
620-
621-
removeApiDir(mdxApiDir);
622-
623-
624-
console.log("All set! Just copy the docs directory to the docs site.");
555+
console.log("deleting ", GENERATED_DOCS_FOLDER);
556+
fs.rmSync(GENERATED_DOCS_FOLDER, { recursive: true, force: true });
557+
console.log("moving ", MDX_API_DIR, "to", GENERATED_DOCS_FOLDER);
558+
moveToApiReference(MDX_API_DIR, GENERATED_DOCS_FOLDER);
559+
console.log("All set! Updated docs directory.");
560+
fs.rmSync(MDX_API_DIR, { recursive: true, force: true });
561+
fs.rmSync(CONFIG_FILE, { recursive: true, force: true });
562+
fs.rmSync(JSDOC_FILE, { recursive: true, force: true });

build/config.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

build/jsdoc.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"serveWithWebCache": "npm run _releaseWebCache && npm run _serveWithWebCacheHelp && http-server ./dist -p 8000 -c-1",
7777
"serveExternal": "http-server . -p 8000 -a 0.0.0.0 --log-ip true -c-1",
7878
"createJSDocs": "gulp createJSDocs && git add docs",
79+
"createMDXDocs": "node build/api-docs-generator.js",
7980
"_translateStrings": "gulp translateStrings",
8081
"_minify": "r.js -o require.min.config.js && echo this is untested see https://stackoverflow.com/questions/14337970/minifying-requirejs-javascript-codebase-to-a-single-file"
8182
},

0 commit comments

Comments
 (0)