How yanquankun/forbid-lint works
Project Type and Purpose
• This is a Node.js-based CLI tool meant to integrate with typical front-end development workflows. It primarily works by “forbidding” changes to certain lint configuration files during git operations (via husky pre-commit hooks).
• The tool offers a couple of CLI commands (e.g., “forbid-lint init” to initialize configuration and “forbid-lint check” to inspect staged changes) and supports both global and project-level installation.
• In essence, it manages file integrity for lint configurations by scanning for modifications and erroring out on forbidden changes.
File Structure
• Top-level project files: Include configuration files (e.g., .eslintrc, tsconfig*, package.json, etc.) and scripts (gulpfile.ts, jest.config.ts) used for development, testing, and publishing.
• The "src" folder contains the core implementation:
▪ src/index.ts – the main entry point.
▪ src/lib – houses helper libraries; for example, inquirer.ts deals with command-line prompts.
▪ src/task – has modularized tasks like:
• checkTask.ts: performs checks for modifications in the lint files.
• forbidden-related tasks such as huskyTask.ts and initTask.ts.
• types.ts defining structure for tasks.
▪ src/template – stores default configuration templates and husky hook template files.
▪ src/utils – supports file and directory operations, logging, error catching, etc.
• A “demo” folder shows an example usage scenario, including its own husky configuration (with pre-commit hooks) and a simplified setup to illustrate how the plugin works.
• Additional scripts in the “scripts” folder manage publishing and utility functions.
Key Components
a. CLI Entry Point:
• CLI interface as the user’s starting component. This would be the “forbid-lint” command which resolves to src/index.ts.
b. Task Manager / Executor:
• A “Task/Command Handler” component that dispatches tasks based on CLI commands. Tasks here include initialization, checking file modifications, and possibly configuration validation.
c. Configuration Manager:
• A component dedicated to handling configuration files (i.e., reading and writing “.forbidrc.json”) where forbidden files are specified.
d. Integration with Husky:
• Connection where the CLI is integrated with husky’s pre-commit hook. This integration ensures “forbid-lint check” is called during git commit procedures.
e. Utility Modules:
• Support modules for IO, file system operations, logging, prompting (via inquirer), error handling, etc.
f. Template Repository:
g. Demo Application:
• A reference to the “demo” folder that models an example project integration.
Relationships and Data Flow
• Client (developer using CLI) → CLI Entry Point: User enters a CLI command.
• CLI passes the command to Task Manager → Task Manager selects an appropriate module (initTask, checkTask, etc.).
• Task Manager interacts with Configuration Manager: It reads default config if none provided or generates a configuration file.
• Task Manager uses Utility Modules for file checks (e.g., reading files from the project root) and logs results.
• For the “check” command, the chain includes: scanning file changes → validating changes against listed forbidden lint files → triggering success/failure responses.
• Integration with external systems (i.e., Git/Husky):
• The pre-commit hook in husky (set up using the husky template from “src/template/husky_tpl”) calls “forbid-lint check”, linking back into the Task Manager.
Architectural Patterns and Important Technologies
• Modular design: Each type of task (init, check, etc.) is separated into its own module.
• CLI pattern: The tool leverages a command-line interface using modules like inquirer for interactive prompts.
• Integration with a Git workflow by automatically setting up Husky hooks.
• Use of Node.js’s file system operations and logging utilities encapsulated in the utils folder.