Watch multiple extensions and run different scripts depending on the extension

Viewed 913

I have a Nodejs project with multiple file extensions. I want to run npm run build:copy-assets when any file ending with .ejs was edited, and run npm run start when a .js file was edited.

I'm trying nodemon with following configuration, but the problem is: nodemon invokes npm run start even when I edit the .ejs files.

# nodemon.json
{
  "execMap": {
    "js": "npm run start",
    "ejs": "npm run build:copy-assets"
  }
}
# package.json
...
  "main": "index.js",
  "scripts": {
    "start": "node dist/server.js",
    "watch": "nodemon -V -w src -w dist --ext ejs,js",
    "build:copy-assets": "npx --no-install ts-node tools/copyAssets",
...
# nodemon verbose logs
[nodemon] files triggering change check: src/views/index.ejs
[nodemon] matched rule: /app/src/**/*
[nodemon] changes after filters (before/after): 1/1
[nodemon] restarting due to changes...
[nodemon] src/views/index.ejs

[nodemon] starting `npm run start index.js`
[nodemon] spawning
[nodemon] child pid: 73

How can I run different scripts depending on the file extension ? The solution doesn't have to be using nodemon, but better to use node ecosystem.

0 Answers
Related