Skip to content

Commit

Permalink
Add promise support
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Nov 12, 2021
1 parent 4e2078d commit ca8d84e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const Finder = require('./lib/finder')
const { fromCallback } = require('catering')
const after = require('after')
const Finder = require('./lib/finder')

const DEFAULTS = {
browsers: require('./lib/browsers')
}
const DEFAULTS = { browsers: require('./lib/browsers') }
const kPromise = Symbol('promise')

module.exports = function detect (names, opts, done) {
if (typeof names === 'string') {
Expand All @@ -23,11 +23,14 @@ module.exports = function detect (names, opts, done) {
opts = Object.assign({}, DEFAULTS, opts)
}

done = fromCallback(done, kPromise)

if (!names || !names.length) {
names = Object.keys(opts.browsers)
}

const result = []
const finish = (err) => err ? done(err) : done(null, result, totalMethods)
const next = after(names.length, finish)

// For debugging: count the number
Expand All @@ -47,8 +50,5 @@ module.exports = function detect (names, opts, done) {
})
})

function finish (err) {
if (err) done(err)
else done(err, result, totalMethods)
}
return done[kPromise]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"dependencies": {
"after": "^0.8.2",
"catering": "^2.1.0",
"chalk": "^4.1.2",
"debug": "^4.1.0",
"existent": "^1.0.1",
Expand Down
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ detect(function (err, browsers) {
console.log(browsers)
})

// Chrome and Firefox
// All browsers with promise
const browsers = await detect()

// Search only for Chrome and Firefox
detect(['chrome', 'firefox'], function (err, browsers) {
if (err) throw err

Expand All @@ -50,9 +53,9 @@ detect(['chrome', 'firefox'], function (err, browsers) {

## API

### `detect([names, ]callback)`
### `detect([names][, callback])`

`names` is an array of browser names you want to find. If omitted or empty, it will detect _[everything](http://youtu.be/k1yvvNvlXtg)_. The `callback` receives an error if any and an array of `results`. A result is excluded if its path has no `.exe` extension or if its version could not be read.
`names` is an array of browser names you want to find. If omitted or empty, it will detect _[everything](http://youtu.be/k1yvvNvlXtg)_. The `callback` receives an error if any and an array of `results`. A result is excluded if its path has no `.exe` extension or if its version could not be read. If no callback is provided, a promise is returned.

Each `result` is an object with the following properties:

Expand Down
52 changes: 51 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,18 @@ test('detect all', function (t) {
})
})

test('detect all with promise', async function (t) {
const results = await detect()
const names = results.map(b => b.name)
const withVersions = results.filter(hasVersion).length

t.ok(names.indexOf('chrome') >= 0, 'found chrome')
t.ok(names.indexOf('firefox') >= 0, 'found firefox')
t.ok(names.indexOf('ie') >= 0, 'found ie')

t.equal(withVersions, results.length, 'have version numbers')
})

test('detect chrome', function (t) {
detect('chrome', function (err, results) {
t.ifError(err, 'no error')
Expand All @@ -370,6 +382,20 @@ test('detect chrome', function (t) {
})
})

test('detect chrome with promise', async function (t) {
const results = await detect('chrome')
const names = results.filter(b => b.name === 'chrome')
t.equal(names.length, results.length, 'have names')

const versions = results.filter(hasVersion)
t.equal(versions.length, results.length, 'have version numbers')

if (argv.canary) {
const msg = 'found ' + results.length + ' chrome versions'
t.ok(results.length >= 2, msg)
}
})

test('detect chrome and firefox', function (t) {
t.plan(4)

Expand All @@ -385,6 +411,16 @@ test('detect chrome and firefox', function (t) {
})
})

test('detect chrome and firefox with promise', async function (t) {
const results = await detect(['chrome', 'firefox'])
const names = results.map(b => b.name)
const withVersions = results.filter(hasVersion).length

t.ok(names.indexOf('chrome') >= 0, 'found chrome')
t.ok(names.indexOf('firefox') >= 0, 'found firefox')
t.ok(withVersions >= 2, 'have version numbers')
})

maybe(argv.opera)('detect all opera versions', function (t) {
t.plan(3)

Expand Down Expand Up @@ -445,7 +481,7 @@ test('no result asynchronicity', function (t) {

let async = false

detect({ browsers: browsers }, function (err, results) {
detect({ browsers }, function (err, results) {
t.ifError(err, 'no error')
t.equal(results.length, 0)
t.equal(async, true)
Expand All @@ -454,6 +490,20 @@ test('no result asynchronicity', function (t) {
async = true
})

test('no result with promise', async function (t) {
const browsers = {
beep: {
bin: 'beep.exe',
find: function () {
this.file()
}
}
}

const results = await detect({ browsers })
t.equal(results.length, 0)
})

function hasVersion (b) {
return b.version && b.version.match(/[\d.]+/)
}
Expand Down

0 comments on commit ca8d84e

Please sign in to comment.