Skip to content

Commit

Permalink
Clarify Snaps CLI usage (#1075)
Browse files Browse the repository at this point in the history
* Clarify Snaps CLI usage

* yarn run mm-snap -> yarn mm-snap

* address reviewer feedback
  • Loading branch information
alexandratran authored Jan 4, 2024
1 parent de0abad commit a764d1b
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 120 deletions.
7 changes: 2 additions & 5 deletions snaps-sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ const sidebar = {
},
{
type: "category",
label: "Snaps command line interface",
link: {
type: "doc",
id: "reference/cli/index",
},
label: "Snaps command line",
link: { type: "generated-index", slug: "reference/cli" },
items: [
{
type: "doc",
Expand Down
29 changes: 10 additions & 19 deletions snaps/concepts/anatomy.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ sidebar_position: 1

# Snaps anatomy

If you look at the directory structure of the
[Snaps template repository](https://github.com/MetaMask/template-snap-monorepo) used in the
If you look at the directory structure of the Snaps monorepo project generated in the
[Snaps quickstart](../get-started/quickstart.mdx), it looks something like this:

```text
template-snap-monorepo/
your-snap-name/
├─ packages/
│ ├─ site/
| | |- src/
Expand All @@ -31,12 +30,6 @@ template-snap-monorepo/
Source files other than `index.ts` are located through its imports.
The defaults can be overwritten in the [configuration file](#configuration-file).

:::tip Create a Snap project
When you create a new Snap project using `mm-snap init`, it has all these files.
Still, we recommend
[cloning the template Snap repository to get started](../get-started/quickstart.mdx).
:::

This page examines the major components of a Snap:

- [The source code](#source-code) contains the primary code of the Snap.
Expand Down Expand Up @@ -155,8 +148,9 @@ details the requirements of both `snap.manifest.json` and its relationship to `p
You might need to modify some manifest fields manually.
For example, if you change the location of the icon SVG file, you must update
`source.location.npm.iconPath` to match.
You can also use the [command line](../reference/cli/index.md) to update some fields for you.
For example, `mm-snap build` or `mm-snap manifest --fix` updates `source.shasum`.
You can also use the [Snaps CLI](../reference/cli/subcommands.md) to update some fields for you.
For example, running [`yarn mm-snap build`](../reference/cli/subcommands.md#b-build) or
[`yarn mm-snap manifest --fix`](../reference/cli/subcommands.md#m-manifest) updates `source.shasum`.

## Configuration file

Expand Down Expand Up @@ -184,7 +178,7 @@ export default config;
```

:::note
You should not publish the configuration file to NPM, since it's only used for development and
You should not publish the configuration file to npm, since it's only used for development and
building.
However, you can commit the file to GitHub to share the configuration with your team, since it
shouldn't contain any secrets.
Expand All @@ -198,12 +192,9 @@ Moreover, the [Snaps execution environment](execution-environment.md) has no DOM
APIs, and no filesystem access, so anything that relies on the DOM doesn't work, and any Node
built-ins must be bundled along with the Snap.

Use the command `mm-snap build` to bundle your Snap using
[webpack](https://webpack.js.org/) or
[Browserify](https://browserify.org).
Running `yarn start` bundles your Snap for you.

You can also run [`yarn mm-snap build`](../reference/cli/subcommands.md#b-build) to bundle your
Snap using [webpack](https://webpack.js.org/) or [Browserify](https://browserify.org).
This command finds all dependencies using your specified main entry point and outputs a bundle
file to your specified output path.

:::note
If you are using the template Snap monorepo, running `yarn start` will bundle your Snap for you.
:::
30 changes: 16 additions & 14 deletions snaps/get-started/quickstart.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
description: Get started quickly using the create-snap CLI.
description: Get started quickly using the create-snap starter kit.
sidebar_position: 2
---

Expand All @@ -8,8 +8,8 @@ import ReactPlayer from 'react-player/lazy'
# Snaps quickstart

Get started creating your own Snap.
Use the [`@metamask/create-snap` CLI](https://github.com/MetaMask/snaps/tree/main/packages/create-snap)
to initialize a Snap monorepo project built with TypeScript and React.
Use the [`@metamask/create-snap`](https://github.com/MetaMask/snaps/tree/main/packages/create-snap)
starter kit to initialize a Snap monorepo project built with TypeScript and React.
See the following video demo:

<p align="center">
Expand All @@ -19,30 +19,32 @@ See the following video demo:
## Prerequisites

- [MetaMask Flask installed](install-flask.md)

:::tip
MetaMask Snaps works with the latest LTS version of Node.js, but we recommend using the version specified in
the template's [`.nvmrc`](https://github.com/MetaMask/template-snap-monorepo/blob/main/.nvmrc) file.
:::

- A text editor (for example, [VS Code](https://code.visualstudio.com/))
- [Yarn](https://yarnpkg.com/) version 3
- [Node](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) version 18.16 or later
- [Yarn](https://yarnpkg.com/)

## Create the project

Create a new Snap project using the [`@metamask/create-snap` CLI](https://github.com/MetaMask/snaps/tree/main/packages/create-snap) by running:
Create a new Snap project using the [`@metamask/create-snap`](https://github.com/MetaMask/snaps/tree/main/packages/create-snap)
starter kit by running:

```bash
yarn create @metamask/snap your-snap-name
```

or

```bash
npx @metamask/create-snap your-snap-name
```

or

```bash
npm create @metamask/snap your-snap-name
```

See the section [Snaps anatomy](../concepts/anatomy.md) to learn about the anatomy of your Snap project files.
See [Snaps anatomy](../concepts/anatomy.md) to learn about the anatomy of your Snap project files.

## Start the Snap

Expand All @@ -58,11 +60,11 @@ Start the development server:
yarn start
```

You are now serving the Snap at [`http://localhost:8080`](http://localhost:8080/) and its front-end at [`http://localhost:8000`](http://localhost:8000/).
You are now serving the Snap at [`http://localhost:8080`](http://localhost:8080/) and its front-end dapp at [`http://localhost:8000`](http://localhost:8000/).

## Connect to the Snap

On the front-end, select the **Connect** button and the MetaMask Flask extension pops up and
On the front-end dapp, select the **Connect** button and the MetaMask Flask extension pops up and
requires you to approve the Snap's permissions.

Once connected, select the **Send message** button to display a custom message within a confirmation
Expand Down
17 changes: 7 additions & 10 deletions snaps/how-to/develop-a-snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Before developing a Snap, make sure you understand the following concepts and gu
- [Snaps security guidelines](../concepts/security-guidelines.md)
:::

You can [get started quickly using the Snaps template](../get-started/quickstart.mdx) or follow a
[tutorial](/snaps/category/tutorials).
You can get started by [creating a new Snap project](../get-started/quickstart.mdx) or following a
[tutorial](/snaps/tutorials).

This page describes additional important steps when developing a Snap.

Expand Down Expand Up @@ -57,21 +57,17 @@ if (provider && isFlask) {
## Test your Snap
Test your Snap by hosting it locally using `mm-snap serve`, installing it in Flask, and calling its
Test your Snap by hosting it locally using `yarn start`, installing it in Flask, and calling its
API methods from a web page.
:::note
If you use the template Snap monorepo, running `yarn start` will serve the Snap at
[`http://localhost:8080`](http://localhost:8080/)
:::

For end-to-end Snap testing, [use the `@metamask/snaps-jest` package](test-a-snap.md).
## Debug your Snap
To debug your Snap, use `console.log` and inspect the MetaMask background process.
You can add your log statements in your source code and build your Snap, or add them directly
to your Snap bundle and use `mm-snap manifest --fix` to update the `shasum` in your Snap manifest file.
to your Snap bundle and use [`yarn mm-snap manifest --fix`](../reference/cli/subcommands.md#m-manifest)
to update the `shasum` in your Snap manifest file.
The manifest `shasum` must match the contents of your bundle at the time MetaMask fetches your Snap.
:::note
Expand Down Expand Up @@ -110,7 +106,8 @@ The following details are specific to Snaps:
After publishing the Snap, any dapp can connect to the Snap by using the Snap ID `npm:[packageName]`.

:::caution
If you are using the Snap template, make sure to only publish the Snap package in `/packages/snap`.
If you are using the Snap monorepo project generated in the [quickstart](../get-started/quickstart.mdx),
make sure to only publish the Snap package in `/packages/snap`.
You can use the [Snaps Simulator](https://metamask.github.io/snaps/snaps-simulator/staging/#/manifest) to verify
that your Snap was published correctly &mdash; just select **localhost** in the top right corner and change the
Snap location to **npm** and the ID of your Snap.
Expand Down
25 changes: 14 additions & 11 deletions snaps/how-to/troubleshoot.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ If you encounter any issues that you can't solve on your own, please
Because [Secure ECMAScript (SES)](../concepts/execution-environment.md) adds additional restrictions
on the JavaScript runtime on top of strict mode, code that executes normally under strict mode might
not under SES.
`mm-snap build` by default attempts to execute a Snap in a stubbed SES environment.
You can also disable this behavior and run the evaluation step separately using `mm-snap eval`.
[`yarn mm-snap build`](../reference/cli/subcommands.md#b-build) by default attempts to execute a
Snap in a stubbed SES environment.
You can also disable this behavior and run the evaluation step separately using
[`yarn mm-snap eval`](../reference/cli/subcommands.md#e-eval).
If an error is thrown during this step, it's likely due to a SES incompatibility, and you must fix
the issues manually.
These incompatibilities tend to occur in dependencies.

While the errors you get from SES may seem scary, they're usually not that hard to fix.
The actual file, function, and variable names in the `mm-snap eval` error stack trace might not make
a lot of sense to you, but the line numbers should correspond to your Snap
[bundle file](../concepts/anatomy.md#bundle-file).
The errors you get from SES are usually easy to fix.
The actual file, function, and variable names in the `eval` error stack trace might not make sense
to you, but the line numbers should correspond to your Snap [bundle file](../concepts/anatomy.md#bundle-file).
Thus, you can identify if the error is due to your code or one of your dependencies.
If the problem is in a dependency, you can try a different version or to fix the issue locally by
using tools such as [`patch-package`](https://npmjs.com/package/patch-package) (see how to
Expand All @@ -36,10 +37,11 @@ and therefore in SES as well.
If you get an error during the `eval` step that says something like `variableName is not defined`,
simply prepending `var variableName;` to your Snap bundle may solve the problem.
(This actually happened so frequently with [Babel's](https://babeljs.io/) `regeneratorRuntime` that
`mm-snap build` automatically handles that one.)
`yarn mm-snap build` automatically handles that one.)

:::caution
Run `mm-snap manifest --fix` if you modified your Snap bundle after building.
Run [`yarn mm-snap manifest --fix`](../reference/cli/subcommands.md#m-manifest) if you modified
your Snap bundle after building.
Otherwise your manifest `shasum` value won't be correct, and attempting to install your Snap fails.
:::

Expand All @@ -55,9 +57,10 @@ plugins for several other build systems:
For examples on how to set up these build systems yourself, please see the
[examples](https://github.com/MetaMask/snaps-monorepo/tree/main/packages/examples/examples).

We still recommend using the Snaps CLI to make sure your manifest `shasum` value is correct, by
running `mm-snap manifest --fix` after creating your bundle.
You may also benefit from running `mm-snap eval` to detect any SES issues up front.
We recommend running [`yarn mm-snap manifest --fix`](../reference/cli/subcommands.md#m-manifest)
after creating your bundle to make sure your manifest `shasum` value is correct.
You might also benefit from running [`yarn mm-snap eval`](../reference/cli/subcommands.md#e-eval)
to detect any SES issues up front.

## Patch dependencies

Expand Down
21 changes: 0 additions & 21 deletions snaps/reference/cli/index.md

This file was deleted.

Loading

0 comments on commit a764d1b

Please sign in to comment.