1
export const CURRENT_VERSION = '1.0.61 (2018.07.13.22.32) - UAT1'
export const CURRENT_ENVIRONMENT = ENVIRONMENT.TEST1
export const environmentVariable = getEnvironmentFile(CURRENT_ENVIRONMENT)

app.js file content is as above.

I am trying to replace the content of the file, during the build time as below

sh "./envScript 'app.js' 2 \"$VERSION_NUMBER ($BUILD_NUMBER) - PROD\""

and my envScript.sh is as below

#!/bin/bash
if [ $# == 3 ]; then
    sed -i '' '/export const CURRENT_VERSION =/d' $1
    sed -i '' '/export const CURRENT_ENVIRONMENT =/d' $1
    sed -i '' '$a\
    export var CURRENT_VERSION = '"'$3'" $1
    case $2 in
        0)
            sed -i '' '$a\
            export var CURRENT_ENVIRONMENT = ENVIRONMENT.UAT1' $1
        ;;
        1)
            sed -i '' '$a\
            export var CURRENT_ENVIRONMENT = ENVIRONMENT.UAT2' $1
        ;;
        2)
            sed -i '' '$a\
            export var CURRENT_ENVIRONMENT = ENVIRONMENT.PROD' $1
        ;;
    esac
else
    echo "You need to pass AppConfig.js path as a parameter"
fi

problem, this works partially, but always, updates the CURRENT_VERSION and CURRENT_ENVIRONMENT , towards the end of the file, due to which getEnvironmentFile(CURRENT_ENVIRONMENT) defaults to CURRENT_ENVIRONMENT=undefined option , since

export const CURRENT_VERSION = '1.0.61 (2018.07.13.22.32) - UAT1'

goes at the end of the file.

requested help : how can we use sed to do inplace edits at the same line in the file, basically find and replace in this situation , so that getEnvironmentFile(CURRENT_ENVIRONMENT) would always find CURRENT_ENVIRONMENT before calling, and won't default to undefined. Any help is really appreciated.

Original app.js before running the script

export const CURRENT_VERSION = '1.0.61 (2018.07.13.22.32) - UAT1'
export const CURRENT_ENVIRONMENT = ENVIRONMENT.TEST1
export const environmentVariable = getEnvironmentFile(CURRENT_ENVIRONMENT)

currently modified app.js

after running the command

sh "./envScript '../../App/Config/AppConfig.js' 2 \"$VERSION_NUMBER ($BUILD_NUMBER) - PROD\""

updated file

export const environmentVariable = getEnvironmentFile(CURRENT_ENVIRONMENT)
export const CURRENT_VERSION = '1.0.61 (2018.07.13.22.32) - PROD'
export const CURRENT_ENVIRONMENT = ENVIRONMENT.PROD

expected result

export const CURRENT_VERSION = '1.0.61 (2018.07.13.22.32) - PROD'
export const CURRENT_ENVIRONMENT = ENVIRONMENT.PROD
export const environmentVariable = getEnvironmentFile(CURRENT_ENVIRONMENT)
raaone7
  • 529
  • 1
  • 7
  • 16
  • 1
    What's the desired output exactly? – Benjamin W. Jul 24 '18 at 14:09
  • AND show a typical call to your script with values for $1,$2,$3. Good luck. – shellter Jul 24 '18 at 15:42
  • Also, you say "inplace edits", "find and replace" but your `sed` code is always appending to the end of the file `$a\...`. (which might work). So we need to see your required output sample. Good luck. – shellter Jul 24 '18 at 15:44
  • my bad, have edited the information – raaone7 Jul 25 '18 at 00:25
  • very good update to your Q. But its really late for me. Ping tomorrow if you're still having trouble. AND clarify how you can call the script once, but get 3 expected outputs. I see 2 lines getting appended for each call to `./envScript`. Good luck. – shellter Jul 25 '18 at 04:12
  • Yep having the same trouble . Please help – raaone7 Jul 25 '18 at 06:15

1 Answers1

2

The problem is exactly what shellter says it is. Instead of using $a, you need to use s, like this:

 sed '/VERSION/s/UAT1/PROD/;/ENVIRONMENT/s/TEST1/PROD/' app.js
Michael Vehrs
  • 3,293
  • 11
  • 10