Coffeescript and node-supervisor together? - node.js

I'm using coffeescript with --watch option to rebuild javascript on changes to .coffee files.
Is it safe to combine that with node-supervisor to restart Node on changes to the compiled javascript?
I'm worried it won't be robust because of atomicity when coffeescript is recompiling multiple files. node-supervisor could jump the gun and restart Node on detecting the first filesystem change. Is it robust enough to realize there were additional changes while it was busy restarting Node?
Is there a better way? Ideally I'd have only one filesystem watcher recompile my coffeescript and restart node.

Create a JavaScript launcher, i.e. run.js, like this:
require('coffee-script');
require('./launch');
Then run this file with supervisor and appropriate options:
supervisor -e "node|js|coffee" run.js
This worked well for me on Windows.

You can use nodemon, it even has a delay feature (to restart server after a number of seconds have passed), for example:
nodemon --debug ./server.coffee 80
Another good feature of nodemon is ignoring files, example:
# this is my ignore file with a nice comment at the top
/vendor/* # ignore all external submodules
/public/* # static files
./README.md # a specific file
*.css # ignore any CSS files too
Other than that, read the documentation on the github repo and watch this Nodetuts video about nodemon: http://nodetuts.com/tutorials/14-some-nodejs-tools.html

You can use supervisor with -x option set to coffee. This will enable it to run script with right executable:
supervisor -x coffee your-script.coffee
Inspired by Lemming's answer.

In some of my Cakefiles, such as the one for connect-assets, I do the watching myself and simply spawn coffee -co lib src each time something changes, then restart the server when that child process finishes. That gets around the atomicity issue. If every .coffee file changes at once (or if you upgrade the coffee runtime), all of the JS files will update at once as well.

My foreman centric solution looks like this:
Procfile.dev
web: ./node_modules/supervisor/lib/cli-wrapper.js -n exit server.js
watch: ./node_modules/iced-coffee-script/bin/coffee --watch --compile server.iced
and then merely foreman start -f Procfile.dev
Then gitignore the resulting .js files. I like this approach because it keeps a constantly-updating vanilla JS file alongside my .iced files, so I can doublecheck my work as I go (I definitely make mistakes in coffeescript that I might not in vanilla).

Related

node server caching files, not changing anything until close and reopen terminal and server

I am getting same error until the terminal closed and reopen, even after i update my code (logs). Server is caching files. I am using nodemon .
How to run server without caching files.
Nodemon only monitors for the changes only for the current working directory. So please make sure you are running/starting nodemon at the proper location.
Or you can manually add the --watch parameter for desired directories or files in your script line, like:
nodemon --watch app --watch libs app/server.js
Well, I have also used this in multiple applications but didn't get any such caching issue.
Thanks

Restart node when dust template changes

I'm using nodemon to restart node when files change. It works fine when modifying .js files, but doesn't restart when modifying public/templates/index.dust file.
The .nodemonignore file had a /public/* entry, which I removed, but still can't get nodemon to restart on .dust file changes. Ideas?
Nodemon only looks for certain extensions by default. From https://github.com/remy/nodemon#specifying-extension-watch-list
By default, nodemon looks for files with the .js, .coffee, .litcoffee,
and .json extensions. If you use the --exec option and monitor app.py
nodemon will monitor files with the extension of .py. However, you can
specify your own list with the -e (or --ext) switch like so:
So what I have to do is launch nodemon with switch like this: -e js,dust or modify the nodemon.json file's ext property to include dust

Grunt: documentation on how to run these before main program

I want to run my app like normal:
node app.js
but before that happens, I want to ensure that a particular grunt task is run first.
I am having trouble finding documentation on how to automatically run (and complete) a Grunt task before starting a node command.
To be more specific, I have TypeScript files that I want/need to compile before running my node.js application.
Is there a way to always run the compilation (with Grunt or another way) before running node? Perhaps I could do a simple synchronous call in my app.js to run Grunt, but that wouldn't be cool would it?
You could use grunt to automatically compile typescript into a folder. Then you use nodemon in another window which is watching for changes in that folder.
Then when you change the typescript files, grunt runs (if you have set up grunt-watch) and after compile nodemon restarts the application.
You can run any scenario with Grunt. Here, you can use both grunt-ts and grunt-shell tasks to run your program/server.
First one compiles/transpiles your code. Second one allows you to run any shell command, such as node my_file.js.

NodeJS + CoffeeScript Workflow

I have recently started learning nodeJS. Being a long-time coffeescript lover I naturally decided to use it along with node. Writing long-running processes with node I found myself restarting the program frequently. After a quick google I found node-supervisor. Node-supervisor simply watches the current directory for file changes and restarts your app for you automatically.
Before I started using supervisor I was using coffeescript with the --watch option to automatically recompile my coffescripts when they changed.
So the problem is this, supervisor and the coffeescript recompiler don't play nice together.
First I run coffee --compile --watch .
Then in a new terminal I run supervisor app.js
After that supervisor keeps restarting my app forever, even when there has been no changes to the source files.
So the question is this, what is your workflow for working with nodeJS and CoffeeScript?
What you are doing is some kind of redundant.
Here are some hints:
after installing CoffeeScript you have an executable called coffee so you can do (no need to compile your coffee-script files):
coffee yourfile.coffee
how to combine this with supervisor?
if you would have read the Readme on the Github page you would have noticed that supervisor can execute CoffeeScript files, too. All you need to do is:
supervisor yourfile.coffee

How to use less.js with node.js to watch .less files in a folder?

I am looking for a simple guide to setting up server-side watching and compiling .less files in a folder using less.js and node.js. Bryan wrote about it in this post.
Unfortunately, the instructions to Server-side usage on lesscss.org are of a little avail to somebody who is new to node.js.
I have tried command line usage: $ lessc styles.less > styles.css.
I have also tried 'watchr' and 'watch-less' and it all works. However, I am looking for a pure node.js + less.js solution. I am sure there is somebody who will be able to plainly explain how to configure node.js and less.js to watch .less files in a folder. Thanks.
I have created demo project to show how it works. You can find it here https://github.com/corpix/watcherDemo
After clone enter project directory and execute npm install to install less Node.js module.
Try running index.js and change .less file into less_files/ directory, modified file will be compiled and placed into css_files/
Honestly, I have seen a lot of the same threads and answers.
If you are like me and don't want any more code bloat than you already have then just download a LESS app for Win/Mac/Nix and it will auto-compile every time you save your .less files.
I honestly think this would put less of a load on the server because you only need to upload your compiled and minified files at this point.
I wrote something that could be what you are looking for. The script will look for .less files in the watch folder and compile them into the output folder. All you need to do is specify the folder you want to watch and the output folder.
node less-watch-compiler.js FOLDER_TO_WATCH FOLDER_TO_OUTPUT
https://github.com/jonycheung/Dead-Simple-LESS-Watch-Compiler
It skips watching files with names that starts with underscore or a period.
I wrote this for my development process but it should still work on a server. However, you may want to re-consider watching a folder for changes constantly on a live server. It's an expensive operation.
You can use supervisor with -e argument to provide extensions to watch:
nohup supervisor -e 'js|ejs|less' $APP_DIR/app.js 1>$APP_DIR/log/app.log 2>&1 &
echo $! > $APP_DIR/pid/app.pid;
Taken from node-startup script: https://github.com/chovy/node-startup
The simpler version of this command is:
supervisor -e 'js|ejs|less' app.js
I know this is an old post, but came up a few times when I was recently searching for the same thing.
I have forked and updated the code from jonycheung - and have submitted a merge request.
Update: jonycheung has accepted the merge request so his original code is now updated
In the mean time, the code can be found here:
https://github.com/mikestreety/Dead-Simple-LESS-Watch-Compiler
I have also been working on and developing a pure bash script which uses lessc, but does not require a node script to run:
https://github.com/mikestreety/less-watch
Maybe not direct answer. But here is my comment:
Usually we need this watch during the development phase, where we need to change the less file and see the result without the need of re-running the lessc command by every change in less file.
We do this by pure less.js in the in the our html. as below:
<link rel="stylesheet/less" href="/path/to/bootstrap.less">
<script src="/path/to/less.js"></script>
After the finalizing the development task. You need to run lessc only once.
During development your pages may take a liitle more time to load, But if your site is ajax base, and you add this to your template page, it will not be a big deal.
You can use less-watch-compiler to watch for .less files. You don't need a main file to watch just like grunt. Just install this plugin:
npm install -g less-watch-compiler
or
yarn global add less-watch-compiler
Usage:
less-watch-compiler [options] <source_dir> <destination_dir>
Basic example
root
└──src
│ └── main.less
│ └── aux.less
└──dist
└── main.css
The project can be compiled with the following command:
less-watch-compiler src dist main.less
Basic Example 2
less-watch-compiler.config.json
{
"watchFolder": "src",
"outputFolder": "dist",
"mainFile": "main.less"
}
The project can be compiled with the following command:
less-watch-compiler
Source.

Resources