Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sloc

flosse170.9kMIT0.3.1TypeScript support: definitely-typed

sloc is a simple tool to count SLOC (source lines of code)

readme

sloc

Create stats of your source code:

  • physical lines
  • lines of code (source)
  • lines with comments
  • lines with single-line comments
  • lines with block comments
  • lines mixed up with source and comments
  • empty lines within block comments
  • empty lines
  • lines with TODO's

NPM version License Minified size

Supported outputs

In addition to the default terminal output (see examples below), sloc provides an alternative set of output formatters:

  • CSV
  • JSON
  • Command line table

Install

To use sloc as an application install it globally:

sudo npm install -g sloc

If you're going to use it as a Node.js module within your project:

npm install --save sloc

Browser

You can also use sloc within your browser application.

Link sloc.js in your HTML file:

<script src="lib/sloc.js"></script>

Usage

CLI

sloc [option] <file>|<directory>

Options:

-h, --help                  output usage information
-V, --version               output the version number
-e, --exclude <regex>       regular expression to exclude files and folders
-i, --include <regex>       regular expression to include files and folders
-f, --format <format>       format output: json, csv, cli-table
    --format-option [value] add formatter option
-k, --keys <keys>           report only numbers of the given keys
-d, --details               report stats of each analyzed file
-a, --alias <custom ext>=<standard ext> alias custom ext to act like standard ext (eg. php5=php,less=css)

e.g.:

$ sloc src/

---------- Result ------------

            Physical :  1202
              Source :  751
             Comment :  322
 Single-line comment :  299
       Block comment :  23
               Mixed :  116
               Empty :  245

Number of files read :  10

------------------------------

or

$ sloc --details \
       --format cli-table \
       --keys total,source,comment \
       --exclude i18n*.\.coffee \
       --format-option no-head src/

┌─────────────────────────────────┬──────────┬────────┬─────────┐
│ src/cli.coffee                  │ 98       │ 74     │ 7       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/helpers.coffee              │ 26       │ 20     │ 0       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/sloc.coffee                 │ 196      │ 142    │ 20      │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/formatters/simple.coffee    │ 44       │ 28     │ 7       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/formatters/csv.coffee       │ 25       │ 14     │ 5       │
├─────────────────────────────────┼──────────┼────────┼─────────┤
│ src/formatters/cli-table.coffee │ 22       │ 13     │ 0       │
└─────────────────────────────────┴──────────┴────────┴─────────┘

Node.js

Or use it in your own node module

var fs    = require('fs');
var sloc  = require('sloc');

fs.readFile("mySourceFile.coffee", "utf8", function(err, code){

  if(err){ console.error(err); }
  else{
    var stats = sloc(code,"coffee");
    for(i in sloc.keys){
      var k = sloc.keys[i];
      console.log(k + " : " + stats[k]);
    }
  }
});

Browser usage

var sourceCode = "foo();\n /* bar */\n baz();";

var stats = window.sloc(sourceCode,"javascript");

Contribute an new formatter

  1. Fork this repo

  2. add the new formatter into src/formatters/ that exports a method with three arguments:

    1. results (object)
    2. global options (object)
    3. formatter specific options (array)
  3. add the formatter in src/cli.coffee

  4. open a pull request

sloc adapters

Supported languages

  • Assembly
  • Agda
  • Brightscript
  • C / C++
  • C#
  • Clojure / ClojureScript
  • CoffeeScript / IcedCoffeeScript
  • Crystal
  • CSS / SCSS / SASS / LESS / Stylus
  • Dart
  • Erlang
  • F#
  • Fortran
  • Go
  • Groovy
  • Handlebars
  • Haskell
  • Haxe
  • Hilbert
  • HTML
  • hy
  • Jade
  • Java
  • JavaScript
  • JSX
  • Julia
  • Kotlin
  • LaTeX
  • LilyPond
  • LiveScript
  • Lua
  • MJS
  • Mochi
  • Monkey
  • Mustache
  • Nim
  • Nix
  • Objective-C / Objective-C++
  • OCaml
  • Perl 5
  • PHP
  • PRQL
  • Pug
  • Python
  • R
  • Racket
  • Ren'Py
  • Ruby
  • Rust
  • Scala
  • Squirrel
  • SVG
  • Swift
  • Typescript
  • Visual Basic
  • XML
  • Yaml

Run tests

npm test

Build

npm run prepublish

Changelog

see CHANGELOG.md

License

sloc is licensed under the MIT license

changelog

Changelog

v0.3.1

  • fix regression (#137)
  • Remove bower support
  • new supported languages/file extensions
    • PRQL

v0.3.0

  • updated dependencies
  • new supported languages/file extensions
    • Agda
    • Dart
    • Pug
    • Ren'Py

v0.2.1

  • new supported languages/file extensions
    • vue
    • latex
    • Fortran 2008/2018
    • LilyPond
    • MJS
    • bsl
    • F#
    • SQL

v0.2.0

  • count lines with TODOs (it's reflected in the summary object with the new property todo)
  • support tsx
  • fixed #83

v0.1.11

  • new supported languages
    • Brightscript
    • Kotlin
    • Squirrel
  • fixed #55
  • fixed #70

v0.1.10

  • new supported languages
    • Assembly
    • Crystal
    • IcedCoffeeScript
    • Jade
    • Mochi
    • Nim
    • Objective-C
    • Objective-C++
    • Ocaml
    • SASS
  • fixed problem with coffee-script v1.9.x
  • fixed #48
  • cli-table formatter: group by extension
  • add some more extensions like hxx, jsx etc.

v0.1.9

  • fixed counting empty lines within block comments

v0.1.8

  • new supported languages
    • ClosureScript
    • Hilbert
    • htm,svg,xml as html variants
    • Handlebars
    • Mustache
  • syntax alias support (e.g. sloc -a foo=php5 src/)

v0.1.7

  • fixed reading empty files
  • fixed calculating source lines
  • new supported languages
    • Groovy
    • Yaml

v0.1.6

  • added C# support
  • use coffee-script 1.8.0
  • `package.json´ optimizations

v0.1.5

  • fixed problems with first line comments
  • added support for nix

v0.1.4

  • new supported languages
    • Clojure
    • Haskell
    • hy
    • Julia
    • Perl 5
    • R
    • Racket
    • Rust
    • Swift
    • Visual Baisc

v0.1.3

  • new supported languages
    • Ruby
    • TypeScript

v0.1.2

  • added CRLF support
  • fixed counting mixed lines
  • fixed counting total comments
  • new supported languages
    • Scala
    • LiveScript

v0.1.1

  • added Monkey support
  • print with colors
  • add --strip-colors option
  • fixed summarize helper method
  • fixed path concatenation

v0.1.0

  • changed API (now it has a better readability)
  • refactored the algorithm
  • relicensed under the MIT license
  • support counting mixed lines (comment + source code)
  • limit reported numbers by a list of keys
  • multiple output formatters
    • csv
    • cli table
    • json
  • new supported languages
    • erlang
    • less
    • lua
    • haxe

v0.0.x

Please run git log v0.0.1...v0.0.8 ;-)