【问题标题】:package.json - is it possible to use version as a variable in scriptpackage.json - 是否可以在脚本中使用版本作为变量
【发布时间】:2021-08-15 06:59:20
【问题描述】:

每次压缩我的项目时,我都需要手动将version与生成的文件名同步,例如CCCC007.tgz
在我的 package.json 中,compress-files 脚本如下:

{
  "name": "MyBuild",
  "version": "0.0.7",
  "scripts": {
    "dev": "nodemon --legacy-watch ./src",
    "start": "node src/",
    "compress-files": "tar --exclude='./node_modules' --exclude='./myBuild.pem' --exclude='./config/local.json' -zcvf ~/CCCC007.tgz . ",
  },
}

我想知道是否可以将version 设为变量并在脚本compress-files 中使用它,例如:

  "name": "MyBuild",
  "version": "0.0.7",
  "scripts": {
    "dev": "nodemon --legacy-watch ./src",
    "start": "node src/",
    "compress-files": "tar --exclude='./node_modules' --exclude='./myBuild.pem' --exclude='./config/local.json' -zcvf ~/CCCC${this.version}.tgz . ",
  },

【问题讨论】:

  • 这可能会帮助您“自动化版本控制和发布” itnext.io/…
  • @Nishant S Vispute You’ve read all of your member-only stories this month.
  • 对不起,我不明白你的评论是你在链接上看到的味精还是一个问题
  • @Nishant S Vispute 我无法查看您链接中的帖子,因为我不是会员
  • 我也不是该网站的成员,但它确实向我展示了这篇文章,不管怎样,这里的链接是来自 StackOverflow 的相同 stackoverflow.com/questions/13059991/… 的链接

标签: node.js


【解决方案1】:

您不能在 JSON 中引用另一个对象,当然也不能在值内引用。

/ 方向看,你好像在linux 上。您可能可以这样做:

"get-version": "node -e \"console.log(require('./package.json').version)\"",
"compress-files": "tar [...] `npm run get-version`",

话虽如此,有一些发布和版本控制工具可以让生活更轻松。你可以试试Release-It。但是...如果您只需要带有 tar 的 package.json 脚本,请不要让任何人告诉您应该以不同的方式进行操作。

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 2013-01-19
    • 1970-01-01
    • 2016-12-05
    • 2016-01-03
    • 1970-01-01
    • 2023-03-28
    • 2023-01-05
    • 2017-12-03
    相关资源
    最近更新 更多