2019-01-25 20:25:54 +00:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2019-10-16 15:00:20 +00:00
|
|
|
const util = require('util');
|
2019-01-25 20:25:54 +00:00
|
|
|
|
|
|
|
// Install browser preferences after downloading and unpacking
|
|
|
|
// firefox instances.
|
|
|
|
// Based on: https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Enterprise_deployment_before_60#Configuration
|
|
|
|
async function installFirefoxPreferences(executablePath) {
|
|
|
|
const firefoxFolder = path.dirname(executablePath);
|
2019-10-16 15:00:20 +00:00
|
|
|
const mkdirAsync = util.promisify(fs.mkdir.bind(fs));
|
2019-01-25 20:25:54 +00:00
|
|
|
|
|
|
|
let prefPath = '';
|
|
|
|
let configPath = '';
|
|
|
|
if (os.platform() === 'darwin') {
|
|
|
|
prefPath = path.join(firefoxFolder, '..', 'Resources', 'defaults', 'pref');
|
|
|
|
configPath = path.join(firefoxFolder, '..', 'Resources');
|
|
|
|
} else if (os.platform() === 'linux') {
|
2019-02-01 01:30:34 +00:00
|
|
|
if (!fs.existsSync(path.join(firefoxFolder, 'browser', 'defaults')))
|
|
|
|
await mkdirAsync(path.join(firefoxFolder, 'browser', 'defaults'));
|
|
|
|
if (!fs.existsSync(path.join(firefoxFolder, 'browser', 'defaults', 'preferences')))
|
|
|
|
await mkdirAsync(path.join(firefoxFolder, 'browser', 'defaults', 'preferences'));
|
2019-01-25 20:25:54 +00:00
|
|
|
prefPath = path.join(firefoxFolder, 'browser', 'defaults', 'preferences');
|
|
|
|
configPath = firefoxFolder;
|
|
|
|
} else if (os.platform() === 'win32') {
|
|
|
|
prefPath = path.join(firefoxFolder, 'defaults', 'pref');
|
|
|
|
configPath = firefoxFolder;
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported platform: ' + os.platform());
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
copyFile({
|
|
|
|
from: path.join(__dirname, '00-puppeteer-prefs.js'),
|
|
|
|
to: path.join(prefPath, '00-puppeteer-prefs.js'),
|
|
|
|
}),
|
|
|
|
copyFile({
|
|
|
|
from: path.join(__dirname, 'puppeteer.cfg'),
|
|
|
|
to: path.join(configPath, 'puppeteer.cfg'),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyFile({from, to}) {
|
|
|
|
var rd = fs.createReadStream(from);
|
|
|
|
var wr = fs.createWriteStream(to);
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
rd.on('error', reject);
|
|
|
|
wr.on('error', reject);
|
|
|
|
wr.on('finish', resolve);
|
|
|
|
rd.pipe(wr);
|
|
|
|
}).catch(function(error) {
|
|
|
|
rd.destroy();
|
|
|
|
wr.end();
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = installFirefoxPreferences;
|