Home > front end >  Parse typescript file on the cli and remove an element
Parse typescript file on the cli and remove an element

Time:02-24

Hey peeps I am looking for a way to parse a typescript file and easily remove and element from the embedded json object within a couple of codegen types.

Would be pretty easy with the likes of jq however that doesnt work for a typescript file with an embedded object like this I found.

Feel like this could be done with sed or awk however I havent had anyluck removing the whole `{..}' element.

before:

export type Pools = {
  "version": "0.1.0",
  "name": "hydra_liquidity_pools",
  "accounts": [
    {
      "name": "poolState",
      "type": {
        "kind": "struct",
        "fields": [
          ...  
          {
            "name": "reserved",
            "type": {
              "defined": "Reserve"
            }
          }
        ]
      }
    }
  ],
...
}

after:

export type Pools = {
  "version": "0.1.0",
  "name": "hydra_liquidity_pools",
  "accounts": [
    {
      "name": "poolState",
      "type": {
        "kind": "struct",
        "fields": [
          ...  
        ]
      }
    }
  ],
...
}

CodePudding user response:

I suppose you can apply jq by extracting the json string. Let me modify the provided TypeScript file to contain a valid json string.

file.ts:

export type Pools = {
  "version": "0.1.0",
  "name": "hydra_liquidity_pools",
  "accounts": [
    {
      "name": "poolState",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "foo": "bar"
          },
          {
            "name": "reserved",
            "type": {
              "defined": "Reserve"
            }
          }
        ]
      }
    }
  ]
}

Then try:

lval=$(sed -nE 's/(.*= ).*/\1/p' < file.ts)             # extract "export type Pools = "
echo "$lval" "$(sed "s/$lval//" file.ts | jq 'del(.accounts[].type.fields[] | select (.name == "reserved"))')"

Output:

export type Pools =  {
  "version": "0.1.0",
  "name": "hydra_liquidity_pools",
  "accounts": [
    {
      "name": "poolState",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "foo": "bar"
          }
        ]
      }
    }
  ]
}

CodePudding user response:

You could have jq take the whole typscript (using -Rs), extract and manipulate the JSON part (using fromjson and tojson), and write it back (using -r). This requires you to know and model the typescript part, and to condone the altered formatting of the JSON part (basically all in one line).

Example using capture:

jq -Rsr '
  capture("(?<ts>export type Pools = )(?<json>.*)";"m")
  | .json |= (fromjson | (.) | tojson)
  | .ts   .json
'
export type Pools = {"version":"0.1.0","name":"hydra_liquidity_pools","accounts":[{"name":"poolState","type":{"kind":"struct","fields":[{"name":"reserved","type":{"defined":"Reserve"}}]}}]}

Demo

Adjust (?<ts>export type Pools = ) to whatever should be captured as the typescript part using a regex, and change (.) to whatever you want to do with the JSON part using jq.

  • Related