11import type { ExtensionConfig , FirefoxAddonsConfig } from './types'
2- import { existsSync , readdirSync } from 'node:fs'
2+ import { existsSync , readFileSync , readdirSync } from 'node:fs'
33import { mkdir , mkdtemp , rm } from 'node:fs/promises'
4+ import { createRequire } from 'node:module'
45import { tmpdir } from 'node:os'
56import { join , resolve } from 'node:path'
67import { buildExtension , resolveOutdir } from './build'
@@ -57,6 +58,27 @@ export function firefoxListingMetadata(config: ExtensionConfig, store: FirefoxAd
5758 }
5859}
5960
61+ /** Resolve web-ext from PATH or from this package's declared dependency. */
62+ export function resolveWebExtExecutable ( which : ( command : string ) => string | null = Bun . which ) : string | undefined {
63+ const fromPath = which ( 'web-ext' )
64+ if ( fromPath )
65+ return fromPath
66+
67+ try {
68+ const require = createRequire ( import . meta. url )
69+ const packagePath = require . resolve ( 'web-ext/package.json' )
70+ const packageJson = JSON . parse ( readFileSync ( packagePath , 'utf8' ) ) as { bin ?: string | Record < string , string > }
71+ const relativeBin = typeof packageJson . bin === 'string' ? packageJson . bin : packageJson . bin ?. [ 'web-ext' ]
72+ if ( ! relativeBin )
73+ return undefined
74+ const executable = resolve ( packagePath , '..' , relativeBin )
75+ return existsSync ( executable ) ? executable : undefined
76+ }
77+ catch {
78+ return undefined
79+ }
80+ }
81+
6082/** Build and submit a Firefox extension through Mozilla's official web-ext/AMO v5 flow. */
6183export async function publishFirefoxExtension ( config : ExtensionConfig , options : FirefoxPublishOptions ) : Promise < FirefoxPublishResult > {
6284 if ( ! config . geckoId )
@@ -69,7 +91,7 @@ export async function publishFirefoxExtension(config: ExtensionConfig, options:
6991
7092 const sourceDir = resolve ( cwd , resolveOutdir ( config , 'firefox' ) )
7193 const artifactsDir = resolve ( cwd , store . artifactsDir ?? 'web-ext-artifacts' )
72- const executable = Bun . which ( 'web-ext' )
94+ const executable = resolveWebExtExecutable ( )
7395 if ( ! executable )
7496 throw new Error ( '[browser-extension] web-ext is unavailable; reinstall @stacksjs/browser-extension dependencies' )
7597 await mkdir ( artifactsDir , { recursive : true } )
0 commit comments