Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pre- and post- 'lifecycle' scripts in targets #9054

Closed
1 task done
pauleustice opened this issue Feb 21, 2022 · 3 comments
Closed
1 task done

Support pre- and post- 'lifecycle' scripts in targets #9054

pauleustice opened this issue Feb 21, 2022 · 3 comments

Comments

@pauleustice
Copy link
Contributor

pauleustice commented Feb 21, 2022

Description

I would like to see Nx support pre- and post- steps on project.json / workspace.json targets.

Motivation

When moving to Nx, we removed a lot of npm scripts that called ng build and replaced them with direct use of nx build. Sometimes, however, you need to always run a post-build step which with npm is achieved with post scripts, e.g.:

{
  "build:app1": "ng build app1",
  "postbuild:app1": " node postbuild.js",
}

The only alternative that I'm aware of with Nx is to do this:

"build": {
  "executor": "@nrwl/workspace:run-commands",
  "options": {
    "cwd": "./",
    "commands": [
      "nx build-app app1 -c {args.c}",
      "node postbuild.js"
    ],
    "parallel": false
  }
},
"build-app": {
  "executor": "@angular-devkit/build-angular:browser",
   etc...
}

This causes a problem: in order to pass the build configuration, one would have to call nx build app1 --c test (note the double dash). However, for other apps that don't need additional build steps, this would be nx build app2 -c test (note the single dash).

This is especially troublesome in generic CI workflows, as a single command cannot be used to build all apps.

Suggested Implementation

One possible solution could be to utilise the existing run-commands executor for new pre- and post- 'lifecycle' scripts on any target. These could be defined as such:

"build": {
  "executor": "@angular-devkit/build-angular:browser",
  "pre-run": {
    "executor": "@nrwl/workspace:run-commands",
    "options": {
      "cwd": "./",
      "commands": [
        "node prebuild.js",
        "node someOtherPreBuildStep.js"
      ],
      "parallel": false
    }
  },
  "post-run": {
    "executor": "@nrwl/workspace:run-commands",
    "options": {
      "cwd": "./",
      "commands": [
        "node postbuild.js",
        "node someOtherPostBuildStep.js"
      ],
      "parallel": false
    }
  },
  "options": {
     etc...
   }
}

The executor could be implied for terseness, removing the need for that line.

Alternate Implementations

Alternatively, instead of nesting the 'lifecycle' scripts in the targets, Nx could mimic the functionality of npm scripts:

"pre-build": {
  "executor": "@nrwl/workspace:run-commands",
  "options": {
    "cwd": "./",
    "commands": [
      "node prebuild.js",
      "node someOtherPreBuildStep.js"
    ],
    "parallel": false
  }
},
"build": {
  "executor": "@angular-devkit/build-angular:browser",
  "options": {
     etc...
   }
},
"post-build": {
  "executor": "@nrwl/workspace:run-commands",
  "options": {
    "cwd": "./",
    "commands": [
      "node postbuild.js",
      "node someOtherPostBuildStep.js"
    ],
    "parallel": false
  }
}

Note, the key / name of the target is important, the order within the file is not.

@ChazUK
Copy link

ChazUK commented Mar 8, 2022

After reading through some of the other requests for a pre and post build mechanic I don't think that the core team are going to implement one due the addition of dependsOn, which is a great tool but can require quite a bit of rejigging to work in the 'post' world.

I've come across an instance where changing the name of the build target is causing the task to fail, which would definitely be fixed with some kind of requiresAfter functionality to mimic dependsOn.

Changing this target name to anything other than build results in a build errors, which are below.

{
  ...
  "targets": {
    "build": {
      "executor": "@nrwl/angular:package",
      "dependsOn": [
        {
          "target": "generate-docs",
          "projects": "self"
        },
        {
          "target": "build",
          "projects": "dependencies"
        }
      ],
      "outputs": [
        "dist/libs/eds-base"
      ],
      "options": {
        "project": "libs/eds-base/ng-package.json"
      },
      "configurations": {
        "production": {
          "tsConfig": "libs/eds-base/tsconfig.lib.prod.json"
        },
        "development": {
          "tsConfig": "libs/eds-base/tsconfig.lib.json"
        }
      },
      "defaultConfiguration": "production"
    },
  },
  "implicitDependencies": [
    "eds-icons"
  ]
}
 libs/eds-base/src/lib/organisms/feature-list/feature-list.module.ts:6:57
    6 import { edsIconCheckSquare, edsIconRemoveSquare } from '@eds/icons';
                                                              ~~~~~~~~~~~~
    File is included via import here.

Hopefully bumping this will get a few more eyes on this feature request.

@FrozenPandaz
Copy link
Collaborator

FrozenPandaz commented Mar 14, 2022

Correct, via dependsOn you can more directly express what should actually be done before instead of renaming things. I would recommend it over defining a different target with @nrwl/workspace:run-commands.

build dependsOn codegen
codegen

Is more direct than

build
prebuild = codegen
codegen

Similarly, postbuild could be a separate target which depends on build.

postbuild: {
  dependsOn: [
    { projects: 'self', target: 'build' }
  ]
}

I've come across an instance where changing the name of the build target is causing the task to fail, which would definitely be fixed with some kind of requiresAfter functionality to mimic dependsOn.

The target name should not be required to be build or any particular name. Could you please open an issue with the case that you saw?

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants