Skip to content

Commit 44ea6a9

Browse files
authored
Android dev script improvements (#111)
* feat: add support for source dir and wext-ext flags passthrough * chore: bump version to 1.4.2
1 parent 95da9b4 commit 44ea6a9

4 files changed

Lines changed: 64 additions & 10 deletions

File tree

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,23 @@ To start the development server for Firefox:
119119
pnpm dev:firefox
120120
```
121121

122-
This builds the Firefox version and runs it on a connected Android device. Make sure ADB is installed, your device is
123-
connected with USB debugging enabled, and set the `ADB_ID` variable to your device ID (found via `adb devices`).
122+
To develop on Android, this builds the Firefox version and runs it on a connected Android device. Make sure ADB is installed, your device is
123+
connected with USB debugging enabled. You can pass additional `web-ext run` flags after the device ID:
124124

125125
```bash
126+
# Basic usage
126127
pnpm dev:android <device-id>
128+
129+
# With custom source directory
130+
pnpm dev:android <device-id> --s ./custom-output
131+
132+
# With additional web-ext flags
133+
pnpm dev:android <device-id> --adb-remove-old-artifacts
127134
```
128135

136+
> [!NOTE]
137+
> Some `web-ext run` options may not work when targeting Android devices. For example, browser startup options like `--start-url` are not supported on Android.
138+
129139
### Building
130140

131141
To build the extension for chromium browsers:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "royalrefresh",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"description": "A web extension for royalroad.com. For people who juggle multiple stories",
55
"main": "background.js",
66
"directories": {

scripts/dev-android.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,63 @@ const __dirname = path.dirname(__filename);
1010
// Get the device ID from command line arguments
1111
const deviceId = process.argv[2];
1212
if (!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
1858
try {
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);

wxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from 'wxt';
33
// See https://wxt.dev/api/config.html
44
export default defineConfig({
55
srcDir: 'src',
6+
outDir: process.env.WXT_OUT_DIR || '.output',
67
modules: ['@wxt-dev/module-svelte'],
78
manifest: {
89
name: 'RoyalRefresh',

0 commit comments

Comments
 (0)