0

I need to add two strings in an js file I am generating with writeFileSync

This is the code example

const svgs = [];
var n = 2;

for (var i = 0; i < n; i++) {
  svgs.push(Cog.prototype.render());
  svgs.push(Manna.prototype.render());
}

fs.writeFileSync(`client/data/${name}.json`, JSON.stringify(svgs));

And this is the js file I am generating ( two objects in an array )

[{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}},{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}}]

My question is how can I add two strings, this one at the beginning const svgs = and export default svgs at the end in order to make my file generated looking like this

const svgs = [{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}},{"xmlns":"http://www.w3.org/svg/2000","viewBox":{"viewBox":"0 0 64 64"},"width":{"size":64},"height":{"size":64},"fill":{"fill":"currentcolor"},"path":{"d":{"pathData":"M 2 2 L 62 2 L 62 62 L 2 62 L 2 2"}}}] export default svgs;
2
  • fs.writeFileSync(client/data/${name}.json, "const svgs =" + JSON.stringify(svgs)) + "export default svgs" ; Commented Jul 1, 2016 at 9:42
  • add as answer , this is slightly corrected "const svgs =" + JSON.stringify(svgs).toString() + "; export default svgs" + ";"); Commented Jul 1, 2016 at 10:48

1 Answer 1

1
fs.writeFileSync(client/data/${name}.json, "const svgs =" + JSON.stringify(svgs)) + "export default svgs" ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.