Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Snap manifests with Prettier #2787

Merged
merged 6 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,3 @@ packages/examples/examples/webpack/index.html
packages/snaps-execution-environments/lavamoat/**/*.json
packages/snaps-jest/public
packages/snaps-simulator/vendor

packages/examples/packages/*/snap.manifest.json
24 changes: 4 additions & 20 deletions packages/examples/packages/bip32/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,21 @@
"snap_dialog": {},
"snap_getBip32Entropy": [
{
"path": [
"m",
"44'",
"0'"
],
"path": ["m", "44'", "0'"],
"curve": "secp256k1"
},
{
"path": [
"m",
"44'",
"0'"
],
"path": ["m", "44'", "0'"],
"curve": "ed25519"
},
{
"path": [
"m",
"44'",
"0'"
],
"path": ["m", "44'", "0'"],
"curve": "ed25519Bip32"
}
],
"snap_getBip32PublicKey": [
{
"path": [
"m",
"44'",
"0'"
],
"path": ["m", "44'", "0'"],
"curve": "secp256k1"
}
]
Expand Down
4 changes: 1 addition & 3 deletions packages/examples/packages/bip44/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
"initialPermissions": {
"endowment:rpc": {
"dapps": true,
"allowedOrigins": [
"npm:@metamask/json-rpc-example-snap"
]
"allowedOrigins": ["npm:@metamask/json-rpc-example-snap"]
},
"snap_dialog": {},
"snap_getBip44Entropy": [
Expand Down
4 changes: 1 addition & 3 deletions packages/examples/packages/get-file/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"registry": "https://registry.npmjs.org"
}
},
"files": [
"./files/foo.json"
]
"files": ["./files/foo.json"]
},
"initialPermissions": {
"endowment:rpc": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
"registry": "https://registry.npmjs.org/"
}
},
"locales": [
"locales/da.json",
"locales/en.json",
"locales/nl.json"
]
"locales": ["locales/da.json", "locales/en.json", "locales/nl.json"]
},
"initialPermissions": {
"endowment:rpc": {
Expand Down
4 changes: 1 addition & 3 deletions packages/examples/packages/name-lookup/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
},
"initialPermissions": {
"endowment:name-lookup": {
"chains": [
"eip155:1"
]
"chains": ["eip155:1"]
}
},
"manifestVersion": "0.1"
Expand Down
2 changes: 1 addition & 1 deletion packages/snaps-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"ora": "^5.4.1",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"prettier": "^2.8.8",
"process": "^0.11.10",
"punycode": "^2.3.0",
"querystring-es3": "^0.2.1",
Expand Down Expand Up @@ -146,7 +147,6 @@
"jest-it-up": "^2.0.0",
"jest-silent-reporter": "^0.6.0",
"memfs": "^3.4.13",
"prettier": "^2.8.8",
"prettier-plugin-packagejson": "^2.5.2",
"ts-node": "^10.9.1",
"tsc-watch": "^4.5.0",
Expand Down
59 changes: 59 additions & 0 deletions packages/snaps-cli/src/commands/manifest/implementation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,63 @@ describe('manifest', () => {
expect.stringMatching('The snap manifest file has been updated.'),
);
});

it('formats a snap manifest with Prettier', async () => {
const error = jest.spyOn(console, 'error').mockImplementation();
const log = jest.spyOn(console, 'log').mockImplementation();

await fs.writeFile(
'/snap/snap.manifest.json',
JSON.stringify(
getSnapManifest({
shasum: 'G/W5b2JZVv+epgNX9pkN63X6Lye9EJVJ4NLSgAw/afd=',
initialPermissions: {
'endowment:name-lookup': {
chains: ['eip155:1', 'eip155:2', 'eip155:3'],
},
},
}),
),
);

const spinner = ora();
const result = await manifest('/snap/snap.manifest.json', true, spinner);
expect(result).toBe(true);

expect(error).not.toHaveBeenCalled();
expect(log).toHaveBeenCalledWith(
expect.stringMatching('The snap manifest file has been updated.'),
);

expect(await fs.readFile('/snap/snap.manifest.json', 'utf8'))
.toMatchInlineSnapshot(`
"{
"version": "1.0.0",
"description": "The test example snap!",
"proposedName": "@metamask/example-snap",
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/example-snap.git"
},
"source": {
"shasum": "d4W7f1lzpVGMj8jjCn1lYhhHmKc/9TSk5QLH5ldKQoI=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
"packageName": "@metamask/example-snap",
"registry": "https://registry.npmjs.org",
"iconPath": "images/icon.svg"
}
}
},
"initialPermissions": {
"endowment:name-lookup": {
"chains": ["eip155:1", "eip155:2", "eip155:3"]
}
},
"manifestVersion": "0.1"
}
"
`);
});
});
20 changes: 20 additions & 0 deletions packages/snaps-cli/src/commands/manifest/implementation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import { checkManifest, indent } from '@metamask/snaps-utils/node';
import { assert } from '@metamask/utils';
import { red, yellow, green } from 'chalk';
import { promises as fs } from 'fs';
import type { Ora } from 'ora';
import { dirname } from 'path';
import babel from 'prettier/parser-babel';
import { format } from 'prettier/standalone';

import { error, info, warn } from '../../utils';

/**
* Write the manifest to disk.
*
* @param path - The path to write the manifest to.
* @param data - The manifest data.
* @returns A promise that resolves when the manifest has been written.
*/
async function writeManifest(path: string, data: string) {
Mrtenz marked this conversation as resolved.
Show resolved Hide resolved
const formattedManifest = format(data, {
parser: 'json',
plugins: [babel],
});

await fs.writeFile(path, formattedManifest);
}

/**
* Check the snap manifest file at the given path. If `write` is `true`, the
* manifest will be written to disk if it is invalid. If `write` is `false`,
Expand All @@ -24,6 +43,7 @@ export async function manifest(
): Promise<boolean> {
const { reports, updated } = await checkManifest(dirname(path), {
updateAndWriteManifest: write,
writeFileFn: writeManifest,
});

const errors = [];
Expand Down
Loading