Skip to content

Commit

Permalink
Merge pull request npm#16 from herodevs/tracker
Browse files Browse the repository at this point in the history
Implement HeroDevs tracker logic in this CLI
  • Loading branch information
jeremymwells authored Sep 15, 2023
2 parents 301b9f1 + 56fe832 commit cbba514
Show file tree
Hide file tree
Showing 25 changed files with 2,571 additions and 641 deletions.
2,374 changes: 1,743 additions & 631 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
"@oclif/core": "^2",
"@oclif/plugin-help": "^5",
"@oclif/plugin-plugins": "^3.2.0",
"chart.js": "^3.9.1",
"chart.js-image": "^6.1.3",
"chartjs-node-canvas": "^4.1.6",
"chartjs-plugin-autocolors": "^0.2.2",
"chartjs-plugin-datalabels": "^2.2.0",
"date-fns": "^2.30.0",
"git-last-commit": "^1.0.1",
"module-alias": "^2.2.3",
"shelljs": "^0.8.5"
"shelljs": "^0.8.5",
"sloc": "^0.2.1"
},
"devDependencies": {
"@oclif/test": "^2.4.4",
Expand Down Expand Up @@ -55,6 +62,9 @@
"topics": {
"committer": {
"description": "Committer actions"
},
"tracker": {
"description": "Track project progress"
}
}
},
Expand Down Expand Up @@ -106,5 +116,11 @@
"keywords": [
"oclif"
],
"types": "dist/index.d.ts"
"types": "dist/index.d.ts",
"prettier": {
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"printWidth": 100
}
}
19 changes: 19 additions & 0 deletions src/commands/tracker/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Args, Command, Flags } from '@oclif/core';
import { TrackerInit } from './init';
import { BaseCommand } from '../../shared';
import { TrackerRun } from './run';

export default class Tracker extends Command {
static description = 'Tracker info';

static examples = [`$ @herodevs/cli tracker`];

static flags = {};

static args = {
init: TrackerInit.args,
run: TrackerRun.args,
} as any;

async run(): Promise<void> {}
}
17 changes: 17 additions & 0 deletions src/commands/tracker/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Args, Command, Flags } from '@oclif/core';
import { initialize } from '../../shared/tracker/initialize';
import { getTheRootDirectory } from '../../shared/tracker/util';

export class TrackerInit extends Command {
static description = 'Initialize the tracker configuration';

static examples = ['<%= config.bin %> <%= command.id %>'];

static flags = {};

static args = {};

public async run(): Promise<void> {
initialize(getTheRootDirectory(global.process.cwd()));
}
}
48 changes: 48 additions & 0 deletions src/commands/tracker/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Args, Command, Flags } from '@oclif/core';
import { resolve } from 'path';
import {
createDataVizIn,
getData,
getTheRootDirectory,
readConfig,
saveResults,
} from '../../shared/tracker/util';
import { processConfig } from '../../shared/tracker/process-config';
import { ChartConfig } from '../../shared/tracker/models/chart-config';

export class TrackerRun extends Command {
static description = 'Run the tracker';

static examples = ['<%= config.bin %> <%= command.id %>'];

static flags = {
root: Flags.string({ char: 'r', description: 'root dir of the project' }),
config: Flags.string({ char: 'c', description: 'path to config file' }),
chart: Flags.custom<ChartConfig>({
description: 'chart configuration',
})(),
};

static args = {};

public async run(): Promise<void> {
const { flags } = await this.parse(TrackerRun);

const localRootDir = getTheRootDirectory(global.process.cwd());

const rootDirectory = flags.root ? resolve(flags.root) : localRootDir;
const config = readConfig(localRootDir, flags.config);

const results = await processConfig(config, rootDirectory);

saveResults(localRootDir, config.outputDir, results);

const allData = getData(localRootDir, config.outputDir);

const parentDir = resolve(localRootDir, config.outputDir);

const chartConfig = new ChartConfig(flags.chart);

await createDataVizIn(chartConfig, parentDir, allData);
}
}
20 changes: 20 additions & 0 deletions src/shared/tracker/default-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Config } from './models/config';

const defaultConfig: Config = {
categories: {
legacy: {
fileTypes: ['js', 'ts', 'html', 'css', 'scss', 'less'],
includes: ['./legacy'],
jsTsPairs: 'js',
},
modern: {
fileTypes: ['ts', 'html', 'css', 'scss', 'less'],
includes: ['./modern'],
jsTsPairs: 'ts',
},
},
ignorePatterns: ['node_modules'],
outputDir: 'hd-tracker',
};

export default defaultConfig;
13 changes: 13 additions & 0 deletions src/shared/tracker/initialize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { join } from "path";
import { existsSync, writeFileSync, mkdirSync } from "fs";

import defaultConfig from "./default-config";

