Skip to content

Commit b1c4eec

Browse files
committed
feat: 🎸 stats
1 parent 3b0c055 commit b1c4eec

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"start:version": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts --version",
1212
"start:help": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts --help",
1313
"start:parse": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts parse --src './test-spa'",
14+
"start:stats": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts stats --src './test-spa'",
1415
"build": "tsc && npm run copy:readme",
1516
"copy:readme": "copyfiles ./README.md ./dist",
1617
"lint": "eslint src/**/*.ts",

src/bin/ng-parsel.bin.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import { readFileSync } from 'fs';
44
import { fileURLToPath } from 'url';
55
import { Command } from 'commander';
66
import { dirname, resolve } from 'path';
7-
import { cosmiconfigSync } from 'cosmiconfig';
87

9-
import { mergeOptionalConfigWithDefaults } from '../config/config.helper.js';
108
import { CONFIG_DEFAULT_VALUES } from '../config/config.model.js';
119
import { writeJson } from '../utils/write.util.js';
12-
import { parse } from '../parser/parser.js';
1310
import { printWelcomeMessage } from '../utils/welcome.util.js';
1411
import { parseCommand } from '../commands/parse.js';
12+
import { statsCommand } from '../commands/stats.js';
1513

1614
const program = new Command();
17-
const explorer = cosmiconfigSync('ng-parsel');
1815

1916
program.version(
2017
/*
@@ -37,8 +34,8 @@ program
3734
.option('--specs', 'Parse Specs', true)
3835
.option('-o, --out <string>', 'Output directory for generated files')
3936
.option('--singleFile', 'Output in a single file')
40-
.action((_srcGlob, options) => {
41-
parseCommand(options);
37+
.action((_srcGlob, cliArgs) => {
38+
parseCommand(cliArgs);
4239
});
4340

4441
program.command('init').action(() => {
@@ -55,21 +52,8 @@ program.command('init').action(() => {
5552
program
5653
.command('stats')
5754
.option('-s, --src', 'Glob pattern to search for files')
58-
.action(() => {
59-
printWelcomeMessage();
60-
61-
const configObject = explorer.search();
62-
63-
// TODO: extract this
64-
if (configObject) {
65-
console.log(
66-
chalk.cyan(
67-
`ng-parsel: configuration found under ${configObject.filepath}.
68-
Configuraton from config file will be used.`
69-
)
70-
);
71-
parse(mergeOptionalConfigWithDefaults(configObject.config));
72-
}
55+
.action((_srcGlob, cliArgs) => {
56+
statsCommand(cliArgs);
7357
});
7458

7559
program.parse();

src/commands/parse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export function parseCommand(cliArgs: { [key: string]: string }) {
2525
// TODO: clean this up later
2626
config['src'] = './test-spa';
2727

28-
const parseOutput = parse(config);
29-
writeParsedOutputToDisk(config, parseOutput);
28+
const parsedOutput = parse(config);
29+
writeParsedOutputToDisk(config, parsedOutput);
3030

3131
console.log(chalk.magenta('===================================='));
3232
}

src/commands/stats.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { printWelcomeMessage } from '../utils/welcome.util.js';
2+
import { loadAndMergeConfig } from '../utils/config.util.js';
3+
import { parse } from '../parser/parser.js';
4+
import { convertToComponentStats } from '../converters/component.converter.js';
5+
6+
export function statsCommand(cliArgs: { [key: string]: string }) {
7+
printWelcomeMessage();
8+
9+
const config = loadAndMergeConfig(cliArgs);
10+
11+
// TODO: clean this up later
12+
config['src'] = './test-spa';
13+
14+
const parsedOutput = parse(config);
15+
console.log(convertToComponentStats(parsedOutput.ngParselComponents));
16+
}

src/converters/component.converter.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { NgParselComponent } from '../parser/component/component.model.js';
2+
3+
export interface NgParselComponentStats {
4+
standalone: number;
5+
moduleBased: number;
6+
cva: number;
7+
total: number;
8+
}
9+
10+
export function convertToComponentStats(componentStats: NgParselComponent[]): NgParselComponentStats {
11+
return componentStats.reduce(
12+
(acc: NgParselComponentStats, component) => {
13+
if (component.standalone) {
14+
acc.standalone = acc.standalone + 1;
15+
} else {
16+
acc.moduleBased = acc.moduleBased + 1;
17+
}
18+
19+
if (component.cva) {
20+
acc.cva = acc.cva + 1;
21+
}
22+
23+
acc.total = acc.total + 1;
24+
return acc;
25+
},
26+
{
27+
standalone: 0,
28+
moduleBased: 0,
29+
cva: 0,
30+
total: 0,
31+
}
32+
);
33+
}

0 commit comments

Comments
 (0)