Home > front end >  How to comment out a line in a file using PowerShell script with parameter?
How to comment out a line in a file using PowerShell script with parameter?

Time:06-30

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.

CodePudding user response:

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.

CodePudding user response:

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';
        }
     }
  • Related