export function initialize(rootDir: string): void {
const output = JSON.stringify(defaultConfig, null, 2);
const dir = join(rootDir, "hd-tracker");
if (!existsSync(dir)) {
mkdirSync(dir);
}
writeFileSync(join(dir, "config.json"), output);
}
5 changes: 5 additions & 0 deletions src/shared/tracker/models/aggregate-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TotalResult } from './total-result';

export interface AggregateResult extends TotalResult {
fileType: string;
}
8 changes: 8 additions & 0 deletions src/shared/tracker/models/category-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AggregateResult } from './aggregate-result';
import { TotalResult } from './total-result';

export interface CategoryResult {
name: string;
totals: TotalResult;
fileTypes: AggregateResult[];
}
10 changes: 10 additions & 0 deletions src/shared/tracker/models/category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Category extends CategoryDefinition {
name: string;
}

export interface CategoryDefinition {
fileTypes: string[];
includes: string[];
excludes?: string[];
jsTsPairs?: 'js' | 'ts' | 'ignore';
}
86 changes: 86 additions & 0 deletions src/shared/tracker/models/chart-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
export class ChartConfig {
private _perCategoryAndFileType = false;
private _perCategoryTotals = true;
private _width = 1200;
private _height = 800;
private _bg = 'white';
private _title = 'Migration Progress LOC';
private _xAxisLabel = 'Date';
private _yAxisLabel = 'Totals';
private _overwrite = true;
private _outFile = 'viz.png';

get perCategoryAndFileType(): boolean {
// if false, use false; false !== undefined
return this._cliOptions.perCategoryAndFileType !== undefined ?
this._cliOptions.perCategoryAndFileType :
this._perCategoryAndFileType;
}

get perCategoryTotals(): boolean {
// if false, use false; false !== undefined
return this._cliOptions.perCategoryTotals !== undefined ?
this._cliOptions.perCategoryTotals:
this._perCategoryTotals;
}

get width(): number {
// if set and non-numeric
if (this._cliOptions.width !== undefined && isNaN(Number(this._cliOptions.width))) {
throw Error('--chart.width must be a number');
}
// if unset or numeric
return this._cliOptions.width !== undefined ?
this._cliOptions.width:
this._width;
}

get height(): number {
// if set and non-numeric
if (this._cliOptions.height !== undefined && isNaN(Number(this._cliOptions.height))) {
throw Error('--chart.height must be a number');
}
// if unset or numeric
return this._cliOptions.height !== undefined ?
this._cliOptions.height:
this._height;
}

get bg(): string {
return this._cliOptions.bg ?
this._cliOptions.bg:
this._bg;
}

get title(): string {
return this._cliOptions.title ?
this._cliOptions.title:
this._title;
}

get xAxisLabel(): string {
return this._cliOptions.xAxisLabel ?
this._cliOptions.xAxisLabel:
this._xAxisLabel;
}

get yAxisLabel(): string {
return this._cliOptions.yAxisLabel ?
this._cliOptions.yAxisLabel:
this._yAxisLabel;
}

get overwrite(): boolean {
return this._cliOptions.overwrite !== undefined ?
this._cliOptions.overwrite:
this._overwrite;
}

get outFile(): string {
return this._cliOptions.outFile ?
this._cliOptions.outFile:
this._outFile;
}

constructor(private _cliOptions: ChartConfig = {} as ChartConfig) { }
}
7 changes: 7 additions & 0 deletions src/shared/tracker/models/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { CategoryDefinition } from './category';

export interface Config {
categories: { [key: string]: CategoryDefinition };
ignorePatterns?: string[];
outputDir: string;
}
6 changes: 6 additions & 0 deletions src/shared/tracker/models/file-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Result } from './result';

export interface FileResult extends Result {
fileType: string;
path: string;
}
7 changes: 7 additions & 0 deletions src/shared/tracker/models/process-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { CategoryResult } from './category-result';

export interface ProcessResult {
timestamp: string;
hash: string;
categories: CategoryResult[];
}
11 changes: 11 additions & 0 deletions src/shared/tracker/models/result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface Result {
total: number;
source: number;
comment: number;
single: number;
block: number;
mixed: number;
empty: number;
todo: number;
blockEmpty: number;
}
5 changes: 5 additions & 0 deletions src/shared/tracker/models/total-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Result } from './result';

export interface TotalResult extends Result {
fileCount: number;
}
4 changes: 4 additions & 0 deletions src/shared/tracker/models/viz-dataset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type VizDataset = {
label: string;
data: number[];
};
6 changes: 6 additions & 0 deletions src/shared/tracker/models/viz-labels-datasets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { VizDataset } from './viz-dataset'

export type VizLabelsDatasets = {
labels: string[],
datasets: VizDataset[],
}
Loading

0 comments on commit cbba514

Please sign in to comment.