How to replace a multiple values in a minified javascript file with sed [duplicate]

  • 0
  • Pump00:00

I need to replace a couple values in a minified javascript file with sed. The OS is alpine Linux.

I’m trying to workout a generic regex which will find foo: and any characters until the next , like so: foo:____ and replace with foo:"bar" or foo:!0 whatever the value in the variable might be. This script needs to work all the time. It shouldn’t depend on was is coming after the : ( meaning whatever the value from the property is).

Here are the first couple characters from the file

(window.webpackJsonp=window.webpackJsonp||[]).push([[5],{3:function(l,n,t){l.exports=t("zUnb")},AytR:function(l,n,t){"use strict";t.d(n,"a",function(){return e});var e={appVersion:"1.4-beta",buildNr:"123.123",production:!0,tracking:!0,apiHost:"/api/",foo:""}},Cdxl:function(l,n,t){"use strict";t.d(n,"a",function(){return a});

Here is my start it isn’t generic at all …

APP_VERSION='"1.4-beta"'
BUILD_NR='"20190118.1"'
PRODUCTION="!"
TRACKING="!0"
API_HOST='"/api/"'
FOO='"bar"'

# demo for the first 3 variables 
sed -i "s/appVersion:["0-9A-Za-z.]*/appVersion:${APP_VERSION}/;s/buildNr:["0-9A-Za-z.]*/buildNr:${BUILD_NR}/;s/production:[!0-9A-Za-z.]*/production:${PRODUCTION}/" path/to/js-file.js

I tried for hours – Any suggestions are more than welcome.

Thanks in advance!

Default Asked on February 7, 2020 in Programming.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.