Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ViES committed Jun 29, 2020
0 parents commit f36aafe
Show file tree
Hide file tree
Showing 7 changed files with 507 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ts-coverage

When you migrate your project to TypeScript it's useful to track the progress.
ts-coverage print simple histogram so you can always know how close to the goal you are.

## Example

`npx ts-coverage ./src`

*Output*

```
.js(x) | ############################################################ | 1933 (97.9%)
.ts(x) | # | 41 (2.1%)
```
14 changes: 14 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path');
const lib = require('./lib');
const { argv } = require('yargs')
.usage("$0 <path>");

if (module.parent) throw new Error(`Can't be imported`);

process.on('unhandledRejection', error => {
throw error;
});

const [dir] = argv._;

lib(dir);
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib');
59 changes: 59 additions & 0 deletions lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require('path');
const fs = require('fs').promises;
const globby = require('globby');
const chalk = require('chalk');
const histogram = require('ascii-histogram');

const percent = (n, total) => (n && (n / total * 100).toFixed(1)) + '%';

const formatter = total => n => `${n} (${percent(n, total)})`;

const printOutput = data => {
const total = Object.values(data)
.reduce((acc, v) => acc + v, 0);

const output = histogram(data, { map: formatter(total) })
.split('\n')
.map((v, i) => {
switch (i) {
case 0:
return chalk.yellowBright(v);
case 1:
return chalk.blueBright(v);
default:
return v;
}
})
.join('\n');

console.log(total
? output
: 'No sources found ¯\\_(ツ)_/¯.'
);
};

/**
* @param {string} directory
*/
module.exports = async (directory = process.cwd()) => {
const extensions = {};

directory = path.resolve(directory);

await fs.access(directory);

for await (const p of globby.stream(`${directory}/**/*.{js,jsx,ts,tsx}`)) {
const extension = path.extname(p);
extensions[extension] = (extensions[extension] || 0) + 1;
}

const jsx = extensions['.js'] + extensions['.jsx'];
const tsx = extensions['.ts'] + extensions['.tsx'];

const data = {
'.js(x)': jsx || 0,
'.ts(x)': tsx || 0,
};

printOutput(data);
};
Loading

0 comments on commit f36aafe

Please sign in to comment.