2

I am writing to a js file using protractor as follows :

index.js

var outputFile = '../Actions/data_write.js';

var username = "someusername";
var password = "somepassword";

var fs = require('fs');

var text =  "userCredentials : {username : '"+username+"', password : '"+password+"'};";

fs.writeFile(outputFile, text, function(error){
    if(error){
        console.log(error);
    }else{
        console.log('data saved to '+outputFile);
    }
});

My issue is I am not able to figure out how to write this data at specific location. So like right now the text is written to the entire file by replacing the old content. I want to write it at say at specific location, say I have multiple data with username and password defined in array in this file. I want to write to that specific array.

Eugene
  • 10,957
  • 20
  • 69
  • 97

1 Answers1

2

FileSystem APIs do not provide support for editing a file, rather focus on file operations.

You can read the older content into an object or a string, then navigate through it to figure out the right position for insertion, add your new content, and then write the transformed data back into the file.

If your app involves intensive file content modification with seeking to and fro, you may consider writing a native add-on with memory mapped file, for efficiency and performance.

Hope this helps.

Gireesh Punathil
  • 1,344
  • 8
  • 18