I have a template JS file where I need to change 1 variable and then save a copy of that js file. I cannot use node because it does not support p5. I'm wondering if there is a way to do this locally.
let result;
function preload() {
result = loadStrings('template.js')
}
function setup() {
if (result[0] != null) {
result[0] = "let number = "+"\"" + 1 + "\"";
let writer = createWriter('modifiedTemplate.js');
writer.write(result)
writer.close()
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>
If I'm understanding correctly, you want to load a file, edit it and save it as a new file. In that case, what you have is pretty much there.
The only potential issues I can see are the following - Let's say you have a templates file that looks like:
let number = 10;
let count = 4;
And you want to change let number = 10 to let number = 1. First of all, you can use template literals to do that:
result[0] = `let number = 1;`;
Then what you have should save the file, however it wouldn't be in the format you might expect - the output of the file would be:
let number = 1;,let count = 4;
That's the nature of the javascript toString method. I'd recommend joining the array and separating each line by a new line:
writer.write(result.join('\n'));
Which should output like the following:
let number = 1;
let count = 4;
If you get stuck I've implemented this in a p5.js sketch which should point you in the right direction.