Files
memory-infrastructure-palace/code/utils/git/updateReadme.js

75 lines
2.6 KiB
JavaScript

/**
* This file is responsible for updating the README file with file links and descriptions.
* It reads the files in the specified directory, extracts their descriptions, and generates
* markdown links for each file. Then, it updates the README file with the generated links
* and descriptions.
*/
import fs from 'fs';
import path from 'path';
const resolvedPath = path.resolve();
console.log(resolvedPath);
const utilsDir = path.join(resolvedPath, 'src/utils');
const bookmarkletsDir = path.join(resolvedPath, 'src/bookmarklets');
const readmePath = path.join(resolvedPath, 'README.md');
fs.readdir(utilsDir, (err, files) => {
function readDirectory(dir) {
const files = fs.readdirSync(dir);
const fileLinks = [];
files.forEach(file => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isFile()) {
const fileContent = fs.readFileSync(filePath, 'utf8');
const description = getFileDescription(fileContent);
fileLinks.push(
`## [${file}](${path.relative(resolvedPath, filePath)})\n${description}`
);
} else if (stats.isDirectory()) {
fileLinks.push(...readDirectory(filePath));
}
});
return fileLinks;
}
const directories = [utilsDir, bookmarkletsDir]; // Add more directories as needed
let fileLinks = [];
directories.forEach(dir => {
fileLinks.push(...readDirectory(dir));
});
const readmeContent = fs.readFileSync(readmePath, 'utf8');
let updatedReadmeContent = updateReadmeContent(readmeContent, fileLinks);
fs.writeFileSync(readmePath, updatedReadmeContent, 'utf8');
console.log('README updated successfully!');
});
function getFileDescription(fileContent) {
// Extract the description from the function comment in the file
// You can use regular expressions or any other method to extract the description
// Replace the following line with your implementation
const descriptionRegex = /\/\*(.*?)\*\//s;
const match = fileContent.match(descriptionRegex);
const description = match
? match[1].trim().replace(/^\s*\* ?/gm, '')
: 'No Description Provided'; // const description = match ? match[1].trim().replace(/^\*/gm, '') : 'No Description Provided';
return description;
}
function updateReadmeContent(readmeContent, fileLinks) {
const readmeSummaryIndex = readmeContent.indexOf('# File Summary');
if (readmeSummaryIndex !== -1) {
// If '# File Summary' is found, keep everything before it
readmeContent = readmeContent.slice(0, readmeSummaryIndex);
} // Append the new '# File Summary' section
readmeContent += '# File Summary\n\n' + fileLinks.join('\n\n');
return readmeContent;
}