Code Monkey home page Code Monkey logo

node-watch-compile's Introduction

node-watch-compile

Note

Only tested under linux.Nodejs's fs.watch is used to watch file changes,but these API is not gareteed in all platform.The availability can be found at http://nodejs.org/api/fs.html#fs_availability

An watch-compile tool for nodejs.Useful for webdev real-time compilation for less/coffee or what ever.

Install

sudo npm install -g watch-compile

Usage

# watchcompile or nwc
# create a template Watchfile
watchcompile -c
# Run it
watchcompile -s
# Or run with custom Watchfile
# watchcompile -f /path/to/Watchfile.server

-f is used to specify the Watchfile which contained watch rules. default is "./Watchfile" -s make watchcompile run initial compilation for all matched files.

#Watchfile

#create an default Watchfile at ./
watchcompile -c

A default Watchfile is like below

//{basename} /css/style.less => style.less
//{fullpath} /css/style.less => /css/style.less (unchanged)
//{filename} /css/style.less => style
//{extname}  /css/style.less => .less
//{directory} /css/style.less => /css/
exports.watchList = [
    // [testFunction,commandToRun]
    // [RegExp|(path:string)=>boolean,string]
    [/^.*\.coffee$/,'coffee -c {fullpath}'],
    [/^.*\.less$/,'lessc {fullpath} > {directory}{filename}.css'],
];

exports.serviceList = [
    //commandToRunOnceAtStart
    "echo WatchCompileStart",
    "echo 'start tsc watch' && tsc -p ./ --watch"
]

Watchfile's working directory is always based on pwd of your shell.

Watchfile is considered as an standard node module and latter running by require("vm").runInContext.

Consider the folder structure

/Watchfile  #this one is the example
/index.html
/js/code.coffee
/css/style.less

When running watchcompile at /

/js/code.coffee is matched by [/^.*coffee$/,"coffee -c {fullpath}"]
/css/style.less is matched by [/^.*less$/,"lessc {fullpath} > {directory}{basename}.css"]
]

Note that the RegExp can also be replaced by a function with signature (path:string)=>boolean.

When /css/style.less changed."lessc /css/style.less > /css/style.css" is excuted.

Supported macros are :

{basename} /css/style.less => style.less
{fullpath} /css/style.less => /css/style.less (unchanged)
{filename} /css/style.less => style
{extname}  /css/style.less => .less
{directory} /css/style.less => /css/

Since Watchfile is considered and excuted as node module. So you can do what ever you can with javascript to generate any exports.watchList you want.

ServiceList

Some compilation has it's own watch system and some of them, such as typescript, may have incremental compilation which lead to a huge performance boost than normal compilation. So we provide a exports.serviceList to run it at start of watchcompile. Also you can run any other command as you wish as a service. It is actually just a command run at start of the watchcompile as a child process.

Behavior

  1. Only one task is running at any moment.
  2. When multiple task with exactly the same command is triggered, they get merged, unless one of them is currently running, then we will queue it.

Notification

Watchcompile it self don't provide notification, but you may write it yourself using command like:

exports.watchList = [
    ["/*\.ts/","tsc -p ./ && notify-send '{fullpath} compile success' || notify-send 'something is wrong with {fullpath}'"]
]

notify-send is a desktop notification service provide by most desktop environment of Linux. OSX user may use applescript instead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.