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

(c)GoogleCustomSearch

Examples

Core module (RecipeJs class)

RecipeJs=require('recipe-js').RecipeJs;

$=new RecipeJs();

$.R('default',['prereq0','prereq1'],(g)=>{
    return(`${g.prereq0} ${g.prereq1}`);
});

$.set('prereq0','Hello');

$.R('prereq1',()=>{
    return new Promise((rs,rv)=>{
        setTimeout(()=>{
            rs('World');
        },1000);
    });
});

$.make('default')
.then((g)=>{
    console.log(g);
});

//-> Hello World

Nodejs module (RecipeNodeJs class extended from RecipeJs)

RecipeNodeJs=require('recipe-js').RecipeNodeJs;

$=new RecipeNodeJs();

$.R('default',['prereq0','prereq1'],(g)=>{
    console.log(`${g.prereq0} ${g.prereq1}`);
});

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

$.R('prereq1',()=>{
    return $.S('whoami')
    .then($.P('sed s/^/Mr./'));
});

$.main('default');

//-> Hello Mr.username
//$.P(cmd) is short hand for (stdin)=>$.S(cmd,stdin)
//$.X(cmd)/$.PX is similar but using shell and unable to inject into stdin

Inference Rules(.)

RecipeJs=require('recipe-js').RecipeJs;

$=new RecipeJs(); 

$.R('%.html','%.md',(g)=>{
    return g.replace(/^## (.*)/,'<h2>$1</h2>')
});

$.set('prereq0.md','## Hello');
$.set('prereq1.md','## RecipeJs');

$.R('default',['prereq0.html','prereq1.html'],(g)=>{
    return(g['prereq0.html']+g['prereq1.html']);
});

$.make('default')
.then((g)=>{
    console.log(g);
});
//-> <h2>Hello</h2><h2>RecipeJs</h2>
//% is wildcard, regex is ok like '(.*)\.html','$1.md'

File IO

RecipeNodeJs=require('recipe-js').RecipeNodeJs;

$=new RecipeNodeJs(); 

// $.F tells that specified targets(extension/filename) are files.
$.F('%.md');
$.F('%.html');

$.R('%.html','%.md',$.P('md2html'));

$.R('default',['prereq0.html']);

$.main('default'); //must be main() not make() for saving results
//-> file prereq0.md(# Hello) -> file prereq0.html (<h1>Hello</h1>) 
//file system's timestamp is used for update decision

Cache / Trace

RecipeNodeJs=require('recipe-js').RecipeNodeJs;

$=new RecipeNodeJs({
    cacheFile:'cache.json',
    traceEnabled:true
});

$.R('default','prereq0',(g)=>{
    console.log(`Hello ${g}`);
});

$.R('prereq0',()=>{
    return $.S('whoami')
    .then((r)=>{
        return $.cache('prereq0',r,180); //cache time:180sec,null is forever
    });
});

$.main('default'); 

//-> result will be saved in 'cache.json' with data '{"prereq0":{"v":"user\n","expire":1499307137335}}'

Deriving (extends)

RecipeJs=require('recipe-js').RecipeJs;

parent=new RecipeJs(); 

parent.R('default','prereq0',(g)=>{
    console.log(`Hello ${g}`);
});

child=new RecipeJs({
    extends:parent
}); 

child.R('prereq0',()=>{
    return('World');
});

child.make('default'); 
//-> Hello World ('prereq0' would be stored in 'child' object)

Deriving (parent)

RecipeJs=require('recipe-js').RecipeJs;

parent=new RecipeJs(); 

parent.R('prereq0',()=>{
    return('World');
});

child=new RecipeJs({
    parent:parent
}); 

child.R('default','prereq0',(g)=>{
    console.log(`Hello ${g}`);
});


child.make('default'); 
//-> Hello World ('prereq0' would be stored in 'parent' object for sharing results by children)

Command line parser

RecipeNodeJs=require('recipe-js').RecipeNodeJs;

$=new RecipeNodeJs();

$.R('default',['prereq0','prereq1','prereq2'],(g)=>{
    console.log(`${g.prereq0} ${g.prereq1} ${g.prereq2}`);
});

$.R('prereq0','-');  //defaults
$.R('prereq1',false);
$.R('prereq2','-');

remains=$.setByArgv(process.argv,{
    b:'prereq0:',  //':' indicates has arg
    c:'prereq1'
});

$.main('default');

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

Install CLI

sudo npm install -g recipe-js

CLI Usage

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

Options:

-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

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'
        ,1000

# '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}"

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

#special target 'OPTIONS' 
$.set 'OPTIONS',
    a:'flagA'
    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 ['%.md','%.html']

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

#>recipe test.html
# ->file test.md(# 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.target} #{t.dep}"
    .then $.saved t.target  #$.saved indicats target has already been saved

$.R 'a',['b.o','a.o'],(g,t)->
    $.S "gcc -o #{t.target} #{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
#>recipe cleanall

Change Log

Popular Articles from This Page

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