最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - One pretask for multiple tasks in package.json - Stack Overflow

programmeradmin2浏览0评论

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 ?

Share Improve this question edited May 11, 2017 at 8:06 Erazihel asked Apr 28, 2017 at 11:51 ErazihelErazihel 7,6056 gold badges32 silver badges56 bronze badges 2
  • 3 The pre and post 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
Add a ment  | 

2 Answers 2

Reset to default 9 +200

I 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);
发布评论

评论列表(0)

  1. 暂无评论