Damon
Damon

Reputation: 10809

How can I add a timestamp into a script in package.json?

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 ;)

Upvotes: 15

Views: 11046

Answers (2)

Shubham Verma
Shubham Verma

Reputation: 9933

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

Upvotes: 0

Dmitry Somov
Dmitry Somov

Reputation: 416

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;"
  },

Upvotes: 25

Related Questions