-1

i've a js file called "env.js"

Content of "env.js": window.enableReports = true;
window.env = window.config.CLIENT_NAME;
window.app_version = "Beta-20210907.1";
window.combineDashboard = true;

How can i replace value of variable "window.app_version" using powershell

P.S i can't use a token as that version is being used by application. i want to update the variable value after every deployment so the only way out is the replace value of variable corresponding to "window.app_version"

MuhammadSannan
  • 109
  • 3
  • 15
  • Does this answer your question? [How can I replace every occurrence of a String in a file with PowerShell?](https://stackoverflow.com/questions/17144355/how-can-i-replace-every-occurrence-of-a-string-in-a-file-with-powershell) – msanford Sep 07 '21 at 17:56
  • @msanford i don't so as i want to set value of variable "window.app_version" to whatever i pass to the function/code. – MuhammadSannan Sep 07 '21 at 18:05
  • Yes, it does: put a token in its place and replace the token. Like `window.app_version = "WINDOW_APP_VERSION";` and then replace the token `WINDOW_APP_VERSION` with PowerShell. This is a very common pattern. – msanford Sep 07 '21 at 18:07
  • @msanford i can't use a token as that version is being used by application. i want to update the variable value after every deployment so the only way out is the replace value of variable corresponding to "window.app_version" – MuhammadSannan Sep 07 '21 at 18:12
  • 1
    `(Get-Content .\env.js) -Replace '(?<=window\.app_version\s=\s).*', 'OtherVersion' |Set-Content .\env.js` – iRon Sep 07 '21 at 18:34

1 Answers1

0

Reference to iRon comment: This Helped me:

Answere: (Get-Content .\env.js) -Replace '(?<=window.app_version\s=\s).*', 'OtherVersion' |Set-Content .\env.js

MuhammadSannan
  • 109
  • 3
  • 15