# sloc

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

Latest version **0.3.2** (published 2024-02-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install sloc
pnpm add sloc
yarn add sloc
bun add sloc
```

Provides the command `sloc`.

## Health

**Score 33/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2024-02-06 |
| First published | 2012-07-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/sloc) |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 34.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 972 |
| Author | Markus Kohlhase |
| Maintainers | flosse |

## Links

- npm: https://www.npmjs.com/package/sloc
- Repository: https://github.com/flosse/sloc
- Homepage: https://github.com/flosse/sloc#readme
- Issues: http://github.com/flosse/sloc/issues
- npm.io page: https://npm.io/package/sloc

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^3.2.4
- [readdirp](https://npm.io/package/readdirp.md) ^3.3.0
- [cli-table](https://npm.io/package/cli-table.md) ^0.3.11
- [commander](https://npm.io/package/commander.md) ^11.0.0

## Recent versions

- 0.3.2 (latest) — 2024-02-06
- 0.3.1 — 2024-01-01
- 0.3.0 — 2023-09-20
- 0.2.1 — 2018-12-13
- 0.2.0 — 2017-01-27
- 0.1.11 — 2016-08-31
- 0.1.10 — 2016-02-05
- 0.1.9 — 2014-12-05
- 0.1.8 — 2014-12-05
- 0.1.7 — 2014-09-11
- 0.1.6 — 2014-09-06
- 0.1.5 — 2014-08-26
- 0.1.4 — 2014-08-21
- 0.1.3 — 2014-07-31
- 0.1.2 — 2014-07-27
- … 10 more at https://npm.io/package/sloc/versions

## 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](https://badge.fury.io/js/sloc.svg)](http://badge.fury.io/js/sloc)
[![License](https://img.shields.io/npm/l/sloc.svg)](https://github.com/flosse/sloc/blob/master/LICENCE.txt)
[![Minified size](http://img.shields.io/badge/size-6,1K-blue.svg)](https://github.com/flosse/sloc)

## 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:

```shell
sudo npm install -g sloc
```

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

```shell
npm install --save sloc
```

### Browser

You can also use sloc within your browser application.

Link `sloc.js` in your HTML file:

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

## Usage

### CLI

```shell
sloc [option] <file>|<directory>
```

Options:

```shell
-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.:

```shell
$ 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

```shell
$ 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

```javascript
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

```javascript
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

- Grunt
  - [grunt-sloc](https://github.com/rhiokim/grunt-sloc).
  - [grunt-sloccount](https://github.com/asciidisco/grunt-sloccount)
  - [grunt-maxlines](https://github.com/zerok/grunt-maxlines)
  - [grunt-file-sloc](https://github.com/bubkoo/grunt-file-sloc)

- Gulp
  - [gulp-sloc](https://github.com/oddjobsman/gulp-sloc):
  - [gulp-sloc2](https://github.com/pnarielwala/gulp-sloc2)

- [Atom](https://atom.io/)

  - [atom-sloc](https://github.com/sgade/atom-sloc)
  - [line-count](https://github.com/mark-hahn/line-count)

- Jenkins
  - [sloc-for-jenkins](https://www.npmjs.com/package/sloc-for-jenkins)
  - [sloccount](https://github.com/asciidisco/sloccount)

- Istanbul
  - [istanbul-reporter-clover-limits](https://github.com/Cellarise/istanbul-reporter-clover-limits/)

- Codemetrics
  - [codemetrics-process-sloc](https://github.com/maxdow/codemetrics-process-sloc)

## 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

```shell
npm test
```

## Build

```shell
npm run prepublish
```

## Changelog

see `CHANGELOG.md`

## License

sloc is licensed under the MIT license

---
_Source: https://npm.io/package/sloc · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
