Skip to content

Commit af8ce77

Browse files
committed
feat: 🎸 add all kinds of stats converters and printers
1 parent 2f1c01a commit af8ce77

22 files changed

+580
-21
lines changed

src/commands/stats.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
import { table } from 'table';
2-
1+
import { convertToComponentStats } from '../converters/component/component.converter.js';
2+
import { convertToDirectiveStats } from '../converters/directive/directive.converter.js';
3+
import { printComponentStats } from '../printers/component-stats.printer.js';
34
import { printWelcomeMessage } from '../utils/welcome.util.js';
45
import { loadAndMergeConfig } from '../utils/config.util.js';
5-
import { convertToComponentStats, NgParselComponentStats } from '../converters/component.converter.js';
6+
import { printDirectiveStats } from '../printers/directive-stats.printer.js';
7+
import { printModuleStats } from '../printers/module-stats.printer.js';
8+
import { convertToModuleStats } from '../converters/module/module.converter.js';
9+
import { printPipeStats } from '../printers/pipe-stats.printer.js';
10+
import { convertToPipeStats } from '../converters/pipes/pipes.converter.js';
11+
import { printServiceStats } from '../printers/service-stats.printer.js';
12+
import { convertToServiceStats } from '../converters/service/service.converter.js';
613
import { parse } from '../parser/parser.js';
14+
import { printHarnessStats } from '../printers/harness-stats.printer.js';
15+
import { convertToHarnessStats } from '../converters/harness/harness.converter.js';
16+
import { printSpecStats } from '../printers/spec-stats.printer.js';
17+
import { convertToSpecStats } from '../converters/spec/spec.converter.js';
718

