在 cli 上解析 typescript 文件并删除元素

发布于 2025-01-09 12:12:22 字数 862 浏览 1 评论 0原文

嘿,偷窥者,我正在寻找一种方法来解析打字稿文件,并轻松地从几个 codegen 类型中嵌入的 json 对象中删除和元素。

使用 jq 之类的东西会很容易,但是这对于我发现的带有嵌入对象的打字稿文件不起作用。

感觉这可以用 sed 或 awk 来完成,但是我没有运气删除整个“{..}”元素。

之前:

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

之后:

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

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": [
          ...  
        ]
      }
    }
  ],
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2025-01-16 12:12:22

您可以让 jq 获取整个打字稿(使用 -Rs),提取并操作 JSON 部分(使用 fromjsontojson),然后编写它返回(使用-r)。这要求您了解 Typescript 部分并对其进行建模,并容忍 JSON 部分的格式更改(基本上全部在一行中)。

使用捕获的示例:

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"}}]}}]}

演示

调整(?;将 type Pools = ) 导出为应使用正则表达式捕获为打字稿部分的任何内容,并将 (.) 更改为您想要对 JSON 部分执行的任何操作jq。

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.

超可爱的懒熊 2025-01-16 12:12:22

我想你可以通过提取 json 字符串来应用 jq 。让我修改一下
提供的 TypeScript 文件包含有效的 json 字符串。

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"
            }
          }
        ]
      }
    }
  ]
}

然后尝试:

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"))')"

输出:

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

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"
          }
        ]
      }
    }
  ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文