-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsToStringConverter.js
More file actions
27 lines (23 loc) · 869 Bytes
/
jsToStringConverter.js
File metadata and controls
27 lines (23 loc) · 869 Bytes
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
const { readFile, writeFile } = require('fs');
// Function to read a JS file and convert it to a JSON string
function convertJsFileToJsonString(inputFilePath, outputFilePath) {
readFile(inputFilePath, 'utf8', (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
// Create an object with the JS content as a string
const dataToWrite = {
code: data
};
// Write the object to a JSON file
writeFile(outputFilePath, JSON.stringify(dataToWrite, null, 2), (writeErr) => {
if (writeErr) {
console.error("Error writing the JSON file:", writeErr);
return;
}
console.log(`The file has been converted and saved as ${outputFilePath}`);
});
});
}
convertJsFileToJsonString(__dirname + "/codeExamples/loopsDont.js", __dirname + "/codeExamples/loopsDont.json");