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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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: 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) {
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