Git Product home page Git Product logo

jake-utils's Introduction

The jake-utils is a collection of useful functions for using Jake.

Note: As a package, it does un-orthodox things like creating global functions and attempting to make everything run synchronously. This design decision was made in order to be consistent with Jake, and behave similarly to a "build system".

Usage

In your Jakefile, simply add the following code near the top:

require('jake-utils');

You will then have a number of functions you an use.

Mocha Tests

My favorite test harness is Mocha.

You can now do something like:

mochaTests({
    directory: "test",
    files    : /test\-.*\.js/,
    coverage : true,
    reporter : 'tap',
    output   : 'test/results.tap'
});

This restores the console.log() before running any of the tests.

Note: Using Mocha with log4js means that your TAP results get wonky when all console.log() calls get interspersed with the TAP code.

Display Messages

You can have obnoxious headers around your tasks, to make it stand out more when you are looking at builds on Jenkins.

task('api', function() {
    start("Generating the Internal Documentation");

    // ...
    
    end("View documents in '%s'", apidest);
});

Lint Analysis using JSHint

We have a nice wrapper around the JSHint utility for analyzing all the files found in the directories specified. For example:

desc("Lints all of the script files in the source directories");
task('lint', function(){
    start("Analyzing the Script Files");

    var codelib    = [ 'services' , 'routes', 'resources' ];
    lint( codelib );

    end();
});

Concatenate files

The method concat is available to concatenate multiple files into one. The method resolves patterns in filenames, and has options to add a header, separators, and a footer to the concatenated file. Example:

desc("Concatenates all source files into library");
task('concat', function(){
    start("Concatenating the source files");

    var result = concat({
        src: [
            './src/main.js',
            './src/extra.js',
            './src/functions/**',
        ],
        dest: './lib/mylibrary.js',           // optional
        header: '// license information...',  // optional
        separator: '\n',                      // optional
        footer: '// the end...'               // optional
    });

    // returned result is an object containing:
    //     {String} code   The concatenated data
    //     {String} src    The list with resolved filenames

    end();
});

Markdown File Conversion

The markdown function converts a markdown-formatted file into an HTML file using the marked parser.

desc("Converts README into HTML File);
task('readme', function(){
    start("Converting the README into a web page");
    markdown( "README.md", "pub/README.html");
    end();
});

The function takes an optional third parameter for marked-specific options.

Minify using uglify-js

The method minify is available to minify a source file using uglify-js. The method accepts one source file, or an array with multiple source files, and resolves patterns in the filenames. Example:

desc("Minify the library");
task('minify', function(){
    start("Minifying library");

    var result = minify({
        src:  './lib/mylibrary.js',
        dest: './lib/mylibrary.min.js',       // optional
        options: {},                          // uglify-js options. optional
        header: '// license information...',  // optional
        separator: '\n',                      // optional
        footer: '// the end...'               // optional
    });

    // returned result is an object containing:
    //     {String} code   The concatenated data
    //     {String} src    The list with resolved filenames

    end();
});

Replace patterns in files

The method replace can be used to replace patterns in files, for example to fill in today's date or the version number of the package. The patterns can be a string or a regular expression. The list with source files can contain patterns. Example:

desc("Replace version and date in source files");
task('replace', function(){
    start("Replace version and date in source files");

    var result = replace({
        replacements: [
            {pattern: '@@date',    replacement: today()},
            {pattern: '@@version', replacement: version()}
        },
        src: [
         'main.js',
         'other/*.js'
        ]
    });

    // returned result is an object containing:
    //     {String} src    The list with resolved filenames

    end();
});

Utility functions

Other utility methods are:

var p = pkg();              // package.json contents
var v = version();          // version number from package.json
var d = today();            // formatted date of today
var data = read(filename);  // read data (fs.readFileSync)
write(filename, data);      // write data (fs.writeFileSync)

jake-utils's People

Contributors

howardabrams avatar jfhbrook avatar josdejong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jake-utils's Issues

jake-utils broken, probably due to a new version of jshint

Loading jake-utils fails with the following error:

Error: Cannot find module 'jshint/lib/hint'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/path-to-project/node_modules/jake-utils/lint.js:1:76)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)

Seems jshint has moved its source files in the package

Use module exports instead of globals

Currently, all methods provided by jake-utils are globally defined. It would be great if jake-utils would export them via regular module exports instead.

Add methods like minify, concat, replace

Hi Howard,

I was looking around for a nice jake-utils library containing easy to use methods like 'minify', concat, replace, etc. I think it would be great to add these methods to your library.

I have just created these methods myself, because I couldn't really find a library matching my needs. I would be happy to contribute them to your jake-utils library.

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.