@@ -10,20 +10,63 @@ const __dirname = path.dirname(__filename);
1010// Get the device ID from command line arguments
1111const deviceId = process . argv [ 2 ] ;
1212if ( ! deviceId ) {
13- console . error ( 'Usage: pnpm dev:android <device-id>' ) ;
13+ console . error ( 'Usage: pnpm dev:android <device-id> [web-ext flags...]' ) ;
14+ console . error ( 'Example: pnpm dev:android emulator-5554 --source-dir ./custom-dir' ) ;
1415 process . exit ( 1 ) ;
1516}
1617
18+ // Get any additional flags to pass to web-ext
19+ const additionalFlags = process . argv . slice ( 3 ) ;
20+
21+ // Extract custom source directory if provided
22+ let customSourceDir = null ;
23+ const sourceDirIndex = additionalFlags . findIndex ( flag => flag === '--source-dir' || flag === '-s' ) ;
24+
25+ if ( sourceDirIndex !== - 1 ) {
26+ // Handle -s value or --source-dir value
27+ customSourceDir = additionalFlags [ sourceDirIndex + 1 ] ;
28+ } else {
29+ // Handle --source-dir=value
30+ const sourceDirFlag = additionalFlags . find ( flag => flag . startsWith ( '--source-dir=' ) ) ;
31+ if ( sourceDirFlag ) {
32+ customSourceDir = sourceDirFlag . split ( '=' ) [ 1 ] ;
33+ }
34+ }
35+
36+ // WXT builds to outDir/firefox-mv2
37+ const wxtOutDir = customSourceDir || '.output' ;
38+ const actualBuildDir = `${ wxtOutDir } /firefox-mv2` ;
39+
40+ // Remove source-dir flags from additional flags (we'll set it ourselves)
41+ const filteredFlags = [ ] ;
42+ for ( let i = 0 ; i < additionalFlags . length ; i ++ ) {
43+ const flag = additionalFlags [ i ] ;
44+ if ( flag === '--source-dir' || flag === '-s' ) {
45+ i ++ ; // Skip the next item (the value)
46+ continue ;
47+ }
48+ if ( flag . startsWith ( '--source-dir=' ) ) {
49+ continue ;
50+ }
51+ filteredFlags . push ( flag ) ;
52+ }
53+
54+ // Build web-ext command
55+ const webExtCommand = `web-ext run --target=firefox-android --android-device=${ deviceId } --firefox-apk=org.mozilla.fenix --source-dir ${ actualBuildDir } ${ filteredFlags . join ( ' ' ) } ` ;
56+
1757// Run the build and web-ext commands
1858try {
19- console . log ( `Building Firefox extension...` ) ;
20- execSync ( 'pnpm build:firefox' , { stdio : 'inherit' , cwd : path . dirname ( __dirname ) } ) ;
59+ // Set environment variable for custom output directory
60+ if ( customSourceDir ) {
61+ process . env . WXT_OUT_DIR = wxtOutDir ;
62+ console . log ( `Using custom output directory: ${ wxtOutDir } ` ) ;
63+ }
64+
65+ console . log ( 'Building Firefox extension...' ) ;
66+ execSync ( 'wxt build -b firefox' , { stdio : 'inherit' , cwd : path . dirname ( __dirname ) } ) ;
2167
2268 console . log ( `Running on Android device ${ deviceId } ...` ) ;
23- execSync ( `web-ext run --target=firefox-android --android-device=${ deviceId } --firefox-apk=org.mozilla.fenix --source-dir ./.output/firefox-mv2` , {
24- stdio : 'inherit' ,
25- cwd : path . dirname ( __dirname )
26- } ) ;
69+ execSync ( webExtCommand , { stdio : 'inherit' , cwd : path . dirname ( __dirname ) } ) ;
2770} catch ( error ) {
2871 console . error ( 'Command failed:' , error . message ) ;
2972 process . exit ( 1 ) ;
0 commit comments