819
export function statsCommand(cliArgs: { [key: string]: string }) {
920
printWelcomeMessage();
@@ -15,22 +26,18 @@ export function statsCommand(cliArgs: { [key: string]: string }) {
1526

1627
const parsedOutput = parse(config);
1728
const componentStats = convertToComponentStats(parsedOutput.ngParselComponents);
18-
printComponentStats(componentStats);
19-
}
29+
const directiveStats = convertToDirectiveStats(parsedOutput.ngParselDirectives);
30+
const moduleStats = convertToModuleStats(parsedOutput.ngParselModules);
31+
const pipeStats = convertToPipeStats(parsedOutput.ngParselPipes);
32+
const serviceStats = convertToServiceStats(parsedOutput.ngParselServices);
33+
const harnessStats = convertToHarnessStats(parsedOutput.ngParselHarnesses);
34+
const specStats = convertToSpecStats(parsedOutput.ngParselSpecs);
2035

21-
// TODO: extract this into a dedicated file
22-
function printComponentStats(componentStats: NgParselComponentStats) {
23-
const tableData = [
24-
['Standalone', 'Module based', 'CVA', 'Total'],
25-
[componentStats.standalone, componentStats.moduleBased, componentStats.cva, componentStats.total],
26-
];
27-
28-
const tableConfig = {
29-
header: {
30-
alignment: 'center',
31-
content: 'COMPONENTS',
32-
},
33-
} as any;
34-
35-
console.log(table(tableData, tableConfig));
36+
printComponentStats(componentStats);
37+
printDirectiveStats(directiveStats);
38+
printModuleStats(moduleStats);
39+
printPipeStats(pipeStats);
40+
printServiceStats(serviceStats);
41+
printHarnessStats(harnessStats);
42+
printSpecStats(specStats);
3643
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { convertToComponentStats } from './component.converter.js';
2+
import { NgParselOutputType } from '../../parser/shared/model/types.model.js';
3+
4+
describe('Component converter', () => {
5+
it('it should return the correct stats for a given NgParselComponent array', () => {
6+
const componentStats = [
7+
{
8+
type: NgParselOutputType.COMPONENT,
9+
filePath: '',
10+
className: 'first',
11+
selector: 'first',
12+
standalone: true,
13+
cva: true,
14+
inputs: [],
15+
outputs: [],
16+
implementation: '',
17+
template: '',
18+
styles: '',
19+
methodsPublicExplicit: [],
20+
},
21+
{
22+
type: NgParselOutputType.COMPONENT,
23+
filePath: '',
24+
className: 'second',
25+
selector: 'second',
26+
standalone: false,
27+
cva: true,
28+
inputs: [],
29+
outputs: [],
30+
implementation: '',
31+
template: '',
32+
styles: '',
33+
methodsPublicExplicit: [],
34+
},
35+
{
36+
type: NgParselOutputType.COMPONENT,
37+
filePath: '',
38+
className: 'third',
39+
selector: 'third',
40+
standalone: true,
41+
cva: false,
42+
inputs: [],
43+
outputs: [],
44+
implementation: '',
45+
template: '',
46+
styles: '',
47+
methodsPublicExplicit: [],
48+
},
49+
];
50+
51+
const expectedStats = {
52+
standalone: 2,
53+
moduleBased: 1,
54+
cva: 2,
55+
total: 3,
56+
};
57+
58+
const stats = convertToComponentStats(componentStats);
59+
60+
expect(expectedStats).toEqual(stats);
61+
});
62+
});

src/converters/component.converter.ts renamed to src/converters/component/component.converter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgParselComponent } from '../parser/component/component.model.js';
1+
import { NgParselComponent } from '../../parser/component/component.model.js';
22

33
export interface NgParselComponentStats {
44
standalone: number;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { convertToDirectiveStats } from './directive.component.js';
2+
import { NgParselOutputType } from '../../parser/shared/model/types.model.js';
3+
4+
describe('Directive converter', () => {
5+
it('should convert to directive stats', () => {
6+
const directiveStats = [
7+
{
8+
type: NgParselOutputType.DIRECTIVE,
9+
filePath: '',
10+
className: 'first',
11+
selector: 'first',
12+
standalone: true,
13+
inputs: [],
14+
outputs: [],
15+
implementation: '',
16+
methodsPublicExplicit: [],
17+
},
18+
{
19+
type: NgParselOutputType.DIRECTIVE,
20+
filePath: '',
21+
className: 'second',
22+
selector: 'second',
23+
standalone: false,
24+
inputs: [],
25+
outputs: [],
26+
implementation: '',
27+
methodsPublicExplicit: [],
28+
},
29+
{
30+
type: NgParselOutputType.DIRECTIVE,
31+
filePath: '',
32+
className: 'third',
33+
selector: 'third',
34+
standalone: true,
35+
inputs: [],
36+
outputs: [],
37+
implementation: '',
38+
methodsPublicExplicit: [],
39+
},
40+
];
41+
42+
const expectedStats = {
43+
standalone: 2,
44+
moduleBased: 1,
45+
total: 3,
46+
};
47+
48+
const stats = convertToDirectiveStats(directiveStats);
49+
50+
expect(expectedStats).toEqual(stats);
51+
});
52+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { NgParselDirective } from '../../parser/directive/directive.model.js';
2+
3+
export interface NgParselDirectiveStats {
4+
standalone: number;
5+
moduleBased: number;
6+
total: number;
7+
}
8+
9+
export function convertToDirectiveStats(directiveStats: NgParselDirective[]): NgParselDirectiveStats {
10+
return directiveStats.reduce(
11+
(acc: NgParselDirectiveStats, component) => {
12+
if (component.standalone) {
13+
acc.standalone = acc.standalone + 1;
14+
} else {
15+
acc.moduleBased = acc.moduleBased + 1;
16+
}
17+
acc.total = acc.total + 1;
18+
return acc;
19+
},
20+
{
21+
standalone: 0,
22+
moduleBased: 0,
23+
total: 0,
24+
}
25+
);
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { NgParselOutputType } from '../../parser/shared/model/types.model.js';
2+
import { convertToHarnessStats } from './harness.converter.js';
3+
4+
describe('Harness converter', () => {
5+
it('should convert to module stats', () => {
6+
const harnessStats = [
7+
{
8+
className: 'first',
9+
type: NgParselOutputType.DIRECTIVE,
10+
filePath: '',
11+
methodsPublicExplicit: [],
12+
},
13+
{
14+
className: 'second',
15+
type: NgParselOutputType.MODULE,
16+
filePath: '',
17+
methodsPublicExplicit: [],
18+
},
19+
{
20+
className: 'third',
21+
type: NgParselOutputType.MODULE,
22+
filePath: '',
23+
methodsPublicExplicit: [],
24+
},
25+
];
26+
27+
const expectedStats = {
28+
total: 3,
29+
};
30+
31+
const stats = convertToHarnessStats(harnessStats);
32+
33+
expect(expectedStats).toEqual(stats);
34+
});
35+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgParselHarness } from '../../parser/harness/harness.model.js';
2+
3+
export interface NgParselHarnessStats {
4+
total: number;
5+
}
6+
7+
export function convertToHarnessStats(moduleStats: NgParselHarness[]): NgParselHarnessStats {
8+
return {
9+
total: moduleStats.length,
10+
};
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { NgParselOutputType } from '../../parser/shared/model/types.model.js';
2+
import { convertToModuleStats } from './module.converter.js';
3+
4+
describe('Module converter', () => {
5+
it('should convert to module stats', () => {
6+
const moduleStats = [
7+
{
8+
className: 'first',
9+
type: NgParselOutputType.DIRECTIVE,
10+
filePath: '',
11+
imports: [],
12+
exports: [],
13+
declarations: [],
14+
providers: [],
15+
bootstrap: [],
16+
},
17+
{
18+
className: 'second',
19+
type: NgParselOutputType.MODULE,
20+
filePath: '',
21+
imports: [],
22+
exports: [],
23+
declarations: [],
24+
providers: [],
25+
bootstrap: [],
26+
},
27+
{
28+
className: 'third',
29+
type: NgParselOutputType.MODULE,
30+
filePath: '',
31+
imports: [],
32+
exports: [],
33+
declarations: [],
34+
providers: [],
35+
bootstrap: [],
36+
},
37+
];
38+
39+
const expectedStats = {
40+
total: 3,
41+
};
42+
43+
const stats = convertToModuleStats(moduleStats);
44+
45+
expect(expectedStats).toEqual(stats);
46+
});
47+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgParselModule } from '../../parser/module/module.model.js';
2+
3+
export interface NgParselModuleStats {
4+
total: number;
5+
}
6+
7+
export function convertToModuleStats(moduleStats: NgParselModule[]): NgParselModuleStats {
8+
return {
9+
total: moduleStats.length,
10+
};
11+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { NgParselOutputType } from '../../parser/shared/model/types.model.js';
2+
import { convertToPipeStats } from './pipes.component.js';
3+
4+
describe('Directive converter', () => {
5+
it('should convert to directive stats', () => {
6+
const pipeStats = [
7+
{
8+
type: NgParselOutputType.PIPE,
9+
filePath: '',
10+
className: 'first',
11+
name: 'first',
12+
standalone: true,
13+
pure: true,
14+
implementation: '',
15+
},
16+
{
17+
type: NgParselOutputType.PIPE,
18+
filePath: '',
19+
className: 'second',
20+
name: 'second',
21+
standalone: true,
22+
pure: false,
23+
implementation: '',
24+
},
25+
{
26+
type: NgParselOutputType.PIPE,
27+
filePath: '',
28+
className: 'third',
29+
name: 'third',
30+
standalone: false,
31+
pure: true,
32+
implementation: '',
33+
},
34+
];
35+
36+
const expectedStats = {
37+
standalone: 2,
38+
pure: 2,
39+
total: 3,
40+
};
41+
42+
const stats = convertToPipeStats(pipeStats);
43+
44+
expect(expectedStats).toEqual(stats);
45+
});
46+
});

0 commit comments

Comments
 (0)