main
js 68 lines 1.89 KB
Raw
1 #!/usr/bin/env node
2
3 'use strict';
4
5 const {exec} = require('child-process-promise');
6 const {Finder} = require('firefox-profile');
7 const {resolve} = require('path');
8 const {argv} = require('yargs');
9
10 const EXTENSION_PATH = resolve('./firefox/build/unpacked');
11 const START_URL = argv.url || 'https://react.dev/';
12
13 const firefoxVersion = process.env.WEB_EXT_FIREFOX;
14
15 const getFirefoxProfileName = () => {
16 // Keys are pulled from https://extensionworkshop.com/documentation/develop/web-ext-command-reference/#--firefox
17 // and profile names from https://searchfox.org/mozilla-central/source/toolkit/profile/xpcshell/head.js#96
18 switch (firefoxVersion) {
19 case 'firefox':
20 return 'default-release';
21 case 'beta':
22 return 'default-beta';
23 case 'nightly':
24 return 'default-nightly';
25 case 'firefoxdeveloperedition':
26 return 'dev-edition-default';
27 default:
28 // Fall back to using the default Firefox profile for testing purposes.
29 // This prevents users from having to re-login-to sites before testing.
30 return 'default';
31 }
32 };
33
34 const main = async () => {
35 const finder = new Finder();
36
37 const findPathPromise = new Promise((resolvePromise, rejectPromise) => {
38 finder.getPath(getFirefoxProfileName(), (error, profile) => {
39 if (error) {
40 rejectPromise(error);
41 } else {
42 resolvePromise(profile);
43 }
44 });
45 });
46
47 const options = [
48 `--source-dir=${EXTENSION_PATH}`,
49 `--start-url=${START_URL}`,
50 '--browser-console',
51 ];
52
53 try {
54 const path = await findPathPromise;
55 const trimmedPath = path.replace(' ', '\\ ');
56 options.push(`--firefox-profile=${trimmedPath}`);
57 } catch (err) {
58 console.warn('Could not find default profile, using temporary profile.');
59 }
60
61 try {
62 await exec(`web-ext run ${options.join(' ')}`);
63 } catch (err) {
64 console.error('`web-ext run` failed', err.stdout, err.stderr);
65 }
66 };
67
68 main();