|
| 1 | +title: Fork the Community Repository. |
| 2 | +stepnum: 1 |
| 3 | +level: 4 |
| 4 | +ref: snip-step-fork-the-repo |
| 5 | +content: | |
| 6 | +
|
| 7 | + If you plan to contribute to the community repository, fork the |
| 8 | + snippets `project repository |
| 9 | + <https://github.com/mongodb-labs/mongosh-snippets>`__. |
| 10 | + |
| 11 | + You do not have to fork the community repository if you want to |
| 12 | + create a private repo, but you should manually recreate a |
| 13 | + similar directory structure as you work through the following |
| 14 | + steps. |
| 15 | +--- |
| 16 | + |
| 17 | +title: Create a Package Directory. |
| 18 | +stepnum: 2 |
| 19 | +level: 4 |
| 20 | +ref: snip-step-create-directory |
| 21 | +content: | |
| 22 | +
|
| 23 | + Create a directory for your snippet package under the ``snippets`` |
| 24 | + directory in the forked repository. This directory will |
| 25 | + contain the code for your script and several metadata files. |
| 26 | +
|
| 27 | + This example shows directories for two snippet packages, |
| 28 | + ``decrypt-cards`` and ``update-auth``. The contents of the |
| 29 | + `community snippets |
| 30 | + <https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets>`__ |
| 31 | + directories are omitted for clarity. |
| 32 | +
|
| 33 | + .. code-block:: shell |
| 34 | + :copyable: false |
| 35 | + :emphasize-lines: 8-13, 18-22 |
| 36 | +
|
| 37 | + mongo-snippets |
| 38 | + | |
| 39 | + ├── scripts |
| 40 | + │ ├── make-index.js |
| 41 | + │ └── show-index.js |
| 42 | + └── snippets |
| 43 | + ├── analyze-schema |
| 44 | + ├── decrypt-cards |
| 45 | + │ ├── LICENSE-Community.txt |
| 46 | + │ ├── README.md |
| 47 | + │ ├── error-matchers.js |
| 48 | + │ ├── index.js |
| 49 | + │ └── package.json |
| 50 | + ├── mock-collection |
| 51 | + ├── mongocompat |
| 52 | + ├── resumetoken |
| 53 | + ├── spawn-mongod |
| 54 | + └── update-auth |
| 55 | + ├── LICENSE |
| 56 | + ├── README.md |
| 57 | + ├── index.js |
| 58 | + └── package.json |
| 59 | +--- |
| 60 | + |
| 61 | +title: Create ``README.md``. |
| 62 | +stepnum: 3 |
| 63 | +level: 4 |
| 64 | +ref: snip-step-readme |
| 65 | +content: | |
| 66 | +
|
| 67 | + Create a ``README.md``. The ``README.md`` describes how to use your |
| 68 | + code. This file is displayed when a user enters ``snippet help`` for |
| 69 | + your snippet. |
| 70 | +--- |
| 71 | + |
| 72 | +title: Create ``LICENSE``. |
| 73 | +stepnum: 4 |
| 74 | +level: 4 |
| 75 | +ref: snip-step-license |
| 76 | +content: | |
| 77 | +
|
| 78 | + Create a ``LICENSE`` file. You will need to enter a license |
| 79 | + identifier string later, so try to chose a license from the |
| 80 | + `SPDX license list <https://www.npmjs.com/package/spdx>`__. |
| 81 | +--- |
| 82 | + |
| 83 | +title: Create ``index.js``. |
| 84 | +stepnum: 5 |
| 85 | +level: 4 |
| 86 | +ref: snip-step-index.js |
| 87 | +content: | |
| 88 | +
|
| 89 | + Create an ``index.js`` file. |
| 90 | +
|
| 91 | + - This file contains the entry point to your code that is exposed in |
| 92 | + the :binary:`~bin.mongosh` console. |
| 93 | + - The script is written in JavaScript and defines your new functions. |
| 94 | + - The script can be in a single file or multiple files. |
| 95 | + - The script can call other files and local or remote npm modules. |
| 96 | + To ``require()`` a remote npm module use the construction: |
| 97 | +
|
| 98 | + .. code-block:: javascript |
| 99 | +
|
| 100 | + const localRequire = require('module').createRequire(__filename);) |
| 101 | +
|
| 102 | + For an example, see |
| 103 | + `index.js <https://github.com/mongodb-labs/mongosh-snippets/blob/main/snippets/resumetoken/index.js>`__ |
| 104 | + in the ``resumetoken`` snippet. |
| 105 | + - ``index.js`` is referenced in :ref:`package.json <snip-prep-package.json>`. |
| 106 | + - The MongoDB repository has `example code |
| 107 | + <https://github.com/mongodb-labs/mongosh-snippets>`__. |
| 108 | +
|
| 109 | + .. tip:: |
| 110 | +
|
| 111 | + If you have an existing script, either rename it ``index.js`` or |
| 112 | + create an ``index.js`` file to load it. For an example of an |
| 113 | + ``index.js`` file that loads other scripts, see `this one |
| 114 | + <https://github.com/mongodb-labs/mongosh-snippets/blob/main/snippets/mongocompat/index.js>`__ |
| 115 | + in the `community repository |
| 116 | + <https://github.com/mongodb-labs/mongosh-snippets>`__. |
| 117 | +
|
| 118 | +
|
0 commit comments