Edit statement in a JS file using Applescript

Hi all,
I have a JS file contained in an application which I modify manually, save it and then carry out some tests . I have to just change the value of a variable over and again to do these tests. I would like to automate the process of changing value of this variable and later combine this with some GUI scripting which will eliminate the manual process that I’m doing right now.

I can loop through the all the lines in the file and get to the statement that needs replacement, but don’t know how to modify the statement and save the file.
This is the statement in my JS file which I modify manually every-time. I have to assign around 20 values to theTemplate variable

Here is the code that I have come up with so far. It’s incomplete right now. I’m just trying to modify one line in the file and Save it.

set theList to {"value1", "value2", "value3", "value4"....} -- This is the list of values that I will assign to theTemplate in JS file
set theFile to (POSIX file (configPath)) as alias
set fRef to (open for access theFile)
set theLines to paragraphs of (read fRef to eof)
repeat with nextLine in theLines
	if (nextLine as string) contains "theTemplate" then
		repeat with val in theList
			set theStatement to "theTemplate = " & val & ";"
			-- Code to overwrite the previous test value with the new test value and save the JS file.	
		end repeat
	end if
	end repeat
	close access fRef

In the above code, I’m constructing the statements “theTemplate= value2;”, “theTemplate= value3;” etc. programmatically. How do I change the line in this file and save it? Is this achievable? Is there an elegant way to do this?

set fRef to (open for access theFile)

set fRef to (open for access theFile with write permission)

set eof fRef to 0

set thewholeshebang to thelines as text
write thewholeshebang to fRef