Is it possible to configure watchman triggers in .watchmanconfig?

1.1k views Asked by At

Facebook watchman docs describe how to configure triggers by passing json into the command like so:

watchman --json-command < ./tasks/cmds/watchman-build-trigger.json where watchman-build-trigger.json contains the following:

[
    "trigger", 
    "/Users/michaelprescott/Projects/neuro", 
    {
        "name": "build",
        "expression": [
            "anyof",
            [
                "match",
                "src/*.js",
                "wholename"
            ],
            [
                "match",
                "src/*.ts",
                "wholename"
            ],
            [
                "match",
                "src/*.html",
                "wholename"
            ]
        ],
        "command": [
            "./tasks/cmds/build.sh"
        ]
    }
]

However, I'm trying to understand how to use .watchmanconfig to setup a watch and set of triggers with watchman watch-project I have the following, but triggers aren't created. Is this possible? Does anyone have examples for .watchmanconfig's

{
    "ignore_dirs": [
        "node_modules"
    ],
    "watched": [
        {
            "path": "/Users/michaelprescott/Projects/neuro",
            "triggers": [
                {
                    "command": [
                        "./tasks/cmds/build.sh"
                    ],
                    "expression": [
                        "anyof",
                        [
                            "match",
                            "src/*.js",
                            "wholename"
                        ],
                        [
                            "match",
                            "src/*.ts",
                            "wholename"
                        ],
                        [
                            "match",
                            "src/*.html",
                            "wholename"
                        ]
                    ],
                    "name": "build"
                }
            ]
        }
    ]
}
1

There are 1 answers

1
Wez Furlong On BEST ANSWER

No, it's not possible via .watchmanconfig. The rationale is that, since .watchmanconfig is intended to be checked into a repo that may be cloned by arbitrary folks, we'd rather not allow it to be a vehicle by which arbitrary code could be run without those people explicitly taking an action to run it.

Our recommendation is to wrap up the trigger creation in a script of some kind that you can direct users to run when they would like to enable the triggering behavior.

In addition, I'd urge you to instead take a look at watchman-make for this use case; it has nicer ergonomics than triggers because it is very much easier to see the output from the commands that are being run and to terminate them when you want them to stop.

For example, you could implement your trigger this way instead:

watchman-make -r tasks/cmds/build.sh -p 'src/*.js' -p 'src/*.ts' -p 'src/*.html'

(the single quotes are important!)