在 cli 上解析 typescript 文件并删除元素
嘿,偷窥者,我正在寻找一种方法来解析打字稿文件,并轻松地从几个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以让 jq 获取整个打字稿(使用
-Rs
),提取并操作 JSON 部分(使用fromjson
和tojson
),然后编写它返回(使用-r
)。这要求您了解 Typescript 部分并对其进行建模,并容忍 JSON 部分的格式更改(基本上全部在一行中)。使用
捕获
的示例:演示
调整
(?;将 type Pools = )
导出为应使用正则表达式捕获为打字稿部分的任何内容,并将(.)
更改为您想要对 JSON 部分执行的任何操作jq。You could have jq take the whole typscript (using
-Rs
), extract and manipulate the JSON part (usingfromjson
andtojson
), 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
: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.我想你可以通过提取 json 字符串来应用
jq
。让我修改一下提供的 TypeScript 文件包含有效的 json 字符串。
file.ts
:然后尝试:
输出:
I suppose you can apply
jq
by extracting the json string. Let me modifythe provided TypeScript file to contain a valid json string.
file.ts
:Then try:
Output: