1

Please help me with the next PowerShell script. I would like to create a PowerShell script that gets a boolean parameter (isProsuction) and comment out a line in api-requests.js file based on isProsuction parameter value:

If provided parameter isProduction = true, I want to comment out the line with port 3366 and uncomment the line with port 5566:

const basicPath = "http://serviceate01:5566/";
// const basicPath = "http://serviceate01:3366/";

and if isProduction = false, I want to comment out the line with port 5566 and uncomment the line with port 3366:

// const basicPath = "http://serviceate01:5566/";
const basicPath = "http://serviceate01:3366/";

The api-requests.js file is located in the current working directory.

1
  • Why comment out? There are simpler ways. Is commenting out a requirement? Commented Jun 28, 2022 at 8:22

2 Answers 2

1

I would use switch for that to examine the lines on-by-one, updating where needed

if ($isProduction) {
    $commentOut = '*const basicPath*=*:3366/";'
    $unComment  = '*const basicPath*=*:5566/";'
}
else {
    $commentOut = '*const basicPath*=*:5566/";'
    $unComment  = '*const basicPath*=*:3366/";'
}

$updated = switch -Wildcard -File 'api-requests.js' {
    $commentOut { '// {0}' -f $_.TrimStart(" /") }
    $unComment  {  $_.TrimStart(" /") }
    default     { $_ }   # output this line unchanged
}

# save the updated file
$updated | Set-Content -Path 'api-requests.js'

Aha, I didn't know you ran the code by saving it as script and call that. If you want it like that, you need to add a param() block to the script file like this:

param (
    [switch]$isProduction
)

if ($isProduction) {
    $commentOut = '*const basicPath*=*:3366/";'
    $unComment  = '*const basicPath*=*:5566/";'
}
else {
    $commentOut = '*const basicPath*=*:5566/";'
    $unComment  = '*const basicPath*=*:3366/";'
}

$updated = switch -Wildcard -File 'D:\Test\api-requests.js' {
    $commentOut { '// {0}' -f $_.TrimStart(" /") }
    $unComment  {  $_.TrimStart(" /") }
    default     { $_ }   # output this line unchanged
}

# save the updated file
$updated | Set-Content -Path 'D:\Test\api-requests.js'

Since -isProduction is merely a boolean, I made it into a [switch] parameter so you don't even need to specify $true or $false, just use the switch (for $true) or leave it out (for $false)

Let's assume this is the content of the current file:

some javascript code;
const basicPath = "http://serviceate01:5566/";
// const basicPath = "http://serviceate01:3366/";
more code;

Now when we can call the script with PowerShell 'D:\Test\test.ps1' -isProduction:$false or simply leave out the switch so it will be interpreted as $false as in PowerShell 'D:\Test\test.ps1', the content is changed into:

some javascript code;
// const basicPath = "http://serviceate01:5566/";
const basicPath = "http://serviceate01:3366/";
more code;

By using the switch, with PowerShell 'D:\Test\test.ps1' -isProduction:$true, or simply PowerShell 'D:\Test\test.ps1' -isProduction, the content of the file is changed again into:

some javascript code;
const basicPath = "http://serviceate01:5566/";
// const basicPath = "http://serviceate01:3366/";
more code;

P.S. It might be a good idea if you added another (optional) parameter to the script file called [string]$Path = 'api-requests.js' where you can specify the path and filename of the javascript file to update.

Sign up to request clarification or add additional context in comments.

5 Comments

Theo, thanks for your response. It works only if both lines are uncommented. In my case, every time, one of the lines should be in the comment. so every time that script is executed it should uncomment or comment each lines depending on isProduction value.
@Maxim Have you actually tried the code? It uses wildcards which means the asteriks at the start makes it possible for any characters (the slashes and space in this case) to appear in front of the line. I have tested with your examples and the results were as expected.
Yes, I tried it, and it works for me only in the first execution when both lines are uncommented. But my initiative stet is when one of the lines is commented and the other is not: ''' // const basicPath = "serviceate01:5566"; const basicPath = "serviceate01:3366"; ''' and I'm running the script with the next line: PowerShell .\test.ps1 -isProduction $true it's not changing the line as expected to: ``` const basicPath = "serviceate01:5566"; // const basicPath = "serviceate01:3366"; ''' Please advise what I'm doing wrong?
@Maxim sorry for this late teply, but I'm not near a computer now. Will try and look at it tomorrow.
@Maxim Please see my edit. I have added a lot of explanation to it.
0

Thank you @Theo and someone else that responded and later removed his post.

As I mentioned before, I have a file api-requests.js, and there are two lines, one of them is in a comment, depending on the current usage (Production or Testing).

// const basicPath = "http://serviceate01:5566/"; #Production
const basicPath = "http://serviceate01:3366/";    #Testing

I needed a Powershell script that gets a boolean parameter isProduction. Based on the value of isProduction parameter I need to comment out the appropriate line in api-requests.js file. This is the solution that worked for me, It's not the best one but it works as expected:

Param($isProduction) #should be in the first line of the PowerShell script.
Clear-Host

 if($isProduction -eq $true)
     {
        $search = (Get-Content -Path 'api-requests.js' | Select-String -Pattern '// const basicPath = "http://serviceate01:3366/";')
        if($search -eq $null){
            echo prod
            $fileContent = (Get-Content -Path 'api-requests.js');
            $updatedContent = $fileContent -replace 'const basicPath = "http://serviceate01:3366/";', '// const basicPath = "http://serviceate01:3366/";';
            $updatedContent | Set-Content -Path 'api-requests.js';

            $fileContent = (Get-Content -Path 'api-requests.js');
            $updatedContent = $fileContent -replace '// const basicPath = "http://serviceate01:5566/";', 'const basicPath = "http://serviceate01:5566/";';
            $updatedContent | Set-Content -Path 'api-requests.js';
        }
        else
        {
            Echo 'Already production';
        }   
     }
 else
     {
        $search = (Get-Content -Path 'api-requests.js' | Select-String -Pattern '// const basicPath = "http://serviceate01:5566/";')
        if($search -eq $null){
           echo testing
            $fileContent = (Get-Content -Path 'api-requests.js');
            $updatedContent = $fileContent -replace 'const basicPath = "http://serviceate01:5566/";', '// const basicPath = "http://serviceate01:5566/";';
            $updatedContent | Set-Content -Path 'api-requests.js';

            $fileContent = (Get-Content -Path 'api-requests.js');
            $updatedContent = $fileContent -replace '// const basicPath = "http://serviceate01:3366/";', 'const basicPath = "http://serviceate01:3366/";';
            $updatedContent | Set-Content -Path 'api-requests.js';
        }
        else
        {
            Echo 'Already testing';
        }
     }

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.