Nx: Replacing build with run-commands builder and getting access to configuration "--prod" ?

Created on 27 Dec 2019  路  3Comments  路  Source: nrwl/nx

  • [x] I am running the latest version
  • [x] I checked the documentation (nx.dev) and found no answer
  • [x] I checked to make sure that this issue has not already been filed
  • [x] I'm reporting the issue to the correct repository (not related to React, Angular or any dependency)

Expected Behavior

I have managed to replace my SERVE with a custom run-command and it's working great apart from some formatting issues.

What I would like to do is replace the build command and get access to the config that is passed by the NG CLI.

Example

ng build --prod

So I would like to be able to pick up the configuration value - which should be set to "production" right?

I know we can pass args using args= which is explained here

https://nx.dev/web/api/workspace/builders/run-commands

but how do we pick up the production config, it would be really great to be able to do this, actually get access to ANY arguments sent in - in a script file

here is what I have

      "architect": {
        "build": {
          "builder": "@nrwl/workspace:run-commands",
          "options": {
            "commands": [
              {
                "command": "./scripts/execute.sh"
              }
            ]
          }
        },

So in my execute.sh file, i would love to be able to get access to any arguments sent in.

Actually the SH file can be a node file, i don't mind - just as long as I can pick up the arguments sent in via NG CLI i.e. ng buld --prod

The above would allow custom builds without leaving the comfort of ng build.

Thanks in advance.

Most helpful comment

the page at https://nx.dev/workspace/plugins_workspace_builders/run-commands has several javascript errors and displays no content

All 3 comments

While not ideal, you can do something like following:

dummy script

// @ts-check
const args = process.argv.slice(2);

main();

function main() {
  console.log(args);
}

workspace architect config

you can interpolate arguments within your config

{
  "commands": [
    {
      "command": "node ./tools/scripts/execute.js --prod={args.prod}"
    }
  ]
}

now you can run

nx run your-project:build --args='--prod=true'

which will console log [ '--prod=true' ]

Now you need to implement arguments parser on your own... but this API is not very nice DX.

Wondering if allowing standard argument passing would be such a problem ? cc @jaysoo

I guess so, as that would basically force to run all command within commands array, via CLI provided args, which might cause various unpredicted issues

Thanks @Hotell , I will give it a shot.

the page at https://nx.dev/workspace/plugins_workspace_builders/run-commands has several javascript errors and displays no content

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasedwards picture jasedwards  路  3Comments

dereklin picture dereklin  路  3Comments

joelmuskwe picture joelmuskwe  路  3Comments

about-code picture about-code  路  3Comments

zpydee picture zpydee  路  3Comments