SLOC

Calculate source lines of code (SLOC) for a list of JavaScript files.

Usage

var analyze = require( '@stdlib/_tools/static-analysis/js/sloc-file-list' );

analyze( files, [options,] clbk )

Calculates source lines of code (SLOC) for a list of JavaScript files.

var files = [ './beep.js', './boop.js' ];

analyze( files, clbk );

function clbk( error, results ) {
    if ( error ) {
        throw error;
    }
    console.log( JSON.stringify( results ) );
}

The function accepts the following options:

  • cumulative: boolean indicating whether to perform a cumulative analysis. Default: true.

By default, the function performs a cumulative analysis. To return a SLOC calculation for each file in files, set the cumulative option to false.

var files = [ './beep.js', './boop.js' ];

var opts = {
    'cumulative': false
};
analyze( files, opts, clbk );

function clbk( error, results ) {
    var i;
    if ( error ) {
        throw error;
    }
    for ( i = 0; i < results.length; i++ ) {
        console.log( files[ i ] + ': ' + results[ i ] );
    }
}

analyze.sync( files[, options] )

Synchronously calculates source lines of code (SLOC) for a list of JavaScript files.

var files = [ './beep.js', './boop.js' ];

var results = analyze( files );
if ( results instanceof Error ) {
    throw results;
}
console.log( JSON.stringify( results ) );

The function accepts the same options as analyze() above.

Notes

  • File paths are resolved relative to the current working directory of the calling process.

Examples

var join = require( 'path' ).join;
var analyze = require( '@stdlib/_tools/static-analysis/js/sloc-file-list' );

var files = [
    join( __dirname, 'examples', 'index.js' ),
    join( __dirname, 'lib', 'index.js' ),
    join( __dirname, 'lib', 'main.js' ),
    join( __dirname, 'lib', 'sync.js' ),
    join( __dirname, 'test', 'test.js' ),
    join( __dirname, 'bin', 'cli' )
];

analyze( files, clbk );

function clbk( error, results ) {
    if ( error ) {
        return console.error( 'Error: %s', error.message );
    }
    console.log( JSON.stringify( results ) );
}

CLI

Usage

Usage: js-sloc-file-list [options] [<file> <file> ...]

Options:

  -h,  --help            Print this message.
  -V,  --version         Print the package version.
       --split sep       Separator used to split stdin data. Default: /\\r?\\n/.
       --no-cumulative   Calculate SLOC per file and print results as CSV.

Notes

  • If cumulative analysis is disabled, the CLI prints SLOC results for each input file as comma-separated values (CSV). The first CSV line is a header line, describing column contents.

Examples

$ js-sloc-file-list ./examples/index.js
{...}

To use as a standard stream,

$ echo -n $'./examples/index.js\n./lib/index.js\n' | js-sloc-file-list
{...}

To print SLOC for each input file, set the no-cumulative flag.

$ echo -n $'./examples/index.js\n./lib/index.js\n' | js-sloc-file-list --no-cumulative
file,sloc
"./examples/index.js",<number>
"./lib/index.js",<number>