如何在package.json中的脚本中添加时间戳?

Dam*_*mon 7 javascript json node.js

它可能是不可能的(因为这是JSON而不是JavaScript).我只是想想一个从npm命令在字符串中插入日期戳的最简单方法,而不增加另一个任务运行器的开销等:

"scripts": {
    "deploy" : "git add -A; git commit -m \"automated deployment {DateStamp}\"; git push deployment browse --force;"
},
Run Code Online (Sandbox Code Playgroud)

而且无需责备我使用--force;)

Dmi*_*mov 15

NPM脚本只是bash脚本.使用bash功能将datestamp添加到某个提交消息中.

例:

  "scripts": {
    "deploy" : "git add -A; timestamp=$(date \"+%s\") && git commit -m \"automated deployment $timestamp\"; git push deployment browse --force;"
  },
Run Code Online (Sandbox Code Playgroud)

  • 是否有独立于平台的方法在Windows/Linux上实现相同的结果? (9认同)
  • 另一个使用文件友好日期时间的示例:`"table-to-csv": "TODAY=$(date \"+%Y-%m-%d_%H-%M-%S\"); 节点脚本。 js --file 导出在-$TODAY.csv"` (2认同)