Restart node when dust template changes - node.js

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

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

How to watch .sss files and re-run my npm script if they change

Is there a way to watch my .sss files and re-run this if they change?
"css": "postcss -u autoprefixer -u postcss-easy-import -p sugarss src/index.sss -o src/index.css"
I can't find a way, I've looked on the net all over. I don't see any kind of watch available from postcss. There's a -w or --watch with the postcss-cli but I tried adding that to the end of the script above. Unless I'm just doing it wrong, my css script does not re-run itself if my .sss files change.
maybe there's some generic node watcher lib I can use?
nodemon is a very powerful tool to re-run your scripts based on any change in your files , default file extension for nodemon to watch is .js and default script to run from your package.json file is start script.
You can overwrite watched extension using -e option , then extensions separated by , for example -e sss in your case.
Also you can overwrite the script you want to run using --exec option and then pass the script you want to run , in your case it will be --exec "npm run css"
And put all together nodemon -e sss --exec "npm run css" , this will watch changes in sss files and run css script

Watch multiple css files with postcss and output a bundle.css

I'm trying to figure out a workflow involving postcss. My need is to have css partials in one folder, watch them and output a bundle css file. The bundle css file must have include a base.css file at the top.
postcss has a --watch flag and can watch multiple files but can only output multiple files and not a bundle css file. I can use cat to combine all the files and use stdin to pipe them to postcss. But I can't trigger the cat command from postcss.
My files structure could look like this:
partials/
|_one.css
|_two.css
styles/
|_base.css
|_bundle.css
How would I, by using npm, arrange my script section to use CLI commands to achieve my goal?
My main issue is to figure out how to orchestra the build steps without one step blocking the next. A link to a working work-flow example would be great!
EDIT I got a solution working but it is very suboptimal as it can not be used with sourcemaps, can not have global variables and is a two step process. But I will outline it here in hope that someone can suggest a better approach.
Using the following structure:
build/
|_stylesheet/
|_default.css (final processed css)
partials/
|_one.css
|_one.htm (html snippet example)
|_two.css
|_two.htm (html snippet example)
styles/
|_base.css
|_bundle/ (css files from partial/ that is partly processed)
|_one.css
|_two.css
|_master.css (import rules)
I have a two step process in my package.json. First I make sure I have a styles/bundle folder (mkdir -p) and then I start nodemon to watch the css files in partials/. When a change occurs, nodemon runs npm run css:build.
css:build process the css files in partials/ and output them in styles/bundle/ (remember that I don't know how to watch multiple files and output one bundled file).
After running css:build, npm runs postcss:build which processes the master.css file that #import css files from styles/bundle/. I then output (>) the processed content from stdout to build/stylesheet/default.css.
{
"css": "mkdir -p styles/bundle && nodemon -I --ext css --watch partials/ --exec 'npm run css:build'",
"css:build": "postcss --use lost --use postcss-cssnext --dir styles/bundle/ partials/*.css",
"postcss:build": "postcss --use postcss-import --use postcss-cssnext --use cssnano styles/master.css > build/stylesheet/default.css",
}
You can check out watch and parallelshell packages from npm. I believe the combo of those two will do the trick. More on that here: http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/
Maybe you should consider using webpack instead to build a single css file and other resources which also has a watch flag. It is very efficient and you don't need to manually rebuild all the time after changes to resources/files.

Does watch work with Jade?

I want to watch my jade files and compile them, but it appears it does not work
jade client/jade --watch --out public
When I change client/jade/draft.jade it does not re-compile. I saw a post, saying it does not work with directories? Is it not fixed yet? Is there a workaround or something? Must I watch all files manually?
https://groups.google.com/d/msg/jadejs/p_slRuISjVg/lL_uxgD6uB0J
What you have now says "watch the directory for changes". If you add or remove a file from the directory, it will recompile all the files. However, because it is only watching the directory (which is really just a file itself) and not the files in the directory, changing one of the files will not trigger a compile.
To watch all jade files in client/jade
$ jade --watch client/jade/*.jade --out public
This doesn't truly watch the directory, it just expands the jade command to watch all jade files present when you start, rather than having to type them all out. If you add a file after beginning the watch, then you'll need to restart it.

Coffeescript and node-supervisor together?

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).

Resources