mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Run A Bash Script From A Node Script as a JavaScript TIL
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1392 TILs and counting..._
|
||||
_1393 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -488,6 +488,7 @@ _1392 TILs and counting..._
|
||||
- [Reach Into An Object For Nested Data With Get](javascript/reach-into-an-object-for-nested-data-with-get.md)
|
||||
- [Render An Array Of Elements With React 16](javascript/render-an-array-of-elements-with-react-16.md)
|
||||
- [Resolve And Pass Multiple Values From A Then](javascript/resolve-and-pass-multiple-values-from-a-then.md)
|
||||
- [Run A Bash Script From A Node Script](javascript/run-a-bash-script-from-a-node-script.md)
|
||||
- [Run Multiple Node Scripts Concurrently](javascript/run-multiple-node-scripts-concurrently.md)
|
||||
- [Running ES6 Specs With Mocha](javascript/running-es6-specs-with-mocha.md)
|
||||
- [Scoping Variables With A Block Statement](javascript/scoping-variables-with-a-block-statement.md)
|
||||
|
||||
43
javascript/run-a-bash-script-from-a-node-script.md
Normal file
43
javascript/run-a-bash-script-from-a-node-script.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Run A Bash Script From A Node Script
|
||||
|
||||
A node script in `package.json` can reference and run any command or executable
|
||||
script available on our system. This is why we're able to have node scripts
|
||||
that do things like run a linter or start a web server.
|
||||
|
||||
We can take this a step further by defining our own scripts to enhance our
|
||||
development and CI environments.
|
||||
|
||||
For instance, let's say we have the following bash script (`check-stripe`) in
|
||||
our project's `scripts` directory for checking that we are connected to the
|
||||
right Stripe account.
|
||||
|
||||
```bash
|
||||
#!/bin/bash
|
||||
|
||||
# Expected stripe display name
|
||||
EXPECTED_STRING="My Online Product"
|
||||
|
||||
# Currently connected stripe display name
|
||||
COMMAND_OUTPUT=$(
|
||||
stripe config --list | grep '^display_name' | awk -F'=' '{print $2}' | xargs
|
||||
)
|
||||
|
||||
# Compare the expected string with the command output
|
||||
if [ "$EXPECTED_STRING" = "$COMMAND_OUTPUT" ]; then
|
||||
exit 0
|
||||
else
|
||||
echo "Mismatched Stripe accounts, expected $EXPECTED_STRING, got $COMMAND_OUTPUT."
|
||||
exit 1
|
||||
fi
|
||||
```
|
||||
|
||||
This script exits with either a successful (`0`) or failed (`1`) status. We can
|
||||
use this in our node script to guard another command from getting run.
|
||||
|
||||
```json
|
||||
{
|
||||
"scripts": {
|
||||
"dev:stripe": "./scripts/check-stripe && stripe listen ..."
|
||||
}
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user