I am using Terraform for a project and I got two tasks in my package.json
to launch terraform plan
and terraform apply
.
"scripts": {
"tf:apply": "terraform apply",
"tf:plan": "terraform plan"
}
For both of these mands, I need to perform a terraform get
first. I would like to have just one pretask
for both of them.
I tried to use:
"scripts": {
"pretf:*": "terraform get",
"tf:apply": "terraform apply",
"tf:plan": "terraform plan"
}
But it doesn't work.
Is there any way to achieve this using NPM
or Yarn
only ? Or am I forced to write the exact same pretask for both of these tasks ?
I am using Terraform for a project and I got two tasks in my package.json
to launch terraform plan
and terraform apply
.
"scripts": {
"tf:apply": "terraform apply",
"tf:plan": "terraform plan"
}
For both of these mands, I need to perform a terraform get
first. I would like to have just one pretask
for both of them.
I tried to use:
"scripts": {
"pretf:*": "terraform get",
"tf:apply": "terraform apply",
"tf:plan": "terraform plan"
}
But it doesn't work.
Is there any way to achieve this using NPM
or Yarn
only ? Or am I forced to write the exact same pretask for both of these tasks ?
-
3
The
pre
andpost
are added only if the mand match, there is no regex unfortunately: github./npm/npm/blob/… – rpadovani Commented Apr 28, 2017 at 12:33 - have one concatenating with ";" your mands? – Santiago Rebella Commented May 14, 2017 at 7:56
2 Answers
Reset to default 9 +200I usually go like this:
"scripts": {
"tf:get": "terraform get",
"tf:apply": "npm run tf:get && terraform apply",
"tf:plan": "npm run tf:get && terraform plan"
}
This is another option which fakes a sort of "tf:*"
prehook. Only for obscure cryptic npm
ninjas and not reended:
"scripts": {
"pretf": "terraform get",
"tf": "terraform",
"tf:apply": "npm run tf -- apply",
"tf:plan": "npm run tf -- plan"
}
(Use it with npm run tf:plan
or directly with any argument npm run tf -- whathever
)
Have you tried to manage it directly using node?
You can bind the events inside your package.json directly to node scripts and inside the node scripts you can execute your terraform mands and your mon code in this way:
var exec = require('child_process').exec;
var cmd = 'terraform apply';
// mon code
exec(cmd, function(error, stdout, stderr) {
// mand output is in stdout
});
You could also just use one single node script accepting a parameter to specify which terraform task to execute, define your mon code inside the script, and then execute the right mand depending on the parameter:
"scripts": {
"tf:apply": "node myscript.js --param=apply",
"tf:plan": "node myscript.js --param=plan"
}
Then inside node you can access your param in this way:
console.log(process.argv.param);