-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinject-json.js
34 lines (28 loc) · 1.21 KB
/
inject-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const fs = require('fs');
const path = require('path');
// File paths
const htmlFilePath = path.join(__dirname, 'index.html');
const jsonFilePath = path.join(__dirname, 'cocktails.json');
// Read files
const htmlContent = fs.readFileSync(htmlFilePath, 'utf8');
const jsonData = JSON.parse(fs.readFileSync(jsonFilePath, 'utf8'));
// Regex to find existing injected content
const scriptTagRegex = /<script id="cocktails-data" type="application\/json">[\s\S]*?<\/script>/;
// Inject JSON into a <script> tag
const injectedScript = `
<script id="cocktails-data" type="application/json">
${JSON.stringify(jsonData, null, 2)}
</script>
`;
// Remove any previously injected JSON content
let updatedHtmlContent;
if (scriptTagRegex.test(htmlContent)) {
// Replace existing content
updatedHtmlContent = htmlContent.replace(scriptTagRegex, injectedScript.trim());
} else {
// Add new content before </body>
updatedHtmlContent = htmlContent.replace('<script src="render-script.js"></script>', `${injectedScript}<script src="render-script.js"></script>`);
}
// Write updated content back to the HTML file
fs.writeFileSync(htmlFilePath, updatedHtmlContent, 'utf8');
console.log('Cocktails JSON successfully injected into index.html');