Comparing version 0.1.0 to 1.0.0
const debug = require('debug')('autobrowser:automater'); | ||
/** | ||
## Automator | ||
**/ | ||
function Automator(ps, cleanup) { | ||
if (! (this instanceof Automator)) { | ||
return new Automator(ps, cleanup); | ||
class Automator { | ||
constructor(ps) { | ||
this.ps = ps; | ||
} | ||
// save the process instance so we can kill it | ||
this.ps = ps; | ||
static of(ps, cleanup) { | ||
if (typeof cleanup == 'function') { | ||
ps.once('exit', () => { | ||
debug('browser process exited, cleaning up'); | ||
cleanup() | ||
}); | ||
} | ||
// patch in the cleanup method | ||
if (typeof cleanup == 'function') { | ||
this.cleanup = cleanup; | ||
return new Automator(ps); | ||
} | ||
kill() { | ||
debug('sending kill signal to browser'); | ||
this.ps.kill(); | ||
} | ||
} | ||
module.exports = Automator; | ||
/** | ||
### cleanup() | ||
Placeholder cleanup method. | ||
**/ | ||
Automator.prototype.cleanup = function(callback) { | ||
callback(); | ||
}; | ||
/** | ||
### kill() | ||
Close the process, run the cleanup method | ||
**/ | ||
Automator.prototype.kill = function(callback) { | ||
if (!!this.cleanup) { | ||
this.ps.once('exit', () => { | ||
debug('browser process exited, cleaning up'); | ||
this.cleanup(callback || function() {}); | ||
}); | ||
} | ||
debug('sending kill signal to browser'); | ||
this.ps.kill(); | ||
}; |
@@ -5,3 +5,3 @@ /* jshint node: true */ | ||
const fs = require('fs'); | ||
const spawn = require('child_process').spawn; | ||
const { spawn } = require('child_process'); | ||
const mkdirp = require('mkdirp'); | ||
@@ -12,3 +12,3 @@ const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
const automator = require('./automator'); | ||
const Automator = require('./automator'); | ||
@@ -54,5 +54,3 @@ const executables = [ | ||
// create a temporary profile for firefox | ||
createProfile(opts, function(err, profile) { | ||
var ps; | ||
createProfile(opts, (err, profile) => { | ||
if (err) { | ||
@@ -62,15 +60,5 @@ return callback(err); | ||
debug('attempting to start ' + executable); | ||
callback(null, automator( | ||
// spawn the process and pass to the automator | ||
ps = spawn(executable, ['-profile', profile, uri]), | ||
// define what happens at cleanup | ||
function(cb) { | ||
rimraf(profile, cb); | ||
} | ||
)); | ||
// handle the process erroring | ||
ps.on('error', callback); | ||
const ps = spawn(executable, ['-profile', profile, uri]); | ||
debug(`created process for ${executable}, creating automator`, callback); | ||
callback(null, Automator.of(ps, () => rimraf(profile, () => {}))); | ||
}); | ||
@@ -85,4 +73,4 @@ }; | ||
function createProfile(opts, callback) { | ||
var profileOpts = getProfileOpts(opts); | ||
var profilePath = path.resolve(__dirname, '.profiles', uuid.v4()); | ||
const profileOpts = getProfileOpts(opts); | ||
const profilePath = path.resolve(__dirname, '.profiles', uuid.v4()); | ||
@@ -99,5 +87,3 @@ // create a temporary directory for the profile | ||
profileOpts.join('\n'), 'utf8', | ||
function(err) { | ||
callback(err, profilePath); | ||
} | ||
(err) => callback(err, profilePath) | ||
); | ||
@@ -108,7 +94,5 @@ }); | ||
function getProfileOpts(opts) { | ||
var profileOpts = [].concat(baselineProfile); | ||
// expand options | ||
let profileOpts = [].concat(baselineProfile); | ||
Object.keys(opts).forEach(function(key) { | ||
var pack = optionPacks[key]; | ||
const pack = optionPacks.get(key); | ||
@@ -126,5 +110,3 @@ // if the option is a known option, then add to the profile opts | ||
return profileOpts.map(function(pairs) { | ||
return 'user_pref("' + pairs[0] + '", ' + pairs[1] + ');'; | ||
}); | ||
return profileOpts.map(([key, value]) => `user_pref("${key}", ${value});`); | ||
} |
32
index.js
@@ -5,4 +5,7 @@ /* jshint node: true */ | ||
const debug = require('debug')('autobrowse'); | ||
const { checkArgument } = require('conditional'); | ||
const adapters = [ | ||
require('./firefox') | ||
[ 'firefox', require('./firefox') ], | ||
[ 'chrome', require('./chrome') ], | ||
[ 'safari', require('./safari') ] | ||
]; | ||
@@ -28,3 +31,3 @@ | ||
```js | ||
autobrowse(browserName, uri, opts?, callback?) | ||
autobrowse(browserName, uri, opts, callback?) | ||
``` | ||
@@ -49,20 +52,15 @@ | ||
module.exports = function(browser, uri, opts, callback) { | ||
// check and create fallback arguments where appropriate | ||
checkArgument(typeof browser == 'string', 'autobrowse browser argument must be a string'); | ||
checkArgument(typeof uri == 'string', 'autobrowse url argument must be a string'); | ||
checkArgument(typeof opts == 'object' || opts === null, 'autobrowse opts must be an object or null'); | ||
callback = callback || {}; | ||
// get the adapter (first matching) that works for the specified executable | ||
const adapter = adapters.filter(adapter => { | ||
return !!adapter.getExecutable(); | ||
})[0]; | ||
const adapter = adapters.filter(([browserName, adapter]) => { | ||
return browserName.toLowerCase() === browser.toLowerCase() && !!adapter.getExecutable(); | ||
}).map(([browserName, adapter]) => adapter)[0]; | ||
debug('found adapter for browser ' + browser); | ||
// if we have not opts and no callback, then create a noop callback | ||
if (opts === undefined) { | ||
callback = function() {}; | ||
opts = {}; | ||
} else if (typeof opts == 'function') { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
// if we have no matching browser adapter, then report the error | ||
if (! browser) { | ||
if (!adapter) { | ||
return callback(new Error('Unable to find suitable adapter for browser executable: ' + executable)); | ||
@@ -69,0 +67,0 @@ } |
@@ -8,3 +8,3 @@ /** | ||
- enable the media source flag so we can load files for testing | ||
**/ | ||
@@ -18,2 +18,2 @@ | ||
[ 'media.mediasource.enabled', true ] | ||
]; | ||
]; |
@@ -1,3 +0,3 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
@@ -8,3 +8,3 @@ /** | ||
Option Packs are used to modify browser functionality when being automated. | ||
In most cases an option pack is enabled by simply calling `autobrowse` | ||
@@ -20,13 +20,7 @@ making use of the options argument. For instance, the following is code that | ||
module.exports = function(targetPath) { | ||
var flags = {}; | ||
const optionPackTuples = fs.readdirSync(targetPath) | ||
.filter(name => name !== 'index.js' || path.extname(name) !== '.js') | ||
.map(name => [path.basename(name, '.js'), require(path.resolve(targetPath, name))]); | ||
fs.readdirSync(targetPath) | ||
.filter(function(name) { | ||
return name !== 'index.js' || path.extname(name) !== '.js'; | ||
}) | ||
.forEach(function(name) { | ||
flags[path.basename(name, '.js')] = require(path.resolve(targetPath, name)); | ||
}); | ||
return flags; | ||
}; | ||
return new Map(optionPackTuples); | ||
}; |
{ | ||
"name": "autobrowse", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Automate browser execution based on known browser executables (like browser-launcher, but not quite as clever)", | ||
@@ -25,2 +25,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"conditional": "^5.3.0", | ||
"debug": "^2.6.8", | ||
@@ -27,0 +28,0 @@ "mkdirp": "^0.5.1", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16816
16
267
2
5
5
3
+ Addedconditional@^5.3.0
+ Addedconditional@5.3.0(transitive)