在本文中,我们分析了如何在 shadcn/ui 代码库中使用名为 error.ts 的文件。

error.ts 包含 12 个变量:
export const missing_dir_or_empty_project = "1" export const existing_config = "2" export const missing_config = "3" export const failed_config_read = "4" export const tailwind_not_configured = "5" export const import_alias_missing = "6" export const unsupported_framework = "7" export const component_url_not_found = "8" export const component_url_unauthorized = "9" export const component_url_forbidden = "10" export const component_url_bad_request = "11" export const component_url_internal_server_error = "12"
这些是描述性的,解释了正在处理的错误类型,例如,missing_dir_or_empty_project 可能用于缺少目录或空项目的场景。
这些变量值有什么特殊含义吗?它们只是分配为字符串的数字。一旦我们了解了如何使用这些变量,它就更有意义了。
如果你打开这个 preflight-init.ts,你会发现下面的代码片段。
// ensure target directory exists.
// check for empty project. we assume if no package.json exists, the project is empty.
if (
!fs.existssync(options.cwd) ||
!fs.existssync(path.resolve(options.cwd, "package.json"))
) {
errors[errors.missing_dir_or_empty_project] = true
return {
errors,
projectinfo: null,
}
}
注意这个errors[errors.missing_dir_or_empty_project]=true,这里的errors是一个对象。 missing_dir_or_empty_project 的值是多少?如前所述,它是“1”。
这意味着错误对象如下所示:
{
"1": true
}
这很好,但是这个错误对象是如何使用的以及在哪里使用?答案就在 init.ts 中。
在 init.ts 的第 91 行,你会发现下面的 if 块。
if (preflight.errors[ERRORS.MISSING_DIR_OR_EMPTY_PROJECT]) {
const { projectPath } = await createProject(options)
if (!projectPath) {
process.exit(1)
}
options.cwd = projectPath
options.isNewProject = true
}
如果这个检查类似于 preflight.error[“1”],你的下一个想法就是这个神秘的值是什么。这意味着,error.ts 中的这些常量用于提高代码可读性
功能列表:底层程序与前台页面分离的效果,对页面的修改无需改动任何程序代码。完善的标签系统,支持自定义标签,公用标签,快捷标签,动态标签,静态标签等等,支持标签内的vbs语法,原则上运用这些标签可以制作出任何想要的页面效果。兼容原来的栏目系统,可以很方便的插入一个栏目或者一个栏目组到页面的任何位置。底层模版解析程序具有非常高的效率,稳定性和容错性,即使模版中有错误的标签也不会影响页面的显示。所有的标
0
在 think throo,我们的使命是教授开源项目中使用的高级代码库架构概念。
通过在 next.js/react 中练习高级架构概念,将您的编码技能提高 10 倍,学习最佳实践并构建生产级项目。
我们是开源的 — https://github.com/thinkthroo/thinkthroo (请给我们一颗星!)
我们还提供网络开发和技术写作服务。请通过hello@thinkthroo.com联系我们了解更多信息!
https://github.com/shadcn-ui/ui/blob/main/packages/shadcn/src/preflights/preflight-init.ts#l22
https://github.com/shadcn-ui/ui/blob/main/packages/shadcn/src/utils/errors.ts
https://github.com/search?q=repo%3ashadcn-ui%2fui%20errors&type=code
https://github.com/shadcn-ui/ui/blob/1297abc8820480681ccec1bb026b29b30d9c858d/packages/shadcn/src/commands/init.ts#l91
以上就是Shadcn CLI如何使用错误常量来提高代码可读性的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号