-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format Snap manifests with Prettier (#2787)
This updates the CLI to format Snap manifests with Prettier before writing to disk. Closes #786.
- Loading branch information
Showing
15 changed files
with
253 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/snaps-webpack-plugin/src/__fixtures__/.prettierrc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Prettier config used for testing. | ||
module.exports = { | ||
tabWidth: 4, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { writeManifest } from './manifest'; | ||
export { default } from './plugin'; | ||
export type { Options } from './plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { getSnapManifest } from '@metamask/snaps-utils/test-utils'; | ||
import { promises as fs } from 'fs'; | ||
import { resolve } from 'path'; | ||
|
||
import { writeManifest } from './manifest'; | ||
|
||
jest.mock('fs', () => ({ | ||
...jest.requireActual('fs'), | ||
promises: { | ||
writeFile: jest.fn(), | ||
}, | ||
})); | ||
|
||
describe('writeManifest', () => { | ||
it('formats the manifest with Prettier before writing to disk', async () => { | ||
const manifest = JSON.stringify(getSnapManifest()); | ||
await writeManifest('test.json', manifest); | ||
|
||
expect(jest.mocked(fs.writeFile).mock.calls[0][1]).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": "rNyfINgNh161cBmUop+F7xlE+GSEDZH53Y/HDpGLGGg=", | ||
"location": { | ||
"npm": { | ||
"filePath": "dist/bundle.js", | ||
"packageName": "@metamask/example-snap", | ||
"registry": "https://registry.npmjs.org", | ||
"iconPath": "images/icon.svg" | ||
} | ||
} | ||
}, | ||
"initialPermissions": { | ||
"snap_dialog": {}, | ||
"endowment:rpc": { "snaps": true, "dapps": false } | ||
}, | ||
"manifestVersion": "0.1" | ||
} | ||
" | ||
`); | ||
}); | ||
|
||
it('uses a custom Prettier config if found', async () => { | ||
const manifest = JSON.stringify(getSnapManifest()); | ||
await writeManifest( | ||
resolve(__dirname, '__fixtures__', 'foo.json'), | ||
manifest, | ||
); | ||
|
||
expect(jest.mocked(fs.writeFile).mock.calls[0][1]).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": "rNyfINgNh161cBmUop+F7xlE+GSEDZH53Y/HDpGLGGg=", | ||
"location": { | ||
"npm": { | ||
"filePath": "dist/bundle.js", | ||
"packageName": "@metamask/example-snap", | ||
"registry": "https://registry.npmjs.org", | ||
"iconPath": "images/icon.svg" | ||
} | ||
} | ||
}, | ||
"initialPermissions": { | ||
"snap_dialog": {}, | ||
"endowment:rpc": { "snaps": true, "dapps": false } | ||
}, | ||
"manifestVersion": "0.1" | ||
} | ||
" | ||
`); | ||
}); | ||
|
||
it('accepts a custom write function', async () => { | ||
const fn = jest.fn(); | ||
const manifest = JSON.stringify(getSnapManifest()); | ||
await writeManifest('test.json', manifest, fn); | ||
|
||
expect(fn).toHaveBeenCalledTimes(1); | ||
expect(fn.mock.calls[0][1]).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": "rNyfINgNh161cBmUop+F7xlE+GSEDZH53Y/HDpGLGGg=", | ||
"location": { | ||
"npm": { | ||
"filePath": "dist/bundle.js", | ||
"packageName": "@metamask/example-snap", | ||
"registry": "https://registry.npmjs.org", | ||
"iconPath": "images/icon.svg" | ||
} | ||
} | ||
}, | ||
"initialPermissions": { | ||
"snap_dialog": {}, | ||
"endowment:rpc": { "snaps": true, "dapps": false } | ||
}, | ||
"manifestVersion": "0.1" | ||
} | ||
" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { WriteFileFunction } from '@metamask/snaps-utils/node'; | ||
import { promises as fs } from 'fs'; | ||
import { format, resolveConfig } from 'prettier'; | ||
|
||
/** | ||
* Format the manifest data with Prettier and write it to disk. | ||
* | ||
* It uses the Prettier configuration found in the project directory (if any), | ||
* or the default Prettier configuration if none is found. | ||
* | ||
* @param path - The path to write the manifest to. | ||
* @param data - The manifest data. | ||
* @param writeFileFn - The function to use to write the manifest. | ||
* @returns A promise that resolves when the manifest has been written. | ||
*/ | ||
export async function writeManifest( | ||
path: string, | ||
data: string, | ||
writeFileFn: WriteFileFunction = fs.writeFile, | ||
) { | ||
const config = await resolveConfig(path, { | ||
editorconfig: true, | ||
}); | ||
|
||
const formattedManifest = format(data, { | ||
...config, | ||
parser: 'json', | ||
filepath: path, | ||
}); | ||
|
||
await writeFileFn(path, formattedManifest); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.