Skip to content

Commit

Permalink
feat: 🎸 stats
Browse files Browse the repository at this point in the history
  • Loading branch information
nivekcode committed Jul 21, 2024
1 parent 3b0c055 commit b1c4eec
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start:version": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts --version",
"start:help": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts --help",
"start:parse": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts parse --src './test-spa'",
"start:stats": "node --loader ts-node/esm src/bin/ng-parsel.bin.ts stats --src './test-spa'",
"build": "tsc && npm run copy:readme",
"copy:readme": "copyfiles ./README.md ./dist",
"lint": "eslint src/**/*.ts",
Expand Down
26 changes: 5 additions & 21 deletions src/bin/ng-parsel.bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { Command } from 'commander';
import { dirname, resolve } from 'path';
import { cosmiconfigSync } from 'cosmiconfig';

import { mergeOptionalConfigWithDefaults } from '../config/config.helper.js';
import { CONFIG_DEFAULT_VALUES } from '../config/config.model.js';
import { writeJson } from '../utils/write.util.js';
import { parse } from '../parser/parser.js';
import { printWelcomeMessage } from '../utils/welcome.util.js';
import { parseCommand } from '../commands/parse.js';
import { statsCommand } from '../commands/stats.js';

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

program.version(
/*
Expand All @@ -37,8 +34,8 @@ program
.option('--specs', 'Parse Specs', true)
.option('-o, --out <string>', 'Output directory for generated files')
.option('--singleFile', 'Output in a single file')
.action((_srcGlob, options) => {
parseCommand(options);
.action((_srcGlob, cliArgs) => {
parseCommand(cliArgs);
});

program.command('init').action(() => {
Expand All @@ -55,21 +52,8 @@ program.command('init').action(() => {
program
.command('stats')
.option('-s, --src', 'Glob pattern to search for files')
.action(() => {
printWelcomeMessage();

const configObject = explorer.search();

// TODO: extract this
if (configObject) {
console.log(
chalk.cyan(
`ng-parsel: configuration found under ${configObject.filepath}.
Configuraton from config file will be used.`
)
);
parse(mergeOptionalConfigWithDefaults(configObject.config));
}
.action((_srcGlob, cliArgs) => {
statsCommand(cliArgs);
});

program.parse();
4 changes: 2 additions & 2 deletions src/commands/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export function parseCommand(cliArgs: { [key: string]: string }) {
// TODO: clean this up later
config['src'] = './test-spa';

const parseOutput = parse(config);
writeParsedOutputToDisk(config, parseOutput);
const parsedOutput = parse(config);
writeParsedOutputToDisk(config, parsedOutput);

console.log(chalk.magenta('===================================='));
}
Expand Down
16 changes: 16 additions & 0 deletions src/commands/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { printWelcomeMessage } from '../utils/welcome.util.js';
import { loadAndMergeConfig } from '../utils/config.util.js';
import { parse } from '../parser/parser.js';
import { convertToComponentStats } from '../converters/component.converter.js';

export function statsCommand(cliArgs: { [key: string]: string }) {
printWelcomeMessage();

const config = loadAndMergeConfig(cliArgs);

// TODO: clean this up later
config['src'] = './test-spa';

const parsedOutput = parse(config);
console.log(convertToComponentStats(parsedOutput.ngParselComponents));
}
33 changes: 33 additions & 0 deletions src/converters/component.converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NgParselComponent } from '../parser/component/component.model.js';

export interface NgParselComponentStats {
standalone: number;
moduleBased: number;
cva: number;
total: number;
}

export function convertToComponentStats(componentStats: NgParselComponent[]): NgParselComponentStats {
return componentStats.reduce(
(acc: NgParselComponentStats, component) => {
if (component.standalone) {
acc.standalone = acc.standalone + 1;
} else {
acc.moduleBased = acc.moduleBased + 1;
}

if (component.cva) {
acc.cva = acc.cva + 1;
}

acc.total = acc.total + 1;
return acc;
},
{
standalone: 0,
moduleBased: 0,
cva: 0,
total: 0,
}
);
}

0 comments on commit b1c4eec

Please sign in to comment.