Skip to content
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

feat: implement list view for cli #49

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"klona": "2.0.6",
"magicast": "0.3.5",
"pathe": "2.0.3",
"table": "6.9.0",
"valibot": "1.0.0-rc.3"
},
"devDependencies": {
Expand Down
62 changes: 62 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions src/cli/action/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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 }) => {
// v1 module should be upgraded to v2 at the entry point of CLI
const todoModule = getTodoModuleV2(await core.readTodoModule());

if (!todoModule) {
throw new Error("Unsupported todo file version");
}

const todoTable = generateTodoInfo(todoModule).map((row) => {
return [
row.rule,
row.violations.files,
row.violations.total,
row.autoFix ? "✔" : "",
];
});

if (todoTable.length === 0) {
logger.success("No rules found in the todo file! Good job!");
return;
}

const tableData = [["Rule", "Files", "Violations", "AutoFix"], ...todoTable];

logger.log.raw(table(tableData));
return;
});

const getTodoModuleV2 = (module: TodoModuleLike): TodoModuleV2 | null => {
return TodoModuleV2Handler.isVersion(module) ? module : null;
};

type TodoInfo = {
autoFix: boolean;
rule: string;
violations: {
files: number;
total: number;
};
};

const generateTodoInfo = (todoModule: TodoModuleV2): TodoInfo[] => {
const todoInfo: TodoInfo[] = [];

for (const [rule, { autoFix, violations }] of Object.entries(
todoModule.todo,
)) {
const violatedFiles = Object.keys(violations).length;
const totalViolations = Object.values(violations).reduce(
(sum, count) => sum + count,
0,
);

todoInfo.push({
autoFix,
rule,
violations: {
files: violatedFiles,
total: totalViolations,
},
});
}

return todoInfo;
};
15 changes: 13 additions & 2 deletions src/cli/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { OperationLimit } from "../operation/types";
import { safeTryNumber } from "../utils/number";

type CLIContext = {
mode: "correct" | "generate";
mode: "correct" | "generate" | "list";
operation: CLIOperation;
/**
* Path to the todo file relative to the current working directory
Expand All @@ -23,6 +23,7 @@ type Input = {

mode: {
correct: boolean;
list: boolean;
};
operation: CLIOperationInput;
todoFileAbsolutePath: string;
Expand All @@ -31,8 +32,18 @@ type Input = {
export const resolveCLIContext = (input: Input): CLIContext => {
const todoFilePath = relative(input.cwd, input.todoFileAbsolutePath);

const mode = (() => {
if (input.mode.correct) {
return "correct";
}
if (input.mode.list) {
return "list";
}
return "generate";
})();

return {
mode: input.mode.correct ? "correct" : "generate",
mode,
operation: resolveCLIOperation(input.operation),
todoFilePath: todoFilePath,
};
Expand Down
14 changes: 14 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ESLintTodoCore } from "../index";
import { runAction } from "./action";
import { deleteRuleAction } from "./action/delete-rule";
import { genAction } from "./action/gen";
import { listAction } from "./action/list";
import { selectRulesToFixAction } from "./action/select-rule";
import { updateAction } from "./action/update";
import { resolveCLIContext } from "./context";
Expand Down Expand Up @@ -47,6 +48,13 @@ const cli = defineCommand({
required: false,
type: "boolean",
},
"list": {
alias: "l",
default: false,
description: "See violations in the todo file with table format",
required: false,
type: "boolean",
},

// operation options
"allow-partial-selection": {
Expand Down Expand Up @@ -115,6 +123,7 @@ const cli = defineCommand({
cwd: cliCwd,
mode: {
correct: args.correct,
list: args.list,
},
operation: {
allowPartialSelection: args["allow-partial-selection"],
Expand All @@ -127,6 +136,11 @@ const cli = defineCommand({

await runAction(updateAction, { consola, options });

if (context.mode === "list") {
await runAction(listAction, { consola, options });
return;
}

if (context.mode === "generate") {
await runAction(genAction, { consola, options });
consola.success(`ESLint todo file generated at ${context.todoFilePath}!`);
Expand Down