Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Shadcn CLI如何使用错误常量来提高代码可读性_创想鸟

Shadcn CLI如何使用错误常量来提高代码可读性

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

Shadcn CLI如何使用错误常量来提高代码可读性

utils/errors.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 中的用法:

如果你打开这个 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 中的错误对象

在 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 中的这些常量用于提高代码可读性

关于我们:

在 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如何使用错误常量来提高代码可读性的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1493238.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
使用 Rollup 创建具有 CommonJS 和 ESM 支持的 NPM 包
上一篇 2025年12月19日 15:31:34
清洁架构:遥不可及的理想——给开发者的寓言
下一篇 2025年12月19日 15:31:37

相关推荐

发表回复

登录后才能评论
关注微信