Creating a conditional npm script that depends on the output of another: a step-by-step guide

I've encountered this problem before while setting up npm scripts, and I haven't been able to find a solution yet. I'm hoping to make the solution compatible with both Mac and Windows environments.

I'm working on splitting up the logic of my npm scripts into smaller scripts that can interact with each other. This, combined with variables, would make the scripts more manageable and readable for other developers.

However, I'm struggling to use the result of one npm script in another. Since I'm not a bash expert, I'm seeking assistance here.

My current goal is to create a script that easily launches a docker (virtual) environment.

In the package.json file:


...
"config": {
    "docker": {
        "container": "docker-test"
    }
},
"scripts": {
    "container_run": "docker run -d -p 80:80 $npm_package_config_docker_container",
    "container_running": "docker inspect -f {{.State.Running}} $npm_package_config_docker_container || echo 'false'",
    "container_stop": "docker stop $npm_package_config_docker_container && docker rm $npm_package_config_docker_container",
    "start": "???"
}

Here, I have a variable storing the name of the docker container to be run:

$npm_package_config_docker_container
.

When I execute npm run container_running, it returns true if the container is running, or throws an error if the container doesn't exist, echoing 'false'.

Now, for the start script, I want it to check if the container is running. If it is, stop the container, then start a new one. If it's not running, just start a new one. Something like this:

"start": "if [ container_running == 'true' ]; then container_stop && container_run; else container_run; fi"

How can I achieve this? And as a bonus question, can it be done in a way that works on both Mac and Windows?

Any assistance would be greatly appreciated!

Answer №1

After doing some further research, I managed to find a solution to the problem, although I believe there is room for improvement.

To achieve this, you can utilize the concept of command substitution by executing $(npm run container_running). This will capture all the output generated by the command within a subshell. Subsequently, you can examine this output to verify if the last word is 'true'.

"start": "if [[ $(npm run container_running) == *true ]]; then npm run container_stop && npm run container_run; else npm run container_run; fi"

This method has not been tested on a Windows operating system yet.

If anyone has a more efficient idea or solution, please do not hesitate to share it!

Answer №2

Personally, when it comes to tasks that require more than just a simple command with options, I prefer to set up a bin directory and include a custom script file for it. Then, I have one of the main scripts call that specific file instead. For example:

"container_run": "./bin/container-run"

The beauty of this approach is that you don't necessarily need to be a bash guru since you can start the file with #! /usr/env node and continue writing the rest in JavaScript. It's a more organized and manageable way to handle complex tasks.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent additional functions from running when clicking in Angular 5

I have implemented an Angular material table where each row expands when clicked. In the last cell of the table, I load a component dynamically using ComponentFactory. This loaded component is a dropdown menu. The problem arises when the dropdown menu is ...

Encountered a 404 error while trying to delete using VueJS, expressJS, and axios. Request failed with

Currently, I'm in the process of learning the fundamentals of creating a MEVN stack application and facing a challenge with the axios DELETE request method. The issue arises when attempting to make a DELETE request using axios, resulting in a Request ...

How to send information to a modal component in ReactJS?

I'm feeling a bit lost here, maybe I'm missing something. What I am trying to achieve is a loop that populates an array with progress bar elements and then displays them with the relevant information. When a user clicks on a progress bar, the d ...

Vue.js Element UI form validation - showcasing errors returned by server

Utilizing Vue.js and Element UI libraries for my current project, I have implemented front-end validation with specific rules. However, I now also require the ability to display backend errors for the current field. When the form is submitted and an error ...

the pause in execution before my function redirects to a different route

Currently, I am developing a page using nodeJs with express which is supposed to display a table. However, I encountered an issue with my variable "allMusique" that contains the data for my page. When trying to access it initially, there seems to be an err ...

Retrieving JSON information from the server

I have been working with the knockout.js framework and adapted a basic contacts form example to suit my needs. I am able to successfully store values in my database, but I am encountering difficulties when trying to load values from the server. Despite h ...

What is the best way to programmatically click on an element within the body of a webpage from an Angular component?

I am running a crisp chat service on my website and I am attempting to interact with the chat box from one of my component's typescript files. The chat box is represented as a div element with the id crisp-client inside the body tag. Can someone plea ...

Utilizing CSS or JavaScript to define the input type

I currently have a group of checkboxes displayed on a webpage within a DIV labeled OPTIONS, shown below: <div id="options"> <input type="checkbox"..... > <input type="checkbox"..... > <input type="checkbox"..... > <input t ...

Expanding Image with HTML and CSS: A Guide

In the process of creating my website, I encountered an issue with the logo placement. I wanted the logo to be in the top left corner, similar to the one shown in this image. Initially, everything was working fine until I made some adjustments to move the ...

What causes arguments to be zeroed out during a WebAssembly imported function call?

I created a WASM module manually that can be decompiled using wasm2wat to reveal the code below. (module (type (;0;) (func)) (type (;1;) (func (param i32 i32))) (import "std" "print" (func (;0;) (type 1))) (func (;1;) (type 0) ...

Is NextJS rendering solely on the server, or is it capable of rendering on both

Within my users.js JSX file, I have an exported component that looks like this: ... return <MainContainer keywords="users"> export default Users During the process of SSR/SSG, the browser displays the users HTML (comprising of <li> t ...

What is the best way to show a message on a webpage after a user inputs a value into a textbox using

I have a JSFiddle with some code. There is a textbox in a table and I want to check if the user inserts 3000, then display a message saying "Yes, you are correct." Here is my jQuery code: $("#text10").keyup(function(){ $("#text10").blur(); ...

Searching for an individual MongoDB document using Express

I recently started working with MongoDB and express. I am trying to authenticate a user based on their username and password, but my code seems to always execute the "else statement" even when the correct credentials are entered. Below is the JavaScript f ...

Creating a Vue single-page application (SPA) and concealing the .vue files upon rendering in the

I have a Single Page App built using Vue.js and hosted on a node.js server. While the app is still in development, it will eventually be accessed by external customers. Due to the sensitive data involved, we want to prevent users from seeing the .vue files ...

Ensuring precise accuracy in JavaScript; transforming 0.5 into 0.5000

My current challenge involves converting every fraction number to n decimal places in JavaScript/Node.js. However, I've encountered a roadblock as it appears impossible to convert 0.5 to 0.5000. This discrepancy is causing my test cases that anticipat ...

Learn how to send dynamic data to another HTML element using Ajax's success function

I received a successful ajax response from one HTML file, but I'm struggling to dynamically set the data from this response to another HTML file. Can anyone help me with this issue? Here is the code snippet from my ajax report: success: function( ...

Having trouble getting an HTML form to work when making a PHP and Ajax request

I am trying to validate an HTML form using Ajax to prevent the browser from loading the page. Ideally, when a user enters their first name, it should display above the HTML form. However, I am encountering an issue where it is not showing up as expected... ...

What is the best method for calculating the total of a mongoose attribute?

I am attempting to calculate the sum of schema using reduce. However, the current code is not adding the items together but rather placing them next to each other. For example, 20 + 30 should result in 50, but instead it gives me 02030. Is there an issue w ...

Testing asynchronous actions in React Redux

Currently encountering an error while testing async actions. The error message reads TypeError: Cannot read poperty 'then' of undefined, pointing to the line below in my code. return store.dispatch(actions.fetchMovies()).then(() => { Below i ...

The Angular template driven forms are flagging as invalid despite the regExp being a match

My input looks like this: <div class="form-group"> <label for="power">Hero Power</label> <input [(ngModel)]="model.powerNumber" name="powerNumber" type="text" class="form-control" pattern="^[0-9]+$"id= ...