@atcute/lex-cli

中文文档
3.3.0发布于 2026/08/190BSDES Module近 30 天下载 1.5万

cli tool to generate type definitions for atcute

原始使用文档
文档目录9 节
  1. quick start
  2. authoring lexicons with TypeScript
  3. exporting lexicons to JSON
  4. pulling lexicons
  5. git sources
  6. atproto sources
  7. running the pull command
  8. publishing your schemas
  9. external references

@atcute/lex-cli

generate TypeScript schemas from lexicon documents.

npm install @atcute/lex-cli

quick start

create a configuration file that instructs the tool on where to locate the lexicon documents and where it should put the generated TypeScript schemas:

// file: lex.config.js
import { defineLexiconConfig } from '@atcute/lex-cli';

export default defineLexiconConfig({
    generate: {
        files: ['lexicons/**/*.json'],
        outdir: 'src/lexicons/',
    },
});

then run the tool (it automatically finds lex.config.js or lex.config.ts):

npm exec lex-cli generate

authoring lexicons with TypeScript

instead of writing lexicons as JSON documents, you can author them programmatically using the builder functions from @atcute/lexicon-doc/builder.

// file: lexicons-src/com/example/bookmark.ts
import { array, document, object, record, required, string } from '@atcute/lexicon-doc/builder';

export default document({
    id: 'com.example.bookmark',
    defs: {
        main: record({
            key: 'tid',
            description: 'a saved link to come back to later',
            record: object({
                properties: {
                    subject: required(string({ format: 'uri' })),
                    createdAt: required(string({ format: 'datetime' })),
                    tags: array({ items: string(), description: 'tags for organizing bookmarks' }),
                },
            }),
        }),
    },
});

update your config to point to TypeScript files:

// file: lex.config.js
import { defineLexiconConfig } from '@atcute/lex-cli';

export default defineLexiconConfig({
    generate: {
        files: ['lexicons-src/**/*.ts'],
        outdir: 'src/lexicons/',
    },
});

exporting lexicons to JSON

if you need the actual JSON lexicon documents (e.g., for publishing or sharing), configure the export command:

export default defineLexiconConfig({
    generate: {
        files: ['lexicons-src/**/*.ts'],
        outdir: 'src/lexicons/',
    },
    export: {
        outdir: 'lexicons/',
        clean: true,
    },
});

then run:

npm exec lex-cli export

pulling lexicons

you can pull lexicon files from other sources.

git sources

pull lexicons from git repositories using sparse checkout:

// file: lex.config.js
import { defineLexiconConfig } from '@atcute/lex-cli';

export default defineLexiconConfig({
    generate: {
        files: ['lexicons/**/*.json'],
        outdir: 'src/lexicons/',
    },
    pull: {
        outdir: 'lexicons/',
        clean: true,
        sources: [
            {
                type: 'git',
                remote: 'https://github.com/bluesky-social/atproto.git',
                ref: 'main',
                pattern: ['lexicons/**/*.json'],
            },
        ],
    },
});

atproto sources

pull lexicons directly from the AT Protocol network.

export default defineLexiconConfig({
    generate: {
        files: ['lexicons/**/*.json'],
        outdir: 'src/lexicons/',
    },
    pull: {
        outdir: 'lexicons/',
        sources: [
            {
                type: 'atproto',
                mode: 'nsids',
                nsids: ['app.bsky.feed.post', 'app.bsky.actor.profile'],
            },
            {
                type: 'atproto',
                mode: 'authority',
                authority: 'atproto-lexicons.bsky.social',
                pattern: ['com.atproto.*'], // optional
            },
        ],
    },
});

running the pull command

pull the lexicons to disk, then generate types from them:

npm exec lex-cli pull
npm exec lex-cli generate

publishing your schemas

if you're packaging your generated schemas as a publishable library, add the atcute:lexicons field to your package.json. this allows other projects to automatically discover and import your schemas without manual configuration.

{
    "name": "@example/my-schemas",
    "atcute:lexicons": {
        "mappings": {
            "com.example.*": {
                "type": "namespace",
                "path": "./types/{{nsid_remainder}}"
            }
        }
    }
}

the path field supports several template expansions:

  • . or ./ at the start is replaced with the package name (e.g., ./types/foo becomes @example/my-schemas/types/foo, or . becomes @example/my-schemas)
  • {{nsid}} - the full NSID with dots replaced by slashes (e.g., com/example/foo/bar)
  • {{nsid_prefix}} - the part before the wildcard (e.g., com/example)
  • {{nsid_remainder}} - the part after the prefix (e.g., foo/bar)

external references

when your lexicons reference types from namespaces outside your configured files, you'll need to configure how these references are resolved.

for example, if your lexicon references a type from another namespace:

// file: lexdocs/tools/ozone/team/defs.json
{
    "lexicon": 1,
    "id": "tools.ozone.team.defs",
    "defs": {
        "member": {
            "type": "object",
            "required": ["did", "role"],
            "properties": {
                "profile": {
                    "type": "ref",
                    "ref": "app.bsky.actor.defs#profileViewDetailed",
                    //     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    //     not covered by any of our included files
                },
            },
        },
    },
}

the simplest way to resolve external references is using the imports array with packages that provide the atcute:lexicons metadata:

// file: lex.config.js
import { defineLexiconConfig } from '@atcute/lex-cli';

export default defineLexiconConfig({
    generate: {
        files: ['lexicons/**/*.json'],
        outdir: 'src/lexicons/',
        imports: ['@atcute/atproto', '@atcute/bluesky'],
    },
});

the CLI will automatically discover the namespace mappings from each package's atcute:lexicons field in their package.json.

for packages without metadata, or when you need more fine-grained control over import resolution, use the mappings configuration instead:

// file: lex.config.js
import { defineLexiconConfig } from '@atcute/lex-cli';

export default defineLexiconConfig({
    generate: {
        files: ['lexicons/**/*.json'],
        outdir: 'src/lexicons/',
        mappings: [
            {
                nsid: ['com.atproto.*'],
                imports: (nsid) => {
                    const specifier = nsid.slice('com.atproto.'.length).replaceAll('.', '/');
                    return { type: 'namespace', from: `@atcute/atproto/types/${specifier}` };
                },
            },
            {
                nsid: ['app.bsky.*'],
                imports: (nsid) => {
                    const specifier = nsid.slice('app.bsky.'.length).replaceAll('.', '/');
                    return { type: 'namespace', from: `@atcute/bluesky/types/app/${specifier}` };
                },
            },
        ],
    },
});

版本与发布记录

@atcute/lex-cli 共发布 38 个版本,首次发布于 2024/08/18;过去 12 个月发布了 19 个版本。

版本发布日期距今
3.3.0latest2026/08/192 个月前
3.2.12026/06/294 个月前
3.2.02026/06/294 个月前
3.1.02026/05/205 个月前
3.0.12026/05/155 个月前
3.0.02026/05/095 个月前

关于 @atcute/lex-cli 的常见问题

@atcute/lex-cli 是什么?

cli tool to generate type definitions for atcute

如何安装 @atcute/lex-cli?

在项目目录运行 npm install @atcute/lex-cli,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 lex-cli,可以用 npx lex-cli 直接运行。

@atcute/lex-cli 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/atcute__lex-cli 是否存在。

@atcute/lex-cli 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@atcute/lex-cli 可以商用吗?

@atcute/lex-cli 使用 0BSD 许可。几乎没有限制:可自由使用、修改和分发,连保留版权声明也不要求。以上为概括,不构成法律意见,请以许可原文为准。

@atcute/lex-cli 的下载量有多少?

根据 npm 官方下载统计,@atcute/lex-cli 在 2026/10/07 – 2026/11/05 这 30 天内被下载 15,450 次,日均约 515 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@atcute/lex-cli 还在维护吗?

最新版本 3.3.0 发布于 2026/08/19(2 个月前);至今共发布 38 个版本,其中过去 12 个月发布了 19 个;首次发布于 2024/08/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。