recipe-js - A gulp/GNU make like task launcher with CLI tool.Supports Dependencies/Cache/Inference Rules/Promise/Child Process/Deriving/Scheduler

A gulp/GNU make like task launcher with CLI tool.Supports Dependencies/Cache/Inference Rules/Promise/Child Process/Deriving/Scheduler


Core module (RecipeJs class)


$=new RecipeJs();

    return(`${g.prereq0} ${g.prereq1}`);


    return new Promise((rs,rv)=>{


//-> Hello World
//$.R(targetName,[prerequisite,prerequisite...],callback) # declares "recipe" of "targetName"
//$.set(target,value) # declares target is value
//$.make(targetName) # make target
//$.remake(takgetName) # clear cache then re-make target

recipe-js is GNU make like task launcher. unlike normal program sequence, recipe-js 'resolves' prerequisites of the target then gathers these task's results automatically.

you can 'declare' task by $.R function or $.set functtion like above example. $.R are function tasks which getting their prerequisites vir argument('g' above) then return result as 'targetName'

'g' argument is just an object which has attributes. the name of attributes is same as the prerequisite name.

.set() is just assigning a value to the target name, unlike .R, that has no function task.

.make() returns promise() with result of target task. so above example means like : 'default' needs 'prereq0' and 'prereq1', 'prereq0' is hello, 'prereq1' will be World after 1 sec later. so 'default' is string hello + World. .make() gets this result and print at .then().

recipe-js caches each result of tasks in memory. so same task will never be called if a prerequisite are used 2 or more times.

you can call .remake(targetName) instead of .make(), the internal cache is cleared for refreshing results or need side-effect of task again.

Nodejs module (RecipeNodeJs class extended from RecipeJs)


$=new RecipeNodeJs();

    console.log(`${g.prereq0} ${g.prereq1}`);

$.R('prereq0',$.P("echo -n Hello"));

    return $.S('whoami')
    .then($.P('sed s/^/Mr./'));


//-> Hello Mr.username
//$.S(cmd) execute shell command as promise. able to inject stdin(as string) via 2nd argument
//$.P(cmd) is short hand for (stdin)=>$.S(cmd,stdin)

There are 2 classes are exported recipe-js module. RecipeJs class and RecipeNodeJs class. RecipeJS class doesn't access NodeJs API such as FileIO(fs) or child_process.

RecipeNodeJs class os derived from Recipe.Js class. this class has Nodejs feature. so basically, you should use RecipeNodeJs class.

.S() and .P() are process execution tasks. you can easy to write and receive external tool's results such as curl without having to write complex child_process programs.

you can pass data into stdin by a 2nd argument of .S(). off course you can use pipe | inside command line string. execution is asynchronous.

Inference Rules


$=new RecipeJs(); 

    return g.replace(/^## (.*)/,'<h2>$1</h2>')

$.set('','## Hello');
$.set('','## RecipeJs');


//-> <h2>Hello</h2><h2>RecipeJs</h2>
//% is wildcard, regex is ok like '(.*)\.html','$'

like GNU make. recipe-js have inference Rules. you can declare .R() which have wildcard (%) or regex.

you can know actual target and prerequisites in recipes by 2nd argument of .R() callback like below.

    console.log(`actual target is '${}'`);
    console.log(`actual prerequisites are ${info.deps}`);

    return request_promise({url:g[0]),headers:{'User-Agent':g[1]}});
//-> actual target is 'html_google'
//   actual prerequisites are ['url_google','useragent']'

File IO(auto load/save)


$=new RecipeNodeJs(); 

// $.F tells that specified targets(extension/filename) are files.



//-> file Hello) -> file prereq0.html (<h1>Hello</h1>) 
//file system's timestamp is used for update decision

.F() marks the target name as "file name". recipe-js automatically read the file even if there is no recipe for this name.

if marked target name is prerequisites or make() target, the file is generated and written by passed data.

target name for $.F can have wild card(%) or regex(automatically enclosed by ^$ e.g (.).jpg will be ^(.).jpg$ ). 2nd argument is file encoding, 'utf8' or 'binary'.

Cache / Trace / Debug


$=new RecipeNodeJs({

    console.log(`Hello ${g}`);

    return $.S('whoami')
        return $.cache('prereq0',r,180); //cache time:180sec,null is forever


//-> MyCache.json -> '{"prereq0":{"v":"user\n","expire":1499307137335}}'
//if you run this program again, 'whoami' will not be called till 180 sec later.

you can specify these options to constructor to see verbose output.

results which enclosed by .cache() is saved to json file which specified in cacheFile option.

the cache will automatically be loaded at next execute then use the cached value instead of execute $.R till expired time.

specifying 'cacheId' instead of 'cacheFile' makes JSON file at ~/.recipe-js/.json . ('cacheFile' is higher priority than 'cacheId'.)

a cache file is synced at end of each make() (including internal make for prerequisites)

$=new RecipeNodeJs({

    $.cache('prereq0',`count:${count++}`,2); // 2sec


    // $.clearCache();  <- if you remove // , then result will be 0,1,2

//  count:0 (from Cache)
//  count:1

.remake(target) method clears memory cache only. so tasks which returns .cache() are never called till expired time evenif .remake() call.

if you want to clear file cache, set 'clearCache:true' option at constructor. or call .clearCache() dynamically(never call this method inside task).

you can .clearCache() for individual object like .clearCache('prereq0') . if omit, all cache data will be removed.

Deriving (extends)


parent=new RecipeJs(); 

    console.log(`Hello ${g}`);

child=new RecipeJs({


//-> Hello World ('prereq0' would be stored in 'child' object)

Deriving is useful when re-using common recipes. just specifying extends attribute to createing new RecipeJs class. The class will be child recipe.

if there are no recipe($.R) in child RecipeJs. parent $.R will be used.(override)

Deriving (parent)


parent=new RecipeJs();

    return new Promise((ok)=>{

child1=new RecipeJs({
    parent:parent    //<-test left 'parent' to 'extends'

    console.log(`Child1 ${g}`);

child2=new RecipeJs({
    parent:parent    //<-test left 'parent' to 'extends'

    console.log(`Child2 ${g}`);

//-> Child1 World (10sec after start)
//-> Child2 World (10sec after start because parent 'prereq0' result are re-used in parent object)
//   if you change like above comment, it will be 20 sec

unlike 'extends' deriving, 'parent' deriving is for sharing results by children.

this means that result objects are stored in parent object, not child object itself. so the object is able to access other child classes.

recipe-js caches each result of tasks in memory even if cacheFile option is not specified. so the same task will be never called till .remake(target)

Command line parser

$=new RecipeNodeJs();

    console.log(`${g.prereq0} ${g.prereq1} ${g.prereq2}`);

$.set('prereq0','-');  //defaults

    b:'prereq0:',  //':' indicates has arg


//command.js -b Hello -c --prereq2=World
//->Hello true World

.setByArgv() is utility to set prerequisites by command line arguments.

recipe-js knows which relation between -option and target names and whether option arg e.g.(-b hello) are necessary or not.

--prereq=2=world style options sets target values directly.


$=new RecipeJs({
    traceScheduler:true  //show time tick on debug out

    console.log("12 o'clock");

    console.log("10sec passed");

    console.log("60sec(one shot)");
    $.clearSchedule('scheduleid2); //turn of 10sec timer

$.schedule("12:00",'timer1');               //12:00 every day
$.schedule("10",'timer2','scheduleid2');    //every 10sec
$.schedule("+60",'timer3');                 //60sec after (one shot)

you can use recipe-js as 'cron' server by .run() method of RecipeJs (off course RecipeNodeJs is also ok)

.schedule() schedules .remake() task. 1st arg is time(3 kind.see above example),2nd is target name, 3rd(optional) is id.

you can turn on / off shedule by .enableSchedule(id) / .clearSchedule(id). id is 3rd argument of .schedule()


$=new RecipeNodeJs();

    return Promise.resolve("Hello");


.main is an utility method. you can use it like .make(). difference between .make() is print out result even if target doesn't console.log().

this is useful if the target recipe just returns value.

if you pass an array of RecipeNodeJs() object in 1st arg and target name in 2nd arg, .main() invokes all of RecipeNodeJs to make the same target then wait complete by Promise.all, then print out collected results.

note that unlike .make(), .main doesn't return not promise() and process.exit() inside after print.

Install CLI

sudo npm install -g recipe-js

CLI Usage

recipe [-f|-F <Recipefile>] [target] [-<option> --<option> ..]
version 1.2.0
A gulp/GNU make like task launcher with CLI tool.Supports Dependencies/Cache/Inference Rules/Promise/Child Process/Deriving/Scheduler.


-f <Recipefile> specify Recipefile,default is "./Recipefile"
<target>:specify target object, default target is 'default'
<option>:options for Recipefile

-F <Recipefile> +trace output
-D <Recipefile> +debug output

(for shebang(#!/usr/bin/env recipe) execution, you can omit -f if there are no Recieptfile in current dir and no other target in command line)

Recipefile example:
# coffee script syntax

# 'default' needs 'prereq0-3', result -> 'Hello World RecipeJs' 
$.R 'default',['prereq0','prereq1','prereq2'],(g)->
    console.log "#{g.prereq0} #{g.prereq1} #{g.prereq2}"

# 'prereq0' needs no prerequisite, -> 'Hello'
$.R 'prereq0',->
    new Promise (rs,rj)->
        setTimeout ->
            rs 'Hello'

# 'prereq1' needs 'prereq1A', g is 'world' , 'prereq1' -> 'World'
$.R 'prereq1','prereq1A',(g)->
    g.replace /^w/,'W'

# 'prereq1A' is 'world' (same as $.R 'prereq1A',->'world')
$.set 'prereq1A','world'

# 'prereq2' needs 'prereq2A', g is 'recipejs', 'prereq2' will be 'RecipeJs'
# @S(this.S=$.S) execlutes child process with stdin(2nd arg)
# @P(this.P=$.P) is short hand of (g)->@S cmd,g
$.R 'prereq2','prereq2A',(g)->
    @S 'sed s/^r/R/',g
    .then @P 'sed s/js$/Js/'

$.R 'prereq2A',->'recipejs'
Example:(with option)
$.R 'default',['flagA','argB','flagC','argD'],(g)->
    console.log "#{g.flagA} #{g.argB} #{g.flagC} #{g.argD}"

$.set 'flagA',false
$.set 'argB','-' 
$.set 'flagC',false
$.set 'argD','-'

#special target 'OPTIONS' 
$.set 'OPTIONS',
    b:'argB:' #':' indicates having argument

#special target 'TRACE'/'DEBUG' for debugging
$.set 'TRACE',true

#command line: recipe -a -b Hello --flagC --argD=World
#->true Hello true World
Example:(file/inference rules)
# $.F tells that specified target(extension/filename) is files(% is wildcard,regex is ok).
$.F ['','%.html']

# Inference rule(% is wildcard, '(.*).html','$' in regex).
$.R '%.html','',$.P 'md2html'

#>recipe test.html
# ->file Hello) -> file test.html (<h1>Hello</h1>)
# file system's timestamp is used for update decision
Example:(file/inference rules2)
$.F ['a','%.o','%.c']

$.R '%.o','%.c',(g,t)->
    $.S "gcc -c -o #{} #{t.dep}"
    .then $.saved  #$.saved indicats target has already been saved

$.R 'a',['b.o','a.o'],(g,t)->
    $.S "gcc -o #{} #{t.deps.join ' '}"
    .then $.saved 'a'

$.R 'clean',$.PX 'rm -f *.o'
$.R 'cleanall','clean',$.PX 'rm -f a'

$.R 'default','a'
$.set 'TRACE',true
#>recipe cleanall

Change Log

Popular Articles from This Page

Top Page

Economizing Technology > recipe-js - A gulp/GNU make like task launcher with CLI tool.Supports Dependencies/Cache/Inference Rules/Promise/Child Process/Deriving/Scheduler