const fs = require('fs');
const path = require('path');
// Configuration
const ROOT_DIR = path.join(__dirname, '..');
const DIST_DIR = path.join(__dirname, 'dist');
const FILES_DIR = path.join(DIST_DIR, 'files');
// Directories to exclude from scanning
const EXCLUDED_DIRS = ['.git', 'node_modules', 'site', 'assets'];
// File extensions to include
const INCLUDED_EXTENSIONS = ['.txt', '.json', '.md'];
// Utility function to escape HTML
function escapeHtml(text) {
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, m => map[m]);
}
// Generate HTML for individual file pages
function generateFileHTML(filePath, content, fileInfo) {
const relativePath = path.relative(ROOT_DIR, filePath);
const extension = path.extname(filePath);
const language = extension === '.json' ? 'json' : extension === '.md' ? 'markdown' : 'text';
return `
${escapeHtml(relativePath)} - System Prompts
Path: ${escapeHtml(relativePath)}
Size: ${fileInfo.size} bytes
Lines: ${content.split('\\n').length}
${escapeHtml(content)}
ā Back to Index
`;
}
// Generate HTML for index page
function generateIndexHTML(fileTree, stats) {
return `
System Prompts and Models of AI Tools
${stats.totalFiles}
Total Files
${stats.totalDirectories}
Directories
${Math.round(stats.totalSize / 1024)}KB
Total Size
${generateFileTreeHTML(fileTree)}
`;
}
// Generate HTML for file tree
function generateFileTreeHTML(tree) {
let html = '';
const sortedDirs = Object.keys(tree).sort();
for (const dir of sortedDirs) {
const files = tree[dir];
if (files.length === 0) continue;
html += `
š ${escapeHtml(dir)}
`;
const sortedFiles = files.sort((a, b) => a.name.localeCompare(b.name));
for (const file of sortedFiles) {
const icon = file.name.endsWith('.json') ? 'š' : file.name.endsWith('.md') ? 'š' : 'š';
html += `-
${icon}
${escapeHtml(file.name)}
`;
}
html += `
`;
}
return html;
}
// Scan directory recursively
function scanDirectory(dir, baseDir = dir, fileTree = {}, stats = { totalFiles: 0, totalDirectories: 0, totalSize: 0 }) {
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const relativePath = path.relative(baseDir, fullPath);
// Skip excluded directories
if (EXCLUDED_DIRS.some(excluded => relativePath.startsWith(excluded))) {
continue;
}
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
stats.totalDirectories++;
scanDirectory(fullPath, baseDir, fileTree, stats);
} else if (stat.isFile()) {
const ext = path.extname(fullPath);
if (INCLUDED_EXTENSIONS.includes(ext)) {
const dirName = path.dirname(relativePath);
const displayDir = dirName === '.' ? 'Root' : dirName;
if (!fileTree[displayDir]) {
fileTree[displayDir] = [];
}
stats.totalFiles++;
stats.totalSize += stat.size;
fileTree[displayDir].push({
name: path.basename(fullPath),
path: fullPath,
relativePath: relativePath,
id: Buffer.from(relativePath).toString('base64').replace(/[^a-zA-Z0-9]/g, '_'),
size: stat.size
});
}
}
}
return { fileTree, stats };
}
// Main build function
function build() {
console.log('š Starting build process...\n');
// Clean and create dist directories
if (fs.existsSync(DIST_DIR)) {
fs.rmSync(DIST_DIR, { recursive: true });
}
fs.mkdirSync(DIST_DIR, { recursive: true });
fs.mkdirSync(FILES_DIR, { recursive: true });
console.log('š Scanning repository...');
const { fileTree, stats } = scanDirectory(ROOT_DIR);
console.log(`\nš Statistics:`);
console.log(` - Total files: ${stats.totalFiles}`);
console.log(` - Total directories: ${stats.totalDirectories}`);
console.log(` - Total size: ${Math.round(stats.totalSize / 1024)}KB\n`);
// Generate individual file pages
console.log('š Generating file pages...');
let generatedPages = 0;
for (const dir in fileTree) {
for (const file of fileTree[dir]) {
try {
const content = fs.readFileSync(file.path, 'utf-8');
const html = generateFileHTML(file.path, content, { size: file.size });
const outputPath = path.join(FILES_DIR, `${file.id}.html`);
fs.writeFileSync(outputPath, html);
generatedPages++;
} catch (error) {
console.error(` ā ļø Error processing ${file.relativePath}: ${error.message}`);
}
}
}
console.log(` ā Generated ${generatedPages} file pages`);
// Generate index page
console.log('\nš Generating index page...');
const indexHTML = generateIndexHTML(fileTree, stats);
fs.writeFileSync(path.join(DIST_DIR, 'index.html'), indexHTML);
console.log(' ā Index page generated');
console.log('\n⨠Build completed successfully!');
console.log(`\nš Output directory: ${DIST_DIR}`);
console.log('š Run "npm run preview" to view the site locally\n');
}
// Run build
try {
build();
} catch (error) {
console.error('ā Build failed:', error);
process.exit(1);
}