Skip to content

Commit 82ee203

Browse files
authored
Format Snap manifests with Prettier (#2787)
This updates the CLI to format Snap manifests with Prettier before writing to disk. Closes #786.
1 parent b2448fb commit 82ee203

File tree

15 files changed

+253
-42
lines changed

15 files changed

+253
-42
lines changed

.prettierignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,3 @@ packages/examples/examples/webpack/index.html
7878
packages/snaps-execution-environments/lavamoat/**/*.json
7979
packages/snaps-jest/public
8080
packages/snaps-simulator/vendor
81-
82-
packages/examples/packages/*/snap.manifest.json

packages/examples/packages/bip32/snap.manifest.json

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,21 @@
2424
"snap_dialog": {},
2525
"snap_getBip32Entropy": [
2626
{
27-
"path": [
28-
"m",
29-
"44'",
30-
"0'"
31-
],
27+
"path": ["m", "44'", "0'"],
3228
"curve": "secp256k1"
3329
},
3430
{
35-
"path": [
36-
"m",
37-
"44'",
38-
"0'"
39-
],
31+
"path": ["m", "44'", "0'"],
4032
"curve": "ed25519"
4133
},
4234
{
43-
"path": [
44-
"m",
45-
"44'",
46-
"0'"
47-
],
35+
"path": ["m", "44'", "0'"],
4836
"curve": "ed25519Bip32"
4937
}
5038
],
5139
"snap_getBip32PublicKey": [
5240
{
53-
"path": [
54-
"m",
55-
"44'",
56-
"0'"
57-
],
41+
"path": ["m", "44'", "0'"],
5842
"curve": "secp256k1"
5943
}
6044
]

packages/examples/packages/bip44/snap.manifest.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
"initialPermissions": {
2020
"endowment:rpc": {
2121
"dapps": true,
22-
"allowedOrigins": [
23-
"npm:@metamask/json-rpc-example-snap"
24-
]
22+
"allowedOrigins": ["npm:@metamask/json-rpc-example-snap"]
2523
},
2624
"snap_dialog": {},
2725
"snap_getBip44Entropy": [

packages/examples/packages/get-file/snap.manifest.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"registry": "https://registry.npmjs.org"
1616
}
1717
},
18-
"files": [
19-
"./files/foo.json"
20-
]
18+
"files": ["./files/foo.json"]
2119
},
2220
"initialPermissions": {
2321
"endowment:rpc": {

packages/examples/packages/localization/snap.manifest.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
"registry": "https://registry.npmjs.org/"
1616
}
1717
},
18-
"locales": [
19-
"locales/da.json",
20-
"locales/en.json",
21-
"locales/nl.json"
22-
]
18+
"locales": ["locales/da.json", "locales/en.json", "locales/nl.json"]
2319
},
2420
"initialPermissions": {
2521
"endowment:rpc": {

packages/examples/packages/name-lookup/snap.manifest.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
},
1919
"initialPermissions": {
2020
"endowment:name-lookup": {
21-
"chains": [
22-
"eip155:1"
23-
]
21+
"chains": ["eip155:1"]
2422
}
2523
},
2624
"manifestVersion": "0.1"

packages/snaps-cli/src/commands/manifest/implementation.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,63 @@ describe('manifest', () => {
118118
expect.stringMatching('The snap manifest file has been updated.'),
119119
);
120120
});
121+
122+
it('formats a snap manifest with Prettier', async () => {
123+
const error = jest.spyOn(console, 'error').mockImplementation();
124+
const log = jest.spyOn(console, 'log').mockImplementation();
125+
126+
await fs.writeFile(
127+
'/snap/snap.manifest.json',
128+
JSON.stringify(
129+
getSnapManifest({
130+
shasum: 'G/W5b2JZVv+epgNX9pkN63X6Lye9EJVJ4NLSgAw/afd=',
131+
initialPermissions: {
132+
'endowment:name-lookup': {
133+
chains: ['eip155:1', 'eip155:2', 'eip155:3'],
134+
},
135+
},
136+
}),
137+
),
138+
);
139+
140+
const spinner = ora();
141+
const result = await manifest('/snap/snap.manifest.json', true, spinner);
142+
expect(result).toBe(true);
143+
144+
expect(error).not.toHaveBeenCalled();
145+
expect(log).toHaveBeenCalledWith(
146+
expect.stringMatching('The snap manifest file has been updated.'),
147+
);
148+
149+
expect(await fs.readFile('/snap/snap.manifest.json', 'utf8'))
150+
.toMatchInlineSnapshot(`
151+
"{
152+
"version": "1.0.0",
153+
"description": "The test example snap!",
154+
"proposedName": "@metamask/example-snap",
155+
"repository": {
156+
"type": "git",
157+
"url": "https://github.com/MetaMask/example-snap.git"
158+
},
159+
"source": {
160+
"shasum": "d4W7f1lzpVGMj8jjCn1lYhhHmKc/9TSk5QLH5ldKQoI=",
161+
"location": {
162+
"npm": {
163+
"filePath": "dist/bundle.js",
164+
"packageName": "@metamask/example-snap",
165+
"registry": "https://registry.npmjs.org",
166+
"iconPath": "images/icon.svg"
167+
}
168+
}
169+
},
170+
"initialPermissions": {
171+
"endowment:name-lookup": {
172+
"chains": ["eip155:1", "eip155:2", "eip155:3"]
173+
}
174+
},
175+
"manifestVersion": "0.1"
176+
}
177+
"
178+
`);
179+
});
121180
});

packages/snaps-cli/src/commands/manifest/implementation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { checkManifest, indent } from '@metamask/snaps-utils/node';
2+
import { writeManifest } from '@metamask/snaps-webpack-plugin';
23
import { assert } from '@metamask/utils';
34
import { red, yellow, green } from 'chalk';
45
import type { Ora } from 'ora';
@@ -24,6 +25,7 @@ export async function manifest(
2425
): Promise<boolean> {
2526
const { reports, updated } = await checkManifest(dirname(path), {
2627
updateAndWriteManifest: write,
28+
writeFileFn: writeManifest,
2729
});
2830

2931
const errors = [];

packages/snaps-webpack-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@metamask/snaps-sdk": "workspace:^",
6161
"@metamask/snaps-utils": "workspace:^",
6262
"@metamask/utils": "^9.2.1",
63+
"prettier": "^2.8.8",
6364
"webpack-sources": "^3.2.3"
6465
},
6566
"devDependencies": {
@@ -90,7 +91,6 @@
9091
"jest-it-up": "^2.0.0",
9192
"jest-silent-reporter": "^0.6.0",
9293
"memfs": "^3.4.13",
93-
"prettier": "^2.8.8",
9494
"prettier-plugin-packagejson": "^2.5.2",
9595
"typescript": "~5.3.3",
9696
"webpack": "^5.88.0"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Prettier config used for testing.
2+
module.exports = {
3+
tabWidth: 4,
4+
};

0 commit comments

Comments
 (0)