Thnaks got it .
Modiy you script so that i can get all the html.json file to convert at once .
Here is the script if any one need.
import fs from 'fs';
import path from 'path';
// Function to extract and format the script part from the JSON object
function extractAndFormatScript(jsonString) {
const jsonObject = JSON.parse(jsonString);
//console.log(jsonObject?.data?.html);
const scriptContent = jsonObject?.data?.html;
return scriptContent;
}
// Function to read JSON file and extract script
function processJsonFile(filePath) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
const scriptContent = extractAndFormatScript(data);
if(scriptContent){
const newHtmlPath = path.join(path.dirname(filePath), 'new.html');
fs.writeFile(newHtmlPath, scriptContent, 'utf8', (err) => {
if (err) {
console.error('Error writing file:', err);
} else {
console.log(Parsed content written to ${newHtmlPath}
);
}
});
}
});
}
// Function to recursively get all files in a directory
function getAllFiles(dirPath, arrayOfFiles) {
const files = fs.readdirSync(dirPath);
arrayOfFiles = arrayOfFiles || [];
files.forEach((file) => {
const filePath = path.join(dirPath, file);
if (fs.statSync(filePath).isDirectory()) {
arrayOfFiles = getAllFiles(filePath, arrayOfFiles);
} else {
arrayOfFiles.push(filePath);
}
});
return arrayOfFiles;
}
// Function to process all html.json files in a directory and its subdirectories
function processAllHtmlJsonFiles(baseDir) {
const allFiles = getAllFiles(baseDir);
const htmlJsonFiles = allFiles.filter((file) => path.basename(file) === 'html.json');
htmlJsonFiles.forEach((file) => {
processJsonFile(file);
});
}
// Get the base directory from command line arguments
const baseDir = process.argv[2];
if (!baseDir) {
console.error('Please provide a base directory as an argument.');
process.exit(1);
}
// Run the script
processAllHtmlJsonFiles(baseDir);
Uses:
1. make a parser.mjs file and put this code inside
2. node parser.mjs /path/to/the/root/folder/
it will convert all html.json folder as new.html in its respective folder