Ok, I need you integrate the first step of the new feature, which is looking inside the comp.html files, the usage of the prop.{key...}, weather it's inside the script tag, or into the component HTML through {...}. Once all the keys are collected, inject a console.log that prints them by component.
Focus on integrating this without breaking any other function from the component, don't go beyond, stay within this limits.
This is a sample of the syntax:
Don't develop anything else that I didn't requested to you.
Assistant
Understood. I'll provide a script that processes .comp.html files to:
Parse the HTML and Script Sections: Extract all instances of prop.{key} both within the HTML template and inside <script> tags.
Collect the Keys: Gather all unique keys used.
Inject a console.log Statement: Add a console.log at the beginning of the <script> tag to print the collected keys.
This script ensures that existing functionalities within the components remain unaffected.
Prerequisites
Node.js installed on your system.
cheerio library for HTML parsing. Install it using:
bash
npm install cheerio
Script Implementation
Create a file named injectConsoleLog.js and add the following code:
javascript
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
// Directory containing .comp.html files
const componentsDir = path.join(__dirname, 'components'); // Adjust the path as needed
// Regex to match prop keys in the form prop.key or {...prop.key}
const propRegex = /prop\.([a-zA-Z0-9_]+)/g;
// Function to process a single component file
function processComponent(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const $ = cheerio.load(content, { xmlMode: false, decodeEntities: false });
let propKeys = new Set();
// Extract prop keys from HTML
$('*').each((i, elem) => {
const html = $.html(elem);
let match;
while ((match = propRegex.exec(html)) !== null) {
propKeys.add(match[1]);
}
});
// Extract prop keys from <script> tags
$('script').each((i, script) => {
const scriptContent = $(script).html();
let match;
while ((match = propRegex.exec(scriptContent)) !== null) {
propKeys.add(match[1]);
}
});
if (propKeys.size === 0) {
console.log(`No prop keys found in ${path.basename(filePath)}`);
return;
}
// Prepare the console.log statement
const keysArray = Array.from(propKeys);
const consoleLogStatement = `console.log('Component "${path.basename(filePath)}" props:', { ${keysArray.join(
', '
)} });\n`;
// Inject the console.log into the first <script> tag
$('script').each((i, script) => {
const scriptContent = $(script).html();
if (scriptContent) {
// Prevent multiple injections
if (!scriptContent.includes(`console.log('Component "${path.basename(filePath)}'`)) {
$(script).html(consoleLogStatement + scriptContent);
}
return false; // Only process the first <script> tag
}
});
// Write the updated content back to the file
fs.writeFileSync(filePath, $.html(), 'utf-8');
console.log(`Injected console.log into ${path.basename(filePath)}`);
}
// Function to process all .comp.html files in the directory
function processAllComponents() {
fs.readdir(componentsDir, (err, files) => {
if (err) {
console.error('Error reading components directory:', err);
return;
}
const compFiles = files.filter((file) => file.endsWith('.comp.html'));
if (compFiles.length === 0) {
console.log('No .comp.html files found.');
return;
}
compFiles.forEach((file) => {
const filePath = path.join(componentsDir, file);
processComponent(filePath);
});
});
}
// Execute the processing
processAllComponents();
How It Works
Parsing the Components Directory: The script looks for all .comp.html files within the specified componentsDir. Adjust the componentsDir path as necessary to point to your components' directory.
Extracting prop Keys:
HTML Template: Uses Cheerio to traverse all HTML elements and extract matches of prop.{key}.
Script Tags: Similarly, it scans within <script> tags to find prop.{key} usages.
Injecting console.log:
Constructs a console.log statement that includes all unique prop keys.
Inserts this statement at the beginning of the first <script> tag in the component.
Ensures that it doesn't inject multiple console.log statements if the script is run multiple times.
Preserving Existing Functionality: By only adding the console.log at the start of the <script> tag and not altering other parts of the component, existing functionalities remain intact.