我想使用jq将包依赖项添加到package.json文件by bash脚本中.

Add new element to existing JSON array with jq

下面是测试脚本,包含5个步骤:

#!/bin/bash

add_element_with_jq() {
    
    # step 1: Add a new root element "{}" first
    echo "============= step 1 ================"
    
    echo "{}" > "package.json"
    
    # step 2: Add a "dependencies" sub element to the root element "{}"
    echo "============= step 2 ================"
    # step 3: Add a "foo" package with version "1.1.1" to the "dependencies"
    echo "============= step 3 ================"
    
    jq '. += dependencies {
      "foo": "1.1.1"
    }' "package.json"
    
    echo "=========== step 3 output =================="
    cat "package.json"
    
    # step 4: Add a "bar" package with version "2.2.2" to the "dependencies"
    echo "============= step 4 ================"
    
    jq '.dependencies += 
      "bar": "2.2.2"
    ' "package.json"
    
    # step 5: If the "foo" package already existed, then update to the latest version. Otherwise, add the "foo" package to the dependencies.
    echo "============= step 5 ================"
    
    jq '.dependencies += 
      "foo": "3.3.3"
    ' "package.json"
    
    echo "=========== final output =================="
    cat "package.json"
}

add_element_with_jq

错误是:

============= step 1 ================
============= step 2 ================
============= step 3 ================
jq: error: syntax error, unexpected '{', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
. += dependencies {                  
jq: 1 compile error
=========== step 3 output ==================
{}
============= step 4 ================
jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 2:
      "bar": "2.2.2"           
jq: 1 compile error
============= step 5 ================
jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at <top-level>, line 2:
      "foo": "3.3.3"           
jq: 1 compile error
=========== final output ==================
{}

以下是预期输出:

=========== step 3 output ==================
{
  "dependencies": {
    "foo": "1.1.1"
  }
}
=========== final output ==================
{
  "dependencies": {
    "foo": "3.3.3",
    "bar": "2.2.2"
  }
}

我的bash函数出了什么问题,以及如何修复jq命令?

推荐答案

首先,你需要的是:

cat <<'.' >package.json
{
   "dependencies": {
      "foo": "3.3.3",
      "bar": "2.2.2"
   }
}
.

但让我们假设你有理由把它分解成几个步骤.


链接的答案是关于添加到array.您并没有try 添加到数组中.要向对象添加键和值,只需使用

.object["key"] = value

因此,一次只做一个加法,程序将如下所示:

{ } |
.dependencies = { } |
.dependencies[ "foo" ] = "3.3.3" |
.dependencies[ "bar" ] = "2.2.2"

Demo在jqplay.

作为单独调用jq,我们得到以下结果:

printf '{ }\n' |
jq '.dependencies = { }' |
jq '.dependencies[ "foo" ] = "3.3.3"' |
jq '.dependencies[ "bar" ] = "2.2.2"' >package.json

作为单个管道,这些命令仍然纠缠在一起.为了将它们分开,我们需要一个临时文件或sponge命令行实用程序.

printf '{ }\n'                                          >package.tmp1.json
jq '.dependencies = { }'              package.tmp1.json >package.tmp2.json
jq '.dependencies[ "foo" ] = "3.3.3"' package.tmp2.json >package.tmp3.json
jq '.dependencies[ "bar" ] = "2.2.2"' package.tmp3.json >package.tmp4.json

mv package.tmp4.json package.json
rm package.tmp{1,2,3}.json

如果你想添加.dependencies只在它不存在的情况下呢?取代

.dependencies = { }

.dependencies //= { }

Node.js相关问答推荐

如何呈现ejs.renderFile中包含的子模板?

如果我加入另一个公会且我的​​机器人已在其中,欢迎消息发送错误

在nodejs中为oauth请求创建和存储csrf令牌的最佳方法

在 Docker 容器内创建一个 cron 作业(job)来执行 run.js 文件中的函数

NodeJS:zlib.gzipSync 在不同平台上给出不同的明文输出

SvelteKit应用程序立即退出,没有错误

使用更新版本仍然找到包@angular/fire但不支持原理图

如何使用包含条件正确分页的 sequelize 查询?

mongoose 7.0.3 使用运算符 $and 严格搜索日期

在 Header 中使用 Authorization 方法和新的附加参数:accessToken 有什么区别?

为什么我的 Cypress Post 请求的请求正文是空的?

Express.js - 监听请求中止

在 node:readline 中按 CTRL+D 时会发出什么信号?

如何让我的 Next.js 应用在运行 ubuntu-latest 的 Azure 应用服务中启动?

如何防止 node.js 中的内存泄漏?

在多个文件 node.js 之间共享和修改变量

如何将`yarn.lock`与`package.json`同步?

在 Jade 包含中使用变量

如何让 Mocha 加载定义全局挂钩或实用程序的 helper.js 文件?

Npm postinstall 仅用于开发