Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watch, recompile, and restart

I have been looking all over for an example of a way to use Grunt's watch module to perform a few steps in order when a file change occurs. I haven't found a good example of any of this, so if someone could point me in the right direction that would be great.

  1. Build TypeScript project (I have this working)
  2. Watch directories for file changes (this works too)
  3. Start running the compiled JavaScript in a node process, while still watching for file changes (what's the best way to do this via Grunt? The watch module seems to kick off the recompile task OK)
  4. On file change, stop the other running process, recompile, and restart when finished. Keep watching for changes (No idea on this one - the restart is the tricky part!)

I've tried a few different ways such as starting a child process with Grunt, but I always end up with dangling processes, locked up ports, misdirected STDIO, or other issues. I'd like for the child processes to be killed if the Grunt process exits.

Is there a good way to handle this? Thanks!

like image 887
jocull Avatar asked Nov 04 '15 01:11

jocull


People also ask

Does Nodemon working with TypeScript?

As of v1. 19.0, nodemon has inbuilt support for TypeScript files with help from ts-node that requires no manual configuration. By default, nodemon uses the node CLI as an execution program for running JavaScript files; for TypeScript files, nodemon uses ts-node as the execution program instead.

What nodemon do?

nodemon is a tool that helps develop Node. js based applications by automatically restarting the node application when file changes in the directory are detected. nodemon does not require any additional changes to your code or method of development. nodemon is a replacement wrapper for node .

What is TS node Dev?

What is ts-node-dev? Compiles your TS app and restarts when files are modified. Compiles your TS app and restarts when files are modified.


2 Answers

My simple implement via config of package.json using nodemon to watch src for ts file changes and compiling typescript/ts files with tsc...

"scripts" section from package.json:

  "scripts": {
     "debug": "nodemon -e js,ts --watch src --exec \"yarn run build:ts && yarn run start:app\"",
     "build:ts": "node_modules/.bin/tsc",
     "start:app": "node dist/app"
  },

tsconfig.json

{
   "compilerOptions": {
     "target": "es6",
     "module": "commonjs",
     "outDir": "dist"
   },
   "include": [
     "src/**/*.ts"
   ],
   "exclude": [
     "node_modules"
   ]
}
like image 153
Adam Cox Avatar answered Sep 24 '22 18:09

Adam Cox


Here's my solution. I have a nodemon which watches the src folder and triggers a build cycle + node call whenever it sees changes. It is called using npm run dev:src. It is a pretty simple solution:

package.json

"scripts": {
  ...
  "build:dev": "npm run clean && npm run compile:dev",
  "clean": "rimraf dist",
  "compile": "babel src -d dist && npm run copy:static",
  "compile:dev": "babel src -d dist -s && npm run copy:static",
  "copy:static": "cp -R src/static dist/static",
  "dev:dist": "npm run build:dev && node --inspect dist/server.js",
  "dev:src": "npm run build:dev && nodemon --watch src --exec npm run dev:dist",
}

EDIT:

This is an archaic choice of technology. I know this may be out of scope, but I'd suggest to either for a webpack/rollup approach like this:

"scripts": {
    "start": "node build/index.js",
    "build": "webpack",
    "dev": "cross-env NODE_ENV=development nodemon --watch src --watch package.* --watch .env --watch .env.development --watch .env.development.local --watch webpack.config.js --exec \"npm run build && node build/index.js\"",
    "lint": "eslint ./src --ext .js && prettier --check ./src/**/*{.js,.mjs}",
    "lint:fix": "eslint ./src --ext .js --fix && prettier --write ./src/**/*{.js,.mjs}",
  },

or consider strongly Kent C. Dodds' approach:

"scripts": {
    "start": "node .",
    "build": "babel --delete-dir-on-start --out-dir dist --copy-files --ignore \"**/__tests__/**,**/__mocks__/**\" --no-copy-ignored src"
  }

https://kentcdodds.com/blog/how-i-structure-express-apps

like image 45
Philippe Hebert Avatar answered Sep 22 '22 18:09

Philippe Hebert