Restart Heroku local on file change?

喜你入骨 提交于 2019-12-03 10:23:37

The easiest way to do this is to run nodemon with heroku local as the executable - i.e. nodemon --exec "heroku local".

However, heroku local exits with a non-zero exit code for the default nodemon shutdown signal (SIGUSR2), so you need to add an additional flag to nodemon to set the interrupt signal to SIGTERM.

nodemon --exec "heroku local" --signal SIGTERM

(tested with heroku-cli@6.14.31-33a2d0a, nodemon@1.12.1, node@8.5.0)

heroku local just uses node-foreman (https://www.npmjs.com/package/heroku-local), so it is easier to use that directly for watching.

First, install foreman and nodemon:

npm i --save-dev foreman nodemon

Now, you need to set up two scripts in your package.json:

{
  ...
  "scripts": {
    "start": "nf start",
    "watch": "nodemon --watch directory-to-watch"
  },
  ...
}

You can now run the app while it watches that directory and reloads on changes with

npm run watch

I think that heroku local will watch for changes to static resources (client-side code). But, it's clearly not ideal to manually restart your server with each source code change you make to the web server (server-side code).

If you're creating a NodeJS application, my suggestion is to try the watch command that Gulp provides. However, that also requires you to (a) install Gulp, and (b) write the Gulp script. Again this solution would only work for NodeJS, and these steps wouldn't be needed if heroku local watched the files for you.

  1. Install NodeJS
  2. Install Gulp

    npm install gulp --save-dev
    
  3. Add the Gulp script (where Procfile lives)

    NOTE: Haven't been able to get the following watch code to work successfully, but it might provide a template for others to create a working solution.

    var gulp = require('gulp');
    var exec = require('child_process').exec;
    
    gulp.task('heroku-local', function () {
      exec("heroku local");
    });
    
    gulp.task('heroku-local:watch', function () {
      gulp.watch([
        'file-to-watch',
        'folder-to-watch/**/*'
      ], ['heroku-local']);
    });
    
  4. Run the Gulp script (from directory where Procfile lives)

     gulp heroku-local:watch
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!