Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 338
Damon Avatar asked Oct 15 '15 14:10

Damon


People also ask

How does scripts work in package json?

Scripts are stored in a project's package. json file, which means they're shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.

What should I add to package json?

The package. json fields you'll be prompted for are name, version, description, main, test command, git repository, keywords, author, and license. It's okay to accept the defaults if you don't know what to put for a field. The important part is generating a package.

Can I use variables in package json?

To use variable, you need to declare a section named config (or something else, but not a name was already taken by the package. json ).


1 Answers

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;"
  },
like image 185
Dmitry Somov Avatar answered Oct 22 '22 15:10

Dmitry Somov