nrwl-nxnrwlnx.dev

How to forward command into run-commands using NX CLI?


Given I have entries in angular.json that define various commands using the serverless CLI (as an example)

"serverless-deploy": {
  "builder": "@nrwl/workspace:run-commands",
  "options": {
    "command": "npx serverless deploy",
    "cwd": "apps/my-app"
  }
},
"serverless-remove": {
  "builder": "@nrwl/workspace:run-commands",
  "options": {
    "command": "npx serverless remove",
    "cwd": "apps/my-app"
  }
}

How can I improve this by reducing duplication and combining it into one entry so that I can forward the command in?

E.G something like this:

"sls": {
  "builder": "@nrwl/workspace:run-commands",
  "options": {
    "command": "npx serverless",
    "cwd": "apps/my-app"
  }
}

And then call it with nx run my-app:sls MYCOMMAND (E.G 'deploy' or 'remove')? Unfortunately the above doesn't work but would love to know if this is possible.


Solution

  • "sls": {
      "builder": "@nrwl/workspace:run-commands",
      "options": {
        "commands": [
          "npx sls {args.cmd}"
        ],
        "cwd": "apps/api",
        "parallel": false
      }
    },
    
    nx run api:sls --cmd=deploy