SLOC

Calculate source lines of code (SLOC) on a file glob.

Usage

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

analyze( [options,] clbk )

Calculates source lines of code (SLOC) on a file glob.

analyze( clbk );

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

The function accepts the following options:

  • cumulative: boolean indicating whether to perform a cumulative analysis. Default: true.
  • dir: root directory from which to search for files. May be either an absolute path or a path relative to the current working directory. Default: current working directory.
  • pattern: glob pattern used to find JavaScript files. Default: '**/*.js'.
  • ignore: list of glob patterns used to exclude matches.

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

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

function clbk( error, results ) {
    if ( error ) {
        throw error;
    }
    console.log( JSON.stringify( results ) ); // array of arrays: [[<file>,<sloc>],...]
}

To search from an alternative directory, set the dir option.

var opts = {
    'dir': '/foo/bar/baz'
};

analyze( opts, clbk );

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

By default, the implementation searches for all files with a .js file extension. To provide an alternative include filter, set the pattern option.

var opts = {
    'pattern': '**/foo/**/*.js'
};

analyze( opts, clbk );

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

To exclude matches, set the ignore option.

var opts = {
    'ignore': [
        'node_modules/**',
        'build/**',
        'reports/**'
    ]
};

analyze( opts, clbk );

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

analyze.sync( [, options] )

Synchronously calculates source lines of code (SLOC) on a file glob.

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

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

Notes

  • If unable to find any matching files and the cumulative option is true, both functions return null for the analysis results.
  • If unable to find any matching files and the cumulative option is false, both functions return an empty array for the analysis results.

Examples

var resolve = require( 'path' ).resolve;
var analyze = require( '@stdlib/_tools/static-analysis/js/sloc-glob' );

var opts = {
    'dir': __dirname
};

analyze( opts, clbk );

function clbk( error, results ) {
    if ( error ) {
        return console.error( 'Error: %s', error.message );
    }
    if ( results ) {
        console.log( JSON.stringify( results ) );
    } else {
        console.log( 'Unable to find any matching files.' );
    }
}

CLI

Usage

Usage: js-sloc-glob [options] [<dir>]

Options:

  -h,  --help                Print this message.
  -V,  --version             Print the package version.
       --pattern pattern     Inclusion glob pattern. Default: **/*.js.
       --ignore pattern      Exclusion glob pattern.
       --no-cumulative       Print SLOC for each matched file separately.
       --format fmt          Output format: 'csv' or 'ndjson'. Default: csv.

Notes

  • If not provided a dir argument, the current working directory is the search directory.

  • To provide multiple exclusion glob patterns, set multiple --ignore option arguments.

    $ js-sloc-glob --ignore=node_modules/** --ignore=build/** --ignore=reports/**
    
  • By default, if cumulative analysis is disabled, the CLI prints SLOC results for each matched file as comma-separated values (CSV). The first CSV line is a header line, describing column contents.

  • If cumulative analysis is disabled and the format flag is set to ndjson, the CLI prints SLOC results for each matched file as newline-delimited JSON (NDJSON). Each JSON object has the following fields:

    • file: filename.
    • sloc: source lines of code (SLOC).

Examples

$ js-sloc-glob .
{...}

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

$ js-sloc-glob --no-cumulative .
file,sloc
"<filename>",<number>
"<filename>",<number>

To print SLOC for each matched file as newline-delimited JSON (NDJSON), set the no-cumulative and format flags.

$ js-sloc-glob --no-cumulative --format=ndjson .
{"file":"...","sloc":<number>}
{"file":"...","sloc":<number>}