question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A way to require a chain of targets a-la NPM pre- post- scripts

See original GitHub issue

Disclaimer

If you think this idea isn’t entirely bananas, I’m willing to take my chances and start working on it. Albeit my time is scarce (young kid to raise) and also I don’t know if I’m just good enough 😃

Description

I’d like to have a way, maybe a property in workspace.json - to declare targets that must run before or after the current target.

Something similar to what you can have for NPM scripts, or CI pipelines.

Motivation

I can write about why I need it:

Every app in my monorepo needs an autogenerated file to be present when it builds (both in dev and in prod, whenever it builds). The file is imported, so the build fails if it’s not present. And the values in the file need to be freshly minted every time.

So I added a new target using the @nrwl/workspace:run-commands builder and I run the target every time via a prestart npm script + a step in the CI pipeline.

This is uglier and less robust than it could be.

@ferdiemmen in the community Slack channel suggested I’d do something like this:

"build": {
  "builder": "@nrwl/workspace:run-commands",
  "options": {
    "commands": [
      "nx run app:do-this-first",
      "nx run app:build-app"
    ],
    "parallel": false
  }
},
"do-this-first": {
  "builder": "@nrwl/workspace:run-commands",
  "options": {
    "commands": [
      "node prepare-my-build"
    ]
  }
},
"build-app": {
  "builder": "@angular-builders/custom-webpack:browser",
  ...
} 

This can work, but it’s still a workaround 😃

Suggested Implementation

I imagine this could be declared in workspace.json like this:

"anyTarget": {
  "needs": "myApp:myOtherTarget",
  "builder": "@nrwl/no-really-any-builder-should-have-this",
  "options": {},
  "configurations": {}
}

And whenever I run nx myApp:anyTarget, nx would run myApp:myOtherTarget first.

Alternate Implementations

Future implementations could:

  • Allow for depending on targets from any other app / lib
  • Allow for passing options / parameters to the dep targets

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
boazrymlandcommented, Sep 13, 2021

so indeed dependsOn provides the ‘pre’ part. What about ‘post’ part? Is that going to be handled somehow? (‘runAfter’? 😃 )

5reactions
ChazUKcommented, Mar 7, 2022

It would be nice to have a runAfter so that we don’t have to coddle together a bunch of targets and then a fake build target.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scripts
To create "pre" or "post" scripts for any scripts defined in the "scripts" section of the package.json , simply create another script with...
Read more >
How to Use npm as a Build Tool
npm Scripts. Firstly, we need to figure out how npm can manage our build scripts. As part of npm's core, it has the...
Read more >
javascript - Running NPM scripts sequentially
Invoke these scripts via npm run and chain them with double ampersand && : npm run pre-build && npm run build_logic && npm...
Read more >
npm Scripts: Tips Everyone Should Know
Using Life-Cycle Hooks. Every script in npm runs three separate scripts under the hood. A pre script, a script itself and a post...
Read more >
CoffeeScript
If you'd like to use Node.js' CommonJS to require CoffeeScript files, ... compile method has the signature compile(code, options) where code is a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found