From d1f83edea7447690eaba4fe7f495072354c3f030 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 8 Mar 2024 18:17:43 -0600 Subject: [PATCH] Add Run A Bash Script From A Node Script as a JavaScript TIL --- README.md | 3 +- .../run-a-bash-script-from-a-node-script.md | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 javascript/run-a-bash-script-from-a-node-script.md diff --git a/README.md b/README.md index 95b0416..b4ff604 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/javascript/run-a-bash-script-from-a-node-script.md b/javascript/run-a-bash-script-from-a-node-script.md new file mode 100644 index 0000000..a4b78fe --- /dev/null +++ b/javascript/run-a-bash-script-from-a-node-script.md @@ -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 ..." + } +} +```