flosse / sloc

simple tool to count SLOC (source lines of code)
MIT License
944 stars 84 forks source link

limiting to a single file type. #59

Open ceelian opened 8 years ago

ceelian commented 8 years ago

Is there any working solution to limit analysis to a single file extension? I tried using the -e (--exclude) switch to limit e.g. to JavaScript files. The idea is to exclude all files which are not javascript files.

sloc  -e ".+^(\.js)$" -d .

But it is not working.

mindplay-dk commented 8 years ago

Yeah, I was looking for something like this as well - most projects have a primary language, and typically, you want the line count for that language.

We have the --exclude option for blacklisting files matching a given pattern - may I suggest a complementary --include option for whitelisting?

Since both options could be used together, I'd suggest running the whitelist filter first (if present) then running the blacklist filter on the remaining files.

flosse commented 8 years ago

Isn't there a library that we can use? Something like minimatch ?

bwin commented 8 years ago

Or node-glob. Minimatch is the just the matcher, while glob operates on the filesystem and returns actual filenames instead of regexes. It uses minimatch internally. Maybe the readDir part in cli.coffee could be simplified by using glob?

bwin commented 8 years ago
glob = require 'glob'

pattern = "**"
options =
    ignore: ["**/*.json", "node_modules/**"]
    nodir: yes # only files
glob pattern, options, (err, matches) -> console.log matches

This returns all filenames that match pattern, filtered by ignore. Supports only one pattern, but multiple ignore-patterns. Edit: Multiple patterns are possible with a workaround: glob('{' + pattern1 + ',' + pattern2 + '}', cb) from isaacs/node-glob#217

mindplay-dk commented 8 years ago

Looks like this will do multiple include and exclude:

https://github.com/mklabs/node-fileset

shyambhiogade commented 7 years ago

one cheap solution to keep it for single file type. run sloc two times. first on all files and after that on all files except the one you wanted and then you can use powershell to minus the second result from first to get the result for single file type.

listenlight commented 5 years ago

I got this to work... npx sloc --format cli-table --format-option head --exclude "node_modules|build|\.svg$\.xml" ./