project-root/ ├── cat/ │ ├── bin/ │ │ └── cat.js │ └── src/ │ └── compiler.js ├──…
- User
project-root/ ├── cat/ │ ├── bin/ │ │ └── cat.js │ └── src/ │ └── compiler.js ├── index.html ├── package.json └── cat.config.json
// cat/src/compiler.js
const fs = require('fs-extra'); const path = require('path'); const glob = require('glob');
/**
- Compiler Function
- @param {string} indexPath - Path to the index.html file.
- @returns {Promise} - Compiled HTML content. */ async function compile(indexPath) { // Read the index.html content let indexContent = await fs.readFile(indexPath, 'utf-8');
// Find all .comp.html files in the project directory const projectRoot = path.dirname(indexPath); const compFiles = glob.sync('/*.comp.html', { cwd: projectRoot, absolute: true, ignore: ['node_modules/', 'cat/**'] // Ignore cat directory to prevent recursion });
// For each component file, inject its content into index.html for (const file of compFiles) { const componentName = path.basename(file, '.comp.html'); const componentContent = await fs.readFile(file, 'utf-8');
code// Define a placeholder in index.html where the component should be injected // For example: <!-- inject:componentName --> const placeholder = `<!-- inject:${componentName} -->`; if (indexContent.includes(placeholder)) { // Replace placeholder with component content indexContent = indexContent.replace(placeholder, componentContent); } else { // If no placeholder found, append the component at the end indexContent += `\n<!-- Component: ${componentName} -->\n${componentContent}`; }}
return indexContent; }
module.exports = { compile };
#!/usr/bin/env node
const { program } = require('commander'); const path = require('path'); const fs = require('fs-extra'); const compiler = require('../src/compiler'); const express = require('express');
/**
- Initialize the package.json and cat.config.json */ function initializeProject() { const projectRoot = process.cwd(); const packageJsonPath = path.join(projectRoot, 'package.json'); const configPath = path.join(projectRoot, 'cat.config.json');
// Initialize package.json if it doesn't exist if (!fs.existsSync(packageJsonPath)) { const defaultPackageJson = { name: path.basename(projectRoot), version: "1.0.0", description: "A project using the Cat framework.", main: "index.html", scripts: { cat: "node ./cat/bin/cat.js" }, dependencies: { "commander": "^10.0.0", "express": "^4.18.2", "fs-extra": "^11.1.1", "glob": "^10.3.3" }, author: "Your Name", license: "MIT" }; fs.writeJsonSync(packageJsonPath, defaultPackageJson, { spaces: 2 }); console.log('✅ package.json has been created at the project root.'); console.log('👉 Please run "npm install" to install the dependencies.'); } else { console.log('ℹ️ package.json already exists.'); }
- Assistant