npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-sloc

v0.2.1

Published

A small tool for counting SLOC.

Downloads

68,265

Readme

node-sloc

A small tool written in node for counting source lines of code.

NPM

Installation

Can be used as either a command line application or as a module.

As a command line application

Install it globally and use it as a CLI -- both node-sloc and sloc will work in this case.

$ npm install -g node-sloc
$ node-sloc .
$ sloc . # Both node-sloc and sloc will run the CLI

You can also run it with npx:

$ npx node-sloc .

As a node module

npm install --save node-sloc

Usage

Command Line

node-sloc [path] [options]

Options

usage:
           node-sloc [path] [options]
options:
           -h, --help                         Prints usage information

           -l, --list-extensions              Lists all default file extensions

           -e, --include-extensions <list>    Include non-default file extensions,
                                              specified by a comma separated string of extensions

           -i, --ignore-extensions <list>     Include list of file extensions to ignore,
                                              specified by a comma separated string of extensions

           -x, --ignore-paths <list>          Include a list of folders to exclude. Supports glob patterns

           -d, --ignore-default               Ignore the default file extensions

           -v, --verbose                      Output extra information during execution

Examples

 node-sloc ../app
 node-sloc ../app --include-extensions "aaa, bbb, ccc" --ignore-extensions "xml, yaml"
 node-sloc ../app --ignore-paths "node_modules, **/*.test.js"
 node-sloc file.js
$ node-sloc . -x "node_modules"
Reading file(s)...

    +---------------------------------------------------+
    | SLOC                          | 2682              |
    |-------------------------------|--------------------
    | Lines of comments             | 206               |
    |-------------------------------|--------------------
    | Blank lines                   | 134               |
    |-------------------------------|--------------------
    | Files counted                 | 27                |
    |-------------------------------|--------------------
    | Total LOC                     | 2888              |
    +---------------------------------------------------+

Module

CommonJS

const sloc = require('node-sloc')

const options = {...}

sloc(options).then((res) => {...})

ES Modules

// Named import is also supported, i.e.
// import {sloc} from 'node-sloc'
import sloc from 'node-sloc'

const options = {...}

sloc(options).then((res) => {...})

Options

The options object the function takes as a parameter has the following properties:

path             Required. The path to walk or file to read.
extensions       Additional file extensions to look for. Required if ignoreDefault is set to true.
ignorePaths      Optional. A list of directories to ignore. Supports glob patterns.
ignoreDefault    Optional. Whether to ignore the default file extensions or not. Defaults to false.
logger           Optional. Outputs extra information to if specified.

Resolved object

The object returned when executing the function has the following structure:

{
  paths,      // An array of all filepaths counted
  loc,        // Lines of code (SLOC + comments)
  sloc,       // Source lines of code
  blank,      // Number of blank lines
  comments,   // Lines of comments
  files,      // Number of files counted
}

Example

const sloc = require('node-sloc')

const options = {
  path: '../app', // Required. The path to walk or file to read.
  extensions: ['aaa', 'bbb', 'ccc'], // Additional file extensions to look for. Required if ignoreDefault is set to true.
  ignorePaths: ['node_modules'], // A list of directories to ignore. Supports glob patterns.
  ignoreDefault: false, // Whether to ignore the default file extensions or not
  logger: console.log, // Optional. Outputs extra information to if specified.
}

// Using promises
sloc(options).then((res) => {
  console.log(res.paths, res.sloc, res.comments)
})

// Using node-style callbacks
sloc(options, (err, res) => {
  if (err) {
    // do some error handling
  }
  console.log(res.paths, res.sloc, res.comments)
})

// Async-await (if supported)
const res = await sloc(options)
console.log(res)

Supported languages

  • ActionScript
  • Assembly
  • C#
  • C/C++
  • CoffeeScript
  • CSS
  • Elixir
  • Elm
  • Erlang
  • Go
  • Groovy
  • Handlebars
  • Haskell
  • HTML
  • Jade
  • Java
  • JavaScript
  • JSX
  • LESS
  • Lua
  • Mustache
  • Objective C
  • Perl
  • PHP
  • Pug
  • Python
  • Ruby
  • Rust
  • Sass
  • Scala
  • Shell script
  • Squirrel
  • Stylus
  • Swift
  • TypeScript
  • Visual Basic
  • XML
  • YAML
  • Any other language using C-style comments

License

MIT