-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc78ce0
commit 5e7093d
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,66 @@ | ||
import { table } from "table"; | ||
|
||
import type { TodoModuleLike } from "../../todofile/types"; | ||
import type { TodoModuleV2 } from "../../todofile/v2"; | ||
|
||
import { TodoModuleV2Handler } from "../../todofile/v2"; | ||
import { defineAction } from "./index"; | ||
|
||
export const listAction = defineAction(async ({ core, logger }) => { | ||
const todoModule = getTodoModuleV2(await core.readTodoModule()); | ||
|
||
if (todoModule === false) { | ||
throw new Error("Unsupported todo file version"); | ||
} | ||
|
||
const todoTable = generateTodoTable(todoModule); | ||
|
||
if (todoTable.length === 0) { | ||
logger.success("No rules found in the todo file! Good job!"); | ||
return; | ||
} | ||
|
||
const tableData = [ | ||
["Rule", "Files", "Violations", "AutoFixable"], | ||
...todoTable, | ||
]; | ||
|
||
logger.log.raw(table(tableData)); | ||
|
||
return; | ||
}); | ||
|
||
const getTodoModuleV2 = (module: TodoModuleLike): false | TodoModuleV2 => { | ||
if (TodoModuleV2Handler.isVersion(module)) { | ||
return module; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
type TableFormat = [ | ||
ruleId: string, | ||
violatedFiles: number, | ||
totalViolations: number, | ||
autoFixableEmoji: string, | ||
]; | ||
|
||
const generateTodoTable = (todoModule: TodoModuleV2): TableFormat[] => { | ||
const tableData: TableFormat[] = []; | ||
|
||
for (const [ruleId, { autoFix, violations }] of Object.entries( | ||
todoModule.todo, | ||
)) { | ||
const violatedFiles = Object.keys(violations).length; | ||
const totalViolations = Object.values(violations).reduce( | ||
(sum, count) => sum + count, | ||
0, | ||
); | ||
|
||
const autoFixEmoji = autoFix ? "✓" : ""; | ||
|
||
tableData.push([ruleId, violatedFiles, totalViolations, autoFixEmoji]); | ||
} | ||
|
||
return tableData; | ||
}; |
This file contains 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