2

I am creating a script to run through the entire 'build and push to git' flow. To commit my changes, I need to include the commit messages, but I want to have a unique message for every execution. Can I include a bash variable so that the user can create a message? I essentially want to do something like:

//package.json
{
  ...
  "scripts": {
     "build:push2Git $message": "npm run build:local ;
        npm run build:webpack && git add . && git commit -m $message
        && git push origin my-branch || echo 'Failed to push to git'",
        ...
   }
}

And then the script would be run as `npm run build:push2Git 'commit message #23445'. Is this possible? I realize my example may be horribly wrong, just my thought to explain what I want and how I would think it would work.

Just to give an update to what I tired, and passing it as a bash variable doesn't work.

So I tried :

//package.json
{
  ...
  "scripts": {
     "build:push2Git $message": "npm run build:local ;
        npm run build:webpack && git add . && git commit -m '$1'
        && git push origin my-branch || echo 'Failed to push to git'",
        ...
   }
}

and then try to call it as npm run build:push2Git my_custom_message . Does not work. Pushed my code to my stash with the commit message "$1".

user3334871
  • 1,251
  • 2
  • 14
  • 36
  • Also tried to do it without the quotation marks, errors out. – user3334871 Nov 27 '17 at 22:30
  • 1
    What's the use of `$message` in front of `build:push2Git` ? this is working fine on my side : `"build:push2Git": "git commit -m $1"` when running `npm run build:push2Git "this is a commit message"` – Bertrand Martel Nov 28 '17 at 04:30
  • I feel that this is a typographical error like @betrandmartel. I suspect npm just does a look up in that script section then execs the value. So if you drop that $message it should work. – HSchmale Nov 28 '17 at 05:12
  • Possible duplicate of https://stackoverflow.com/questions/43705195/how-can-i-use-variables-in-package-json – tripleee Nov 28 '17 at 09:57
  • Maybe remove the bounty and accept the duplicate? – tripleee Nov 28 '17 at 09:57
  • @HSchmale doing it that way throws `error: switch 'm' requires a value` and doesn't commit. Fails with `failed to push to git my custom message`. Also, as I am relatively new to bash, I mistook naming a variable rather than using `$1`, `$2`, etc. – user3334871 Nov 28 '17 at 16:54
  • @tripleee I don't want to define the variable in the config script, I want to pass a value when I run the script, so it can be inserted dynamically. – user3334871 Nov 28 '17 at 16:55

1 Answers1

1

I'm not an npm coder, but I do know bash. Try this:

//package.json
{
  …
  "scripts": {
     "build:push2Git": "p2g() { npm run build:local ;
        npm run build:webpack && git add . && git commit -m '$@'
        && git push origin my-branch || echo 'Failed to push to git' ; }; p2g",
        …
   }
}

Assuming bash functions can be used and assuming scripts are called like bash aliases (with arguments getting tacked onto the end of the command), this should allow you to invoke your script as npm run build:push2Git -- "this is a commit message". (See also this answer.)

This establishes a shell function called p2g() that thanks to $@ will absorb your arguments (you really want "$@" to properly preserve your spacing, but I don't know how to properly escape or format the JSON to make that work). Arguments appear to need to be delimited by --.

Here's a simpler version that doesn't use a function:

//package.json
{
  …
  "scripts": {
     "build:push2Git": "git commit",
        …
   }
}

You can use this one like: npm run build:push2Git -- -m "this is a commit message"

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
  • I'll give you the answer, as it's what lead me to the answer that works for me! The only change I made to what you suggested was to get rid of `$message`, and change `'$@1' to just '$@'. Other then that, this was exactly what I needed, thanks! – user3334871 Nov 29 '17 at 21:40
  • Dang, that was a typo, sorry. and `$message` was a bad paste from your previous code. I'm glad you figured it out. I have updated my answer ([here](https://stackoverflow.com/revisions/47538103/3) is the last version of my answer with the errors in it). – Adam Katz Dec 01 '17 at 20:44