Consider creating a distinct document for your "scripts"

Within my package.json configuration file, the "scripts" section contains numerous commands structured as shown below.

"scripts" {
  "script1": "command example",
  "script2": "command example",
  "script3": "command example",
  "script4": "command example",
  "script5": "command example",
  "script6": "command example",
  "script7": "command example",
  ...
}

Is there a method to relocate these commands to a separate file?

One idea I have is to transfer them to a file named bin/commands.js.

Answer №1

Dealing with an overloaded package.json due to excessive scripts is a common issue. Thankfully, Kent Dodds has developed a helpful solution known as NPS (npm-package-scripts). Unfortunately, options for addressing this problem are limited.

PRO: By defining scripts in a js file, users gain more flexibility, reduce clutter in package.json, and benefit from strong community support.

CON: The main downside is simply switching from npm to nps for command execution.

Answer №2

To utilize separate scripts, you can implement the following methods

Shell Script

if [ $1 == "1" ]; then
        sh <path to script1>
elif [ $1 == "2"]; then
        sh <path to script2>
else
        echo "script does not exist"
fi

Node Script

{
    var exec = require('child-process-promise').exec;
    let args = process.argv[2];
    let obj = {
        "script1": "command example",
        "script2": "command example",
        "script3": "command example",
        "script4": "command example",
        "script5": "command example",
        "script6": "command example",
        "script7": "command example"
    }
    exec(obj[`script${args}`]).then(result => {
        console.log(result.stdout);
    })
}

For configuration in package.json

"scripts": {
   "myscript": "script.sh"[or "script.js"]
}

Then run your script using

npm run myscript 1

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

The function addEventListener is not found

When a button is pressed, I want to add a value into a container by adding an event listener. <div className="grid-container"> <div> <button id="redBet" className="redButton" onclick={thi ...

Leveraging the power of axios.all for dynamic operations

Is there a way to efficiently reuse the same Vue-component for both editing and creating new users? While working with vue-router, I have implemented a beforeRouteEnter method to fetch data via API. Depending on whether an ID is set in the URL parameter, ...

To enhance user experience, it is recommended to reload the page once

Hello, I'm looking for a way to automatically refresh the page after submitting an AJAX form. Currently, I have an onClick function that seems to refresh the page, but I still need to press F5 to see the changes I've made. Here's the JavaSc ...

What is the best way to refresh a navigation bar after making an API request, such as when using Google Sign-In?

Struggling to grasp the hook concept in this particular scenario: The flow goes like this - the user logs in with Google, which updates the session state. Consequently, the visitorType state transitions from 'viewer' to 'buyside'... A ...

Using the clientWidth property in React

While I have a solid background in Javascript, I am relatively new to working with React. In my previous projects where I coded directly in javascript for the browser, I frequently used the following code snippet: width = document.getElementById('elem ...

Tips for verifying login credentials with MongoDB and Express: Leveraging db.collection().findOne() or .find() functions

I am encountering an issue with a POST request involving user credentials being sent from a Login page to the API Server. The code looks like this: loginUser(creds) { //creds is in the form of { username: bob, password: 123 } var request = { ...

Coldbox handler does not receive the data from AJAX call

In my current project, I encountered a strange issue while making an $.ajax call to a Coldbox handler method. When I dump the rc scope at the beginning of the handler, there's no data in it other than my usual additions on each request. Even more biz ...

Retrieve data from the database and automatically populate all text fields when the dropdown value is modified

I need assistance with populating all textbox values based on the dropdown selection. The dropdown values are fetched using an SQL query. Here is the HTML Code: <select name="name" ID="name" class="form-control"> <opt ...

The styling of a CSS class in Internet Explorer may not be applied correctly if there are multiple elements sharing the same class name

For nearly a full week now, I've been plagued by a persistent issue! Here's the situation: I have 6 step tabs - step 1, step 2, and so on. Each tab has a CSS class called "locked" and "active." "Locked" - this style features top: 3em;, causing ...

Attempting to invoke a promise within a function yields an error message stating that it lacks call signatures

Recently, I came across this interesting class: export class ExponentialBackoffUtils { public static retry(promise: Promise<any>, maxRetries: number, onRetry?: Function) { function waitFor(milliseconds: number) { return new Pr ...

Oops! An unexpected field was encountered while trying to use the uploadMultiple function from dropzone.js

I was looking for a way to allow users to select images to accompany their reviews. That's when I came across dropzone.js. However, I encountered an issue when trying to send multiple images in one request. I expected the req.files to contain an arra ...

Unit testing component in Ionic 2 with Ionic's specific markup and elements

Within my Angular 2 component for an Ionic 2 app, I utilize Ionic's markup as shown below: <ion-card> <h3>{{ rawcontent.name }}</h3> <p *ngIf="rawcontent.description">{{ rawcontent.description }}</p> </ion-car ...

Iteratively sift through data for boolean value

My navigation dynamically retrieves routes from the vue-router, eliminating the need for manual addition. Within these routes, there is a boolean key named "inMenu" in both parent and child routes. I have successfully filtered out the parent routes based ...

Struggling to make Tumblr Javascript function properly

I have integrated the following code snippet: <script type="text/javascript" src="http://underlineent.tumblr.com/api/read/json"> </script> Unfortunately, my Tumblr blog is not appearing on this site. Can anyone provide assistance? The provid ...

What causes an asynchronous function to exhibit different behavior when utilized in conjunction with addEventListener versus when manually invoked?

I was delving into the concepts of async and await keywords and decided to create a basic demonstration using an HTML file and a corresponding JS file. In my example, I defined two promises - promise1 and promise2. The handlePromises function uses async/ ...

Is it feasible to incorporate a third-party JavaScript file into a React application?

I have a JavaScript file from a previous MVC project that generates a basic table using ExtJS. Currently, I'm working on developing a new React application with a navigation bar and sidebar. My goal is to explore the possibility of importing the exis ...

"Mongo server is rejecting the connection, and the reason is unclear to me

I created a MongoDB model with the following structure:- var mongoose = require('mongoose'); const itemsModel = new mongoose.Schema({ _id: { type: String, }, userName: { type: String, required: true }, ...

Newbie in JavaScript seeking help with JSON indexing: What could be causing my script using (for ... in) to fail?

Not being a seasoned Javascript coder or very familiar with JSON, my approach may seem naive. Any recommendations for better solutions are appreciated. Below is the code I've written: <!DOCTYPE html> <html> <head> <script& ...

How to retrieve the index of a nested ng-repeat within another ng-repeat loop

On my page, there is an array containing nested arrays that are being displayed using ng-repeat twice. <div ng-repeat="chapter in chapters"> <div ng-repeat="page in chapter.pages"> <p>Title: {{page.title}}</p> </d ...

Exporting functions using reactjs and babel

I'm currently working on a project that involves using reactjs, transpiled by babel with es2015 and react transforms configured in my .babelrc file. In the initial stages of refactoring, I realized that many of the classes should actually be functions ...