Docs Home / mongosh / Snippets / Reference

Create a Registry Index File Manually手动创建注册表索引文件

Warning

Experimental feature实验特性

This feature is experimental. MongoDB does not provide support for Snippets. This feature may be changed or removed at any time without prior notice.此功能是实验性的。MongoDB不支持代码段。此功能可能随时更改或删除,恕不另行通知。

Bugs are not expected, however should you encounter one, please open an issue in the GitHub repository for this project.预计不会出现错误,但如果您遇到错误,请在GitHub存储库中为此项目打开一个问题。

This page discusses how to manually create a registry index file. To generate the registry index file using a script, see Create a Registry Index File.本页讨论如何手动创建注册表索引文件。要使用脚本生成注册表索引文件,请参阅创建注册表索引文件

To create the registry index file:要创建注册表索引文件,请执行以下操作:

  1. Copy the following TypeScript template and save it as make-index.ts:复制以下TypeScript模板并将其另存为make-index.ts

    import bson from 'bson';
    import zlib from 'zlib';

    interface ErrorMatcher {
    // Add additional information to shell errors matching one of the regular expressions. 为与其中一个正则表达式匹配的shell错误添加其他信息。
    // The message can point to a snippet helping solve that error.该消息可以指向一个帮助解决该错误的代码段。
    matches: RegExp[];
    message: string;
    }

    interface SnippetDescription {
    // The *npm* package name. Users do not interact with this.*npm*包名。用户不与此交互。
    name: string;

    // The snippet name. This is what users interact with.代码段名称。这就是用户交互的对象。
    snippetName: string;

    // An optional install specifier that can be used to point npm towards packages that are not uploaded to the registry. 一个可选的安装说明符,可用于将npm指向未上传到注册表的包。
    // For example,this could be an URL to a git repository or a tarball.例如,这可能是一个指向git存储库或tarball的URL。
    installSpec?: string;

    // A version field. Users do not interact with this, as currently, `snippet` always installs the latest versions of snippets.版本字段。用户不与此交互,因为目前“snippet”总是安装最新版本的snippet。
    version: string;
    description: string;
    readme: string;

    // License should be a SPDX license identifier.许可证应该是SPDX许可证标识符。
    license: string;
    errorMatchers?: ErrorMatcher[];
    }

    interface SnippetIndexFile {
    // This must be 1 currently.目前必须为1。
    indexFileVersion: 1;
    index: SnippetDescription[];
    metadata: { homepage: string };
    }

    const indexFileContents: SnippetIndexFile = {
    indexFileVersion: 1,
    index: [ /* ... */ ],
    metadata: { homepage: 'https://example.com' }
    };

    // Serialize, compress and store the index file:
    fs.writeFileSync('index.bson.br',
    zlib.brotliCompressSync(
    bson.serialize(indexFileContents)));
  2. Edit make-index.ts as needed for your snippet package. Use the comments as a guide to filling out the required information.根据需要编辑代码段包的make-index.ts。使用评论作为填写所需信息的指南。
  3. Run ts-node make-index.ts to create the index.bson.br file.运行ts-node make-index.ts以创建index.bson.br文件。
  4. Upload index.bson.br to your GitHub repository.index.bson.br上传到GitHub存储库。
  5. Update snippetIndexSourceURLs to include a URL that references your index.bson.br.更新snippetIndexSourceURLs以包含引用index.bson.br的URL。