konsistent-config
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesekonsistent Configuration
konsistent配置
Create or modify a file that enforces structural conventions for the project. The CLI checks filesystem structure and TypeScript exports/imports — it is not a style linter.
konsistent.jsonkonsistent创建或修改文件,为项目强制执行结构规范。 CLI检查文件系统结构和TypeScript导出/导入——它不是代码风格检查器。
konsistent.jsonkonsistentPrerequisites
前提条件
Check if the package is already installed:
konsistent- available in root
konsistentpackage.json - directory exists
node_modules/konsistent - a script exists in
konsistentpackage.json
If not, the CLI must be installed first. Use the project's package manager. For example, with PNPM:
konsistentbash
pnpm add konsistent --save-devThen, ensure has a script which invokes the CLI. At a minimum:
package.jsonkonsistentkonsistent "scripts": {
"konsistent": "konsistent"
}Once the package has been successfully installed, proceed to the primary workflow.
检查是否已安装包:
konsistent- 根目录中存在
package.jsonkonsistent - 目录存在
node_modules/konsistent - 中存在
package.json脚本konsistent
如果未安装,必须先安装 CLI。使用项目的包管理器,例如使用PNPM:
konsistentbash
pnpm add konsistent --save-dev然后,确保中有一个调用 CLI的脚本。至少需要:
package.jsonkonsistentkonsistent "scripts": {
"konsistent": "konsistent"
}成功安装包后,进入主工作流程。
Workflow
工作流程
- Check if already exists at the project root.
konsistent.json - Read to confirm the authoritative shape.
node_modules/konsistent/konsistent.schema.json - Read the relevant docs in (see References below) before making changes.
node_modules/konsistent/docs/ - If exists: read it, then add/remove/update conventions as requested by the user.
konsistent.json - If does not exist: create it at the project root.
konsistent.json
Before creating a new config, or if the user has not provided any specific requests for editing an existing config, you must:
- Explore the user's codebase to understand existing structure and naming patterns.
- Refer to for what to look out for.
node_modules/konsistent/docs/guides/exploring-codebases.md
When modifying an existing config:
- Preserve all conventions not related to the user's request.
- Preserve existing ,
name, anddescriptionvalues unless asked to change them.severity - When adding conventions, append to the array.
conventions - When the user reports violations, read the existing config and the violating files to determine whether to fix the config or advise fixing the code.
- 检查项目根目录是否已存在。
konsistent.json - 读取以确认权威结构。
node_modules/konsistent/konsistent.schema.json - 在进行更改前,阅读中的相关文档(见下文参考资料)。
node_modules/konsistent/docs/ - 如果已存在:读取该文件,然后根据用户请求添加/删除/更新规范。
konsistent.json - 如果不存在:在项目根目录创建该文件。
konsistent.json
在创建新配置,或用户未提供编辑现有配置的具体请求时,必须:
- 探索用户的代码库,了解现有结构和命名模式。
- 参考,明确需要关注的内容。
node_modules/konsistent/docs/guides/exploring-codebases.md
修改现有配置时:
- 保留所有与用户请求无关的规范。
- 保留现有的、
name和description值,除非用户要求更改。severity - 添加规范时,追加到数组中。
conventions - 当用户报告违规时,读取现有配置和违规文件,以确定是修复配置还是建议修复代码。
References
参考资料
All canonical documentation lives in (published with the package). Read these before authoring config:
node_modules/konsistent/docs/- — top-level
node_modules/konsistent/docs/reference/configuration.mdshape (version, conventions, severity, excludeFiles).konsistent.json - — every
node_modules/konsistent/docs/reference/predicates.mdpredicate (must,haveType,haveFiles,export,exportTypes,exportConstants,exportFunctions,exportInterfaces,exportClasses,import).importTypes - — globs, placeholders, case transformations (
node_modules/konsistent/docs/reference/path-patterns.md,toPascalCase,toCamelCase,toFlatCase,toNthSegment, …), negation.extract - —
node_modules/konsistent/docs/reference/constraints.md,matchesfor inline path constraints andsegments.if.placeholderSatisfies - —
node_modules/konsistent/docs/reference/conditional-rules.md/if/forblocks whenexcludeFilesis an array.must - —
node_modules/konsistent/docs/reference/case-maps.md,kebabToPascalMapfor acronyms and special casing.kebabToCamelMap - — copy-pasteable common patterns (provider packages, factories, adapters, conditional rules, …).
node_modules/konsistent/docs/guides/examples.md - — pattern-identification approach before writing rules.
node_modules/konsistent/docs/guides/exploring-codebases.md
If is not present (e.g. the project doesn't have installed yet), fall back to the published docs at https://github.com/vercel-labs/konsistent/tree/main/docs.
node_modules/konsistent/docs/konsistent所有标准文档都位于中(随包发布)。编写配置前请阅读这些文档:
node_modules/konsistent/docs/- —— 顶层
node_modules/konsistent/docs/reference/configuration.md结构(version、conventions、severity、excludeFiles)。konsistent.json - —— 所有
node_modules/konsistent/docs/reference/predicates.md断言(must、haveType、haveFiles、export、exportTypes、exportConstants、exportFunctions、exportInterfaces、exportClasses、import)。importTypes - —— 通配符、占位符、大小写转换(
node_modules/konsistent/docs/reference/path-patterns.md、toPascalCase、toCamelCase、toFlatCase、toNthSegment等)、取反操作。extract - —— 用于内联路径约束的
node_modules/konsistent/docs/reference/constraints.md、matches,以及segments。if.placeholderSatisfies - —— 当
node_modules/konsistent/docs/reference/conditional-rules.md为数组时的must/if/for块。excludeFiles - —— 用于首字母缩写和特殊大小写的
node_modules/konsistent/docs/reference/case-maps.md、kebabToPascalMap。kebabToCamelMap - —— 可直接复制的常见模式(提供者包、工厂、适配器、条件规则等)。
node_modules/konsistent/docs/guides/examples.md - —— 编写规则前的模式识别方法。
node_modules/konsistent/docs/guides/exploring-codebases.md
如果不存在(例如项目尚未安装),请使用GitHub上发布的文档:https://github.com/vercel-labs/konsistent/tree/main/docs。
node_modules/konsistent/docs/konsistentGuidelines
指南
- Explore the user's project first — understand actual directory structure, naming conventions, and patterns before writing rules.
- Use for conventions that are recommended but not mandatory.
severity: "warning" - Use on conventions to give them identifiable IDs (must be kebab-case).
name - Use when the convention name alone isn't self-explanatory.
description - Prefer templates with case transformations over hardcoded names — this is konsistent's key strength.
- Group related predicates in one convention when they apply to the same path.
- Use separate conventions for the same path when different severities are needed.
- Use path negation to exclude known exceptions rather than listing all included paths.
- Consider conventions between related files as well, not only within a single file. Use the predicate to ensure a specific other file exists based on the matched file, and use
haveFilesto enforce conventions within specific other related files based on the matched file.for.files - Validate the generated config by running via the
konsistent validatescript (e.g.package.json).pnpm konsistent validate - Test the config against the actual codebase by running via the
konsistentscript (with no arguments).package.json
Important reminder: The objective is NOT to write a file that leads to zero errors when running the CLI. That would defeat the purpose. The objective is to create a konsistent.json file that identifies violations to patterns used in the codebase, even if they are not being 100% adhered to.
konsistent.json- 首先探索用户的项目——在编写规则前,了解实际的目录结构、命名规范和模式。
- 对于建议性而非强制性的规范,使用。
severity: "warning" - 在规范上使用属性,为其分配可识别的ID(必须为kebab-case格式)。
name - 当规范名称本身无法清晰说明时,使用。
description - 优先使用带大小写转换的模板,而非硬编码名称——这是konsistent的核心优势。
- 将适用于同一路径的相关断言分组到一个规范中。
- 当需要不同的严重级别时,为同一路径使用单独的规范。
- 使用路径取反来排除已知例外,而非列出所有包含的路径。
- 考虑相关文件之间的规范,而不仅仅是单个文件内的规范。使用断言确保匹配的文件对应的特定其他文件存在,并使用
haveFiles在匹配文件对应的特定相关文件内强制执行规范。for.files - 通过脚本运行
package.json来验证生成的配置(例如konsistent validate)。pnpm konsistent validate - 通过脚本运行
package.json(不带参数),在实际代码库中测试配置。konsistent
重要提醒: 目标不是编写一个运行CLI时零错误的文件,这会违背其初衷。目标是创建一个konsistent.json文件,能够识别代码库中使用的模式的违规情况,即使这些模式并未被100%遵守。
konsistent.json