14

It may not be possible (because this is JSON not JavaScript). I'm just trying to think of the simplest way to insert a datestamp in a string from an npm command without adding the overhead of another task runner etc:

"scripts": {
    "deploy" : "git add -A; git commit -m \"automated deployment {DateStamp}\"; git push deployment browse --force;"
},

And no need to chide me for using --force ;)

Thiago Rosa
  • 311
  • 1
  • 12
Damon
  • 10,493
  • 16
  • 86
  • 144

2 Answers2

24

NPM scripts are just bash scripts. Use bash features to add datestamp to some commit message.

Example:

  "scripts": {
    "deploy" : "git add -A; timestamp=$(date \"+%s\") && git commit -m \"automated deployment $timestamp\"; git push deployment browse --force;"
  },
Dmitry Somov
  • 406
  • 3
  • 4
  • 1
    Great.. I [used `%c` instead of `%s`](https://www.gnu.org/software/coreutils/manual/html_node/Date-conversion-specifiers.html#Date-conversion-specifiers) to make it more readable.. but you did give me exactly what I asked for ;) – Damon Oct 15 '15 at 19:10
  • 20
    Is there a platform independent way to achieve the same result on Windows/Linux? – Lanti Apr 07 '17 at 13:46
  • 2
    Another example using a file-friendly datetime: `"table-to-csv": "TODAY=$(date \"+%Y-%m-%d_%H-%M-%S\"); node script.js --file exported-at-$TODAY.csv"` – Vadorequest Jun 26 '19 at 09:21
  • To use same script on windows, see this SO answer: https://stackoverflow.com/a/25614832/1012616 – piotr_cz Jun 08 '22 at 08:09
0

Result: 1685441258

You can pass the date like this:

{
    "name": "javascript",
    "config" : { "filename" : "test2" },
    "version": "1.0.0",
    "description": "My javascript",
    "scripts": {
        "build": "remotion render HelloWorld out/$(date \"+%s\").mp4",
    }
}

Output: enter image description here

For result: 2023-05-31_13-15-30

To get the result in YYYY-MM-DD-HH-MM-ss: ie. 2023-05-31_13-15-30, you can do this:

"scripts": {
    "build": "remotion render src/index.tsx Story results/$(date \"+%Y-%m-%d_%H-%M-%S\").mp4",
}

enter image description here enter image description here

Shubham Verma
  • 8,783
  • 6
  • 58
  • 79