-
Notifications
You must be signed in to change notification settings - Fork 10
feat(tracker): add run command #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
84e9f43
feat(tracker): add tracker init command
eduardoRoth 028bfa5
fix(tracker): update code based on biome ci
eduardoRoth e7c3b19
fix(tracker): update tests for windows paths
eduardoRoth 08fe76b
Merge branch 'refs/heads/main' into feat/eroth/tracker-run
eduardoRoth 5cd47dd
Merge branch 'refs/heads/main' into feat/eroth/tracker-run
eduardoRoth bd75b7d
feat(tracker): added run command
eduardoRoth 2766875
chore(tracker): update ignorePatterns default value
eduardoRoth 85cee57
chore(tracker): remove deprecated jsToPairs property
eduardoRoth 00ee03a
chore(tracker): fix biome import order
eduardoRoth 8fb6843
chore(tracker): update readme with tracker run info
eduardoRoth ef2f3a8
fix(tracker): show warning when no files are found
eduardoRoth a20b649
chore(): format, lint, biome ci
eduardoRoth 7b88ff9
feat(tracker): use glob package
eduardoRoth 85c4100
chore(tracker): remove empty finally
eduardoRoth 98c9f52
chore(tracker): add note about globSync
eduardoRoth 9cff0d4
chore(tracker): replace global with node cwd
eduardoRoth 10edd76
fix(tracker): update tests to handle Windows backslash
eduardoRoth 9e8fd5c
chore(tracker): formatting
eduardoRoth 13eb3dd
Merge branch 'main' into feat/eroth/tracker-run
eduardoRoth 3234d1c
chore(): update package-lock
eduardoRoth e33e99b
chore(): apply pr comments
eduardoRoth d5bb781
fix(tracker): update tests to vitest
eduardoRoth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
| import { spawnSync } from 'node:child_process'; | ||
| import { cwd } from 'node:process'; | ||
| import { Command, Flags, ux } from '@oclif/core'; | ||
| import { Presets, SingleBar } from 'cli-progress'; | ||
| import ora from 'ora'; | ||
| import terminalLink from 'terminal-link'; | ||
| import { TRACKER_GIT_OUTPUT_FORMAT } from '../../config/constants.js'; | ||
| import { getErrorMessage, isErrnoException } from '../../service/error.svc.js'; | ||
| import { | ||
| type CategoryStatsResult, | ||
| type FilesStats, | ||
| type GitLastCommit, | ||
| getConfiguration, | ||
| getFileStats, | ||
| getFilesFromCategory, | ||
| getRootDir, | ||
| INITIAL_FILES_STATS, | ||
| saveResults, | ||
| } from '../../service/tracker.svc.js'; | ||
|
|
||
| export default class Run extends Command { | ||
| static override description = 'Run the tracker'; | ||
| static enableJsonFlag = false; | ||
| static override examples = [ | ||
| '<%= config.bin %> <%= command.id %>', | ||
| '<%= config.bin %> <%= command.id %> -d tracker-configuration', | ||
| '<%= config.bin %> <%= command.id %> -d tracker -f settings.json', | ||
| ]; | ||
|
|
||
| static override flags = { | ||
| configDir: Flags.string({ | ||
| char: 'd', | ||
| description: 'Directory where the tracker configuration file resides', | ||
| default: 'hd-tracker', | ||
| }), | ||
| configFile: Flags.string({ | ||
| char: 'f', | ||
| description: 'Filename for the tracker configuration file', | ||
| default: 'config.json', | ||
| }), | ||
| }; | ||
|
|
||
| public async run(): Promise<void> { | ||
| const { flags } = await this.parse(Run); | ||
| const { configDir, configFile } = flags; | ||
|
|
||
| try { | ||
| const rootDir = getRootDir(cwd()); | ||
| const confSpinner = ora('Searching for configuration file').start(); | ||
| const { categories, ignorePatterns, outputDir } = getConfiguration(rootDir, configDir, configFile); | ||
|
|
||
| confSpinner.text = `Configuration file ${configFile} found in ${rootDir}/${configDir}`; | ||
|
|
||
| const categoriesTotal = Object.keys(categories).length; | ||
| if (categoriesTotal > 0) { | ||
| confSpinner.stopAndPersist({ | ||
| text: ux.colorize('green', `Found ${categoriesTotal} categor${categoriesTotal === 1 ? 'y' : 'ies'}`), | ||
| symbol: ux.colorize('green', `\u2714`), | ||
| }); | ||
| } else { | ||
| confSpinner.stopAndPersist({ | ||
| text: ux.colorize('red', `No categories found, please check your configuration file`), | ||
| symbol: ux.colorize('red', `\u2716`), | ||
| }); | ||
| return; | ||
| } | ||
| this.log(''); | ||
| const results = Object.entries(categories).reduce((acc: CategoryStatsResult[], [name, category]) => { | ||
| const loadingFilesSpinner = ora(`[${ux.colorize('blueBright', name)}] Getting files`).start(); | ||
| const fileProgress = new SingleBar( | ||
| { | ||
| format: `${ux.colorize('green', '{bar}')} | {value}/{total} | {name}`, | ||
| clearOnComplete: false, | ||
| fps: 100, | ||
| hideCursor: true, | ||
| }, | ||
| Presets.shades_grey, | ||
| ); | ||
|
|
||
| const fileTypes: Set<string> = new Set(); | ||
| const categoryFilesWithError: string[] = []; | ||
|
|
||
| const files = getFilesFromCategory(category, { | ||
| rootDir, | ||
| ignorePatterns, | ||
| }); | ||
|
|
||
| if (files.length === 0) { | ||
| loadingFilesSpinner.stopAndPersist({ | ||
| text: ux.colorize('yellow', `[${ux.colorize('yellowBright', name)}] Found 0 files`), | ||
| symbol: ux.colorize('yellowBright', `\u26A0`), | ||
| }); | ||
| this.log( | ||
| ux.colorize( | ||
| 'yellow', | ||
| `Please check your configuration [includes] property so it matches folders in your project directory`, | ||
| ), | ||
| ); | ||
| this.log(''); | ||
| return acc; | ||
| } | ||
|
|
||
| loadingFilesSpinner.stopAndPersist({ | ||
| text: ux.colorize('green', `[${ux.colorize('blueBright', name)}] Found ${files.length} files`), | ||
| symbol: ux.colorize('green', `\u2714`), | ||
| }); | ||
| fileProgress.start(files.length, 1); | ||
|
|
||
| const fileResults = files.reduce((result: FilesStats, file, currentIndex, array) => { | ||
| const fileStats = getFileStats(file, { | ||
| rootDir, | ||
| }); | ||
| if (currentIndex === array.length - 1) { | ||
| fileProgress.update({ | ||
| name: ux.colorize('green', 'All files were processed successfully'), | ||
| }); | ||
| fileProgress.stop(); | ||
| } else { | ||
| fileProgress.increment({ | ||
| name: file, | ||
| }); | ||
| } | ||
|
|
||
| if ('error' in fileStats) { | ||
| categoryFilesWithError.push(file); | ||
| fileProgress.increment(); | ||
| return result; | ||
| } else { | ||
| fileTypes.add(fileStats.fileType); | ||
| return { | ||
| total: fileStats.total + result.total, | ||
| block: fileStats.block + result.block, | ||
| blockEmpty: fileStats.blockEmpty + result.blockEmpty, | ||
| comment: fileStats.comment + result.comment, | ||
| empty: fileStats.empty + result.empty, | ||
| mixed: fileStats.mixed + result.mixed, | ||
| single: fileStats.single + result.single, | ||
| source: fileStats.source + result.source, | ||
| todo: fileStats.todo + result.todo, | ||
| }; | ||
| } | ||
| }, INITIAL_FILES_STATS); | ||
|
|
||
| this.log(''); | ||
eduardoRoth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| acc.push({ | ||
| name, | ||
| totals: fileResults, | ||
| errors: categoryFilesWithError, | ||
| fileTypes: Array.from(fileTypes), | ||
| }); | ||
| return acc; | ||
| }, []); | ||
|
|
||
| this.log(''); | ||
eduardoRoth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const spinner = ora('Saving results').start(); | ||
| const resultsLink = saveResults(results, rootDir, outputDir, this.fetchGitLastCommit(rootDir)); | ||
| spinner.stopAndPersist({ | ||
| text: ux.colorize('green', 'Tracker results saved!\n'), | ||
| symbol: ux.colorize('green', '\u2713'), | ||
| }); | ||
|
|
||
| this.log(`${ux.colorize('blueBright', terminalLink(`Open Tracker Results`, `file://${resultsLink}`))}\n`); | ||
| } catch (err) { | ||
| if (err instanceof Error) { | ||
| this.error(ux.colorize('red', err.message)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Fetches Git last commit | ||
| */ | ||
| private fetchGitLastCommit(rootDir?: string): GitLastCommit { | ||
| const logParameters = ['log', `-1`, `--format=${TRACKER_GIT_OUTPUT_FORMAT}`, ...(rootDir ? ['--', rootDir] : [])]; | ||
|
|
||
| const logProcess = spawnSync('git', logParameters, { | ||
| encoding: 'utf-8', | ||
| }); | ||
|
|
||
| if (logProcess.error) { | ||
| if (isErrnoException(logProcess.error)) { | ||
| if (logProcess.error.code === 'ENOENT') { | ||
| this.error('Git command not found. Please ensure git is installed and available in your PATH.'); | ||
| } | ||
| this.error(`Git command failed: ${getErrorMessage(logProcess.error)}`); | ||
| } | ||
| this.error(`Git command failed: ${getErrorMessage(logProcess.error)}`); | ||
| } | ||
|
|
||
| if (logProcess.status !== 0) { | ||
| this.error(`Git command failed with status ${logProcess.status}: ${logProcess.stderr}`); | ||
| } | ||
|
|
||
| if (!logProcess.stdout) { | ||
| return { | ||
| hash: '', | ||
| timestamp: '', | ||
| author: '', | ||
| }; | ||
| } | ||
|
|
||
| return logProcess.stdout | ||
| .split('\n') | ||
| .filter(Boolean) | ||
| .reduce( | ||
| (_acc, curr) => { | ||
| const [hash, author, timestamp] = curr.replace(/^"(.*)"$/, '$1').split('|'); | ||
| return { | ||
| timestamp, | ||
| hash, | ||
| author, | ||
| }; | ||
| }, | ||
| { | ||
| hash: '', | ||
| timestamp: '', | ||
| author: '', | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.