A single pre-task to handle multiple tasks within the package.json file

Currently, I am implementing Terraform for a specific project and I have been assigned two tasks within my package.json file. These tasks involve executing the commands terraform plan and terraform apply.

"scripts": {
    "tf:apply": "terraform apply",
    "tf:plan": "terraform plan"
}

It is essential for both of these commands to be preceded by a terraform get operation. Ideally, I am looking to streamline this process by incorporating just one pretask that can be applied to both.

My initial attempt involved using the following code snippet:

"scripts": {
    "pretf:*": "terraform get",
    "tf:apply": "terraform apply",
    "tf:plan": "terraform plan"
}

Unfortunately, this approach did not yield the desired outcome.

Is there a way to accomplish this utilizing NPM or Yarn exclusively? Or is it inevitable for me to specify the same pretask for each of these tasks separately?

Answer №1

My usual approach is as follows:

"commands": {
    "tf:get": "terraform get",
    "tf:apply": "npm run tf:get && terraform apply",
    "tf:plan": "npm run tf:get && terraform plan"
}

Here is an alternative method that simulates a "tf:*" prehook. This is more suitable for those who are skilled in obscure and cryptic npm techniques:

"commands": {
    "pretf": "terraform get",
    "tf": "terraform",
    "tf:apply": "npm run tf -- apply",
    "tf:plan": "npm run tf -- plan"
}

(Execute it with npm run tf:plan or directly with any argument like npm run tf -- whatever)

Answer №2

Have you attempted to directly manage it using node.js?

You have the option to bind events inside your package.json directly to node scripts. Within these node scripts, you can run your terraform commands along with any common code like so:

var exec = require('child_process').exec;
var cmd = 'terraform apply';

// common code

exec(cmd, function(error, stdout, stderr) {
  // Output from the command will be in stdout
});

Another approach is to use a single node script that accepts a parameter to specify which terraform task to execute. You can define your common code within the script and then run the appropriate command based on the parameter provided:

"scripts": {
    "tf:apply": "node myscript.js --param=apply",
    "tf:plan": "node myscript.js --param=plan"
}

Within the node script, you can access the parameter like this:

console.log(process.argv.param);

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

Saving logs to a file or variable in Ionic using Angularjs

I am currently developing a new project using Ionic (based on AngularJs) and so far everything is functioning as expected. For debugging purposes, I have implemented a method where every 'Function call' (every step) is output to the console via ...

Wordpress website fails to initiate Automate on Scroll (aos) functionality

I seem to be having trouble tackling any task that requires even the smallest amount of brain power. My current struggle is trying to integrate the AOS library into my Wordpress site. In an attempt to make it work, I inserted the following code into my fu ...

Learning the process of accessing a JSON file from a different directory

I am faced with a straightforward folder structure, it looks like this: project │ └───data │ file.json │ └───js test.js My goal is to access the content of file.json within the test.js file in order to perform some ...

The Foundation 6 Zurb Template is not compatible for offline use

After successfully installing Foundation 6 Zurb Template via the cli, I encountered no issues. I then added the missing babel install and everything worked fine online. However, BrowserSync does not seem to work offline. Upon initiating watch, I receive a ...

Removing a Node in VisJs in Real Time

function activateAddEdgeMode(nodeId){ network.addEdgeMode(); } /**looking to implement a method similar to network.addEdgeMode(); that will allow for node deletion. For instance: network.deleteNodeMode()*/ ...

Modify the td attributes while retaining the extracted data values from the website

I'm currently utilizing this code to extract data from another website: $searchURL = "http://www.anotherwebsite.com"; $html = file_get_contents($searchURL); $patternform = '/(<tbody.*<\/tbody>)/sm'; preg_match_all($patternfor ...

Utilizing Command-Line Authentication with B2C Authentication Integration (Distinct Module)

Currently, I'm in the process of developing a React application with B2C authentication functionality. My objective is to develop a distinct package that can handle command-line authentication for the primary application and preserve the token acquire ...

A critical issue occurred: array length is invalid. The attempt to include EJS in the template failed due to

Currently, I am attempting to loop through my JSON data using EJS from Node/Express. However, I need to insert a different pin into the flexbox when it reaches the 6th iteration. Whenever I try to implement this logic, I encounter a severe error that disr ...

Issue with Semantic-UI Special PopUp not displaying on screen

I'm currently working on creating a unique pop-up feature using custom HTML, with the intention of adding content to it later on. My console is displaying the following message: Popup: No visible position could be found for the popup. $(document) ...

Output the keycode to the console repeatedly

I'm currently facing a minor mental obstacle: I have a javascript function embedded in html that displays the keycode when a key is pressed. It's connected to a function that provides detailed information about the character and keycode being pre ...

What methods can I use to gauge the loading time of an AJAX request and showcase a loading panel?

I am facing an issue with my AJAX request that sometimes deals with a large JSON object. I need to display a loading panel during this process, similar to the one shown in the image below (scaled at 45%): https://i.stack.imgur.com/rw9ft.jpg The problem I ...

Implementing AJAX to dynamically insert content into div elements on the page

Currently facing a small issue with my AJAX implementation for creating comments on posts. The functionality is working well, but the problem arises when executing it in the index.html.erb view. The create.js.erb file locates the initial div labeled "comme ...

Exploring the controller logic in Sails.js index.ejs

I'm struggling to understand how to integrate dynamic logic into the homepage of my Sails.js application. Currently, the homepage is static, but I want to display data on the index.ejs page. I have a MainController with an index function that retrieve ...

Guide on displaying the value of an element in a Vue modal

I am working with a list of items displayed as <li> elements in a loop. When one of these elements is clicked, I would like a modal box to open up displaying specific content related to that particular element. The following data represents the item ...

Is it feasible to gather a list of private npm packages along with their versions using the npm API?

We currently have a private npm registry set up on npmjs.org that houses numerous private npm packages. However, we are transitioning to an in-house private npm registry using verdaccio. Due to the nature of our AWS infrastructure, there is a possibility t ...

What is the best way to utilize MUI breakpoints for displaying images based on different screen sizes?

I need help displaying an image based on the screen size using MUI breakpoints. I'm struggling to understand how to implement this with MUI. Can someone assist me with the breakpoints? interface EmptyStateProps { title: string; description: string ...

Working with JSON Requests using Python's GET and POST Methods

Currently immersed in the realm of automating dating applications, specifically focusing on a French app called Fruitz. I have successfully reverse engineered the API to obtain the necessary POST and GET sequences. Here is the function I'm currently ...

Downloading Files from Mongodb Using GridFS

I am working on an application that enables users to upload and download various files. Currently, I am facing a challenge where I am able to retrieve the file from my MongoDB database and download it locally on my machine, but I am encountering difficulti ...

The anchor tag causes the tooltip to display outside of the td element

One of my requirements is to display the click percentage for each link in an HTML template that is dynamically fetched from a database. I attempted to show the click percentage for each anchor link by adding them to the `data-title` attribute using jQuery ...

Navigating through pages in a server component using Next.js

I am currently working on a project that involves implementing pagination using the NextJS 13 server component without relying on the use client. The goal is to ensure that when a button is clicked, new entries are added to the screen in a sequential order ...