Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: script automatically deletes non-required markdown files #1891

Merged
merged 1 commit into from
Oct 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion build/api-docs-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,63 @@ async function removeDir(dirPath) {
await fs.rm(dirPath, { recursive: true, force: true });
}

/**
* Responsible to extract file names with their parent dir
* Ex :- docs/API-Reference/worker/WorkerComm -> worker/WorkerComm
* @param {array} files list of all the files
* @param {string} excludeParent for files with no parent dir(eg:NodeConnector)
* @returns array of all file names
*/
function getFileNames(files, excludeParent) {
return files.map(filePath => {
// Extract directory and filename
const { dir, name } = path.parse(filePath);
// Get the parent folder name
const parentFolder = path.basename(dir);

// Check if the parent folder is the one to exclude
// Return only the base name if it's the excluded parent
if (parentFolder === excludeParent) {
return name;
}

// Combine if parent folder exists
return parentFolder ? `${parentFolder}/${name}` : name;
});
}

// Main async function to get existing markdown and js files
/**
* Responsible to remove all non-required markdown files
* If `@INCLUDE_IN_API_DOCS` is removed from the source file
* @param {array} jsFiles files to be included in API docs
*/
async function getExistingMarkdownFiles(jsFiles) {
const mdFiles = await globPromise(`./docs/API-Reference/**/*.md`);

// Get markdown file names without extensions
const mdFileNames = getFileNames(mdFiles, 'API-Reference');

// Get JS file names without extensions
const jsFileNames = getFileNames(jsFiles, 'src');

const filesToRemove = mdFileNames.filter(
mdFileName => !jsFileNames.includes(mdFileName)
);

for (const mdFile of mdFiles) {
const temp = String(getFileNames([mdFile], 'API-Reference'));
if (filesToRemove.includes(temp)) {
await fs.unlink(mdFile);
console.log(
`${mdFile} has been removed as no more needed in API docs`
);
}
}

console.log('All non required files removed successfully!');
}


/**
* Responsible to get the JS Files that are to be included in API DOCS
Expand Down Expand Up @@ -228,8 +285,9 @@ async function driver() {
try {
console.log("Fetching required JS files...");
const jsFiles = await getJsFiles();
console.log(`Found ${jsFiles.length} files to process`);
await getExistingMarkdownFiles(jsFiles);

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

Expand Down
Loading