Skip to content

Commit

Permalink
✨ feat(metamask): Add prepareExtension function (#910)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception authored Oct 4, 2023
1 parent 815dc96 commit da70e59
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 12 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"lint:check": "biome check . --verbose",
"lint:unsafe": "biome check . --apply-unsafe",
"prepare": "husky install",
"sort-package-json": "sort-package-json 'package.json' 'packages/*/package.json' 'examples/*/package.json'",
"sort-package-json:check": "sort-package-json 'package.json' 'packages/*/package.json' 'examples/*/package.json' --check",
"sort-package-json": "sort-package-json 'package.json' '{packages,wallets,examples}/*/package.json'",
"sort-package-json:check": "sort-package-json 'package.json' '{packages,wallets,examples}/*/package.json' --check",
"test": "turbo test"
},
"lint-staged": {
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions wallets/metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@
],
"scripts": {
"build": "pnpm run clean && pnpm run build:dist && pnpm run build:types",
"build:dist": "tsup",
"build:types": "tsc --emitDeclarationOnly",
"build:dist": "tsup --tsconfig tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly --project tsconfig.build.json",
"clean": "rimraf dist types",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:watch": "vitest watch",
"types:check": "tsc --noEmit"
},
"dependencies": {
"core": "workspace:*"
},
"devDependencies": {
"@vitest/coverage-v8": "1.0.0-beta.0",
"rimraf": "^5.0.1",
"tsconfig": "workspace:*",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"vitest": "^0.34.6"
}
}
20 changes: 20 additions & 0 deletions wallets/metamask/src/prepareExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { downloadFile, ensureCacheDirExists, unzipArchive } from 'core'

export const DEFAULT_METAMASK_VERSION = '10.25.0'
export const EXTENSION_DOWNLOAD_URL = `https://github.com/MetaMask/metamask-extension/releases/download/v${DEFAULT_METAMASK_VERSION}/metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`

export async function prepareExtension() {
const cacheDirPath = ensureCacheDirExists()

const downloadResult = await downloadFile({
url: EXTENSION_DOWNLOAD_URL,
outputDir: cacheDirPath,
fileName: `metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`
})

const unzipResult = await unzipArchive({
archivePath: downloadResult.filePath
})

return unzipResult.outputPath
}
82 changes: 82 additions & 0 deletions wallets/metamask/test/prepareExtension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as core from 'core'
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'
import {
DEFAULT_METAMASK_VERSION,
EXTENSION_DOWNLOAD_URL,
prepareExtension
} from '../src/prepareExtension'

const MOCK_CACHE_DIR_PATH = 'mockCacheDirPath'
const MOCK_EXTENSION_ARCHIVE_PATH = 'mockExtensionArchivePath'
const MOCK_EXTENSION_FINAL_PATH = 'mockExtensionFinalPath'

vi.mock('core', async () => {
return {
default: vi.fn(),
ensureCacheDirExists: vi.fn().mockImplementation(() => MOCK_CACHE_DIR_PATH),
downloadFile: vi.fn().mockImplementation(() => {
return {
filePath: 'mockExtensionArchivePath'
}
}),
unzipArchive: vi.fn().mockImplementation(() => {
return {
outputPath: MOCK_EXTENSION_FINAL_PATH
}
})
}
})

describe('prepareExtension', () => {
afterEach(() => {
vi.clearAllMocks()
})

afterAll(() => {
vi.resetAllMocks()
})

it('creates cache directory', async () => {
const ensureCacheDirExistsSpy = vi.spyOn(core, 'ensureCacheDirExists')

await prepareExtension()

expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce()
expect(ensureCacheDirExistsSpy).toReturnWith(MOCK_CACHE_DIR_PATH)
})

it('downloads MetaMask extension archive', async () => {
const downloadFileSpy = vi.spyOn(core, 'downloadFile')

await prepareExtension()

expect(downloadFileSpy).toHaveBeenCalledOnce()
expect(downloadFileSpy).toHaveBeenCalledWith({
url: EXTENSION_DOWNLOAD_URL,
outputDir: MOCK_CACHE_DIR_PATH,
fileName: `metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`
})
expect(downloadFileSpy).toReturnWith({
filePath: MOCK_EXTENSION_ARCHIVE_PATH
})
})

it('unzips MetaMask extension archive', async () => {
const unzipArchiveSpy = vi.spyOn(core, 'unzipArchive')

await prepareExtension()

expect(unzipArchiveSpy).toHaveBeenCalledOnce()
expect(unzipArchiveSpy).toHaveBeenCalledWith({
archivePath: MOCK_EXTENSION_ARCHIVE_PATH
})
expect(unzipArchiveSpy).toReturnWith({
outputPath: MOCK_EXTENSION_FINAL_PATH
})
})

it('returns correct unzipped extension path', async () => {
const extensionPath = await prepareExtension()
expect(extensionPath).toEqual(MOCK_EXTENSION_FINAL_PATH)
})
})
11 changes: 11 additions & 0 deletions wallets/metamask/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "types",
"declaration": true,
"sourceMap": true,
"declarationMap": true
},
"include": ["src"]
}
11 changes: 4 additions & 7 deletions wallets/metamask/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{
"extends": "tsconfig/base.json",
"extends": "./tsconfig.build.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "types",
"declaration": true,
"sourceMap": true,
"declarationMap": true
"rootDir": "."
},
"include": [
"src"
"src",
"test"
]
}

0 comments on commit da70e59

Please sign in to comment.