-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (48 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
//@ts-check
import fs from "fs";
import { Application, arg } from "@balam314/cli-app";
import glob from "glob";
import chalk from "chalk";
const countlines = new Application("count-lines", "A small script to count lines of files in a directory.");
countlines.onlyCommand().args({
namedArgs: {
ignoreEmptyLines: arg().description("Whether to ignore empty lines when counting.")
.valueless().aliases("e")
},
positionalArgs: [{
name: "pattern",
description: "Only file names that match this glob pattern are counted",
default: "*.*"
}]
}).impl(async (opts, app) => {
process.stdout.write("\n");
const lines = await countLines(opts.positionalArgs[0], {
ignoreEmptyLines: opts.namedArgs.ignoreEmptyLines
});
console.log(chalk.blueBright(`\t${lines} lines.`));
});
function countLines(pattern, options){
return new Promise((resolve, reject) => {
glob(pattern, {
nodir: true
}, (err, files) => {
if(err) reject(err.message);
console.log(chalk.blueBright(`\tCounting lines in ${files.length} files...`));
resolve(
files
.map(file => fs.readFileSync(file, "utf-8"))
.map(file => countLinesInText(file, options))
.reduce((count, lines) => count + lines, 0)
);
});
});
}
function countLinesInText(text, options){
if(options.ignoreEmptyLines){
return text.split(/\r?\n/g).map(line => line.replace(/[ \t]/g, "")).filter(line => line != "").length;
} else {
return text.split(/\r?\n/g).length;
}
}
countlines.run(process.argv);