npm script hook on multiple scripts

Viewed 825

I've got a reasonably easy question about pre-script hooks.

In my package.json, I've got a couple of commands that all should run the same pre-script. I already have a way to do it, but I'm wondering if there's a more efficient why of doing this.

Current implementation

{
    "scripts": {
        "myproject:run": "npm run custom.prescript && ng serve ...",
        "myproject:prod": "npm run custom.prescript && ng serve -env=prod ...",
        "myproject:build.devserver": "npm run custom.prescript && ng build -env=dev ...",
        ...
        "myproject:build.prodserver": "npm run custom.prescript && ng build -env=prod ...",
        "custom.prescript": "..."
    }
}

This works all fine and dandy, but I'm hoping there's a cleaner way to implement this. As I have about 20 of these scripts that need that same pre-script. When I have to change the command for some reason, I will have to do that 20 times for every project in the repo (about 5 projects).

I know that there's a way to add pre-scripts by creating a script with the same name and the prefix 'pre' to make sure it runs before that specific script, but that doesn't work for multiple scripts as far as I know.

Example of my goal

{
    "scripts": {
        "myproject:run": "ng serve ...",
        "myproject:prod": "ng serve -env=prod ...",
        ...
        "secondproject:run": "ng serve ...",
        "secondproject:prod": "ng serve -env=prod ...",
        ...
        "premyproject:*": "...",  // Runs before all 'myproject:*' scripts
        "presecondproject:*": "..."  // Runs before all 'secondproject:*' scripts
    }
}

Is there a more efficient way to accomplish this, or do I HAVE to do it like I'm doing it already?

Thanks for your time! :)

0 Answers
Related