I am working in Windows 10 and trying to use npm script for git commit with message that includes date and time of the commit:
"deploy": "cd dist && git add . && git commit -m \"Release at $(date)\" && git push"
Resulting git commit message is
Release at $(date)
instead of
Release at 03/06/2019 11:43:57
that I get when running in terminal the same command:
git commit -m "Release at $(date)"
What is a cross platform solution ?
Can you explain what causes different results ?
Cross-platform compatibility cannot be realized by utilizing the shell command date that is available on *nix platforms. This is because the Windows/cmd.exe DATE command behaves differently. The differences are:
date command prints the date/time.DATE command prompts the user to set the system date/time.Also command substitution, i.e. the $(...) part is a bash feature found on most *nix shells - it will fail via Windows cmd.exe.
For a cross-platform solution (i.e. one that runs successfully on Windows, Linux, and macOS...), consider the following approach:
cd and git commands using the built-in execSync().Date() object similar to this answer instead.scripts section of your package.json
There are a couple of different ways to approach this as described in the following two sub-sections titled:
Note: both approaches effectively yield the same desired result.
The following utilizes moment for obtaining the date. To install that run the following command in your project directory:
npm i -D moment
Create a nodejs script as follows, let's name the file deploy.js and save it in the root of your project directory, i.e. in the same directory where package.json currently resides::
deploy.js
const moment = require('moment');
const execSync = require('child_process').execSync;
const dateTime = moment().format('MM/DD/YYYY HH:mm:ss');
execSync(`cd dist && git add . && git commit -m \"Release at ${dateTime}\" && git push`, { stdio: [0, 1, 2]} );
In the scripts section of your package.json replace your current deploy script with the following:
package.json
"scripts": {
"deploy": "node deploy"
}
Invoke the npm deploy script as per normal by running the following via your CLI:
npm run deploy
Explanation:
moment package and the nodejs built-in execSync().moment() and call its format() method to match your given formatting, i.e. MM/DD/YYYY HH:mm:ss.cd and gitcommands using execSync. A reference to the date/time is provided in the git message part using Template literals, i.e.${dateTime}
options.stdio option configures the pipes between the parent and child process - [0, 1, 2] effectively inherit's stdin, stdout, and stderr.Alternatively, you can inline your nodejs/JavaScript code in the scripts section of your package.json.
In the scripts section of your package.json replace your current deploy script with the following instead:
package.json
"scripts": {
"deploy": "node -e \"const dateTime = require('moment')().format('MM/DD/YYYY HH:mm:ss'); require('child_process').execSync(`cd dist && git add . && git commit -m \"Release at ${dateTime}\" && git push`, { stdio: [0, 1, 2]} );\""
}
Explanation:
.js file (albeit slightly refactored). The use of a separate nodejs script/file is now redundant.-e is utilized to evaluate the inline JavaScript.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With