laucher: add Ramda and refactor code a little

This commit is contained in:
Gleb Bahmutov
2017-05-24 12:00:43 -04:00
parent 762d6d0754
commit f4ca92013e
3 changed files with 39 additions and 23 deletions
+5 -1
View File
@@ -10,5 +10,9 @@ if (!module.parent) {
// quick way to check if TS is working
console.log('Launcher project exports')
console.log(launcher)
console.log('please use it as a module, not from CLI')
console.log('⛔️ please use it as a module, not from CLI')
launcher.detect().then(browsers => {
console.log('detected %d browser(s)', browsers.length)
console.log(browsers)
}, console.error)
}
+31 -21
View File
@@ -1,38 +1,48 @@
import {log} from '../log'
import {NotInstalledError} from '../types'
import {prop, tap} from 'ramda'
import execa = require('execa')
import fs = require('fs-extra')
import path = require('path')
import plist = require('plist')
export function parse (p: string, prop: string) {
export function parse (p: string, property: string) {
const pl = path.join(p, 'Contents', 'Info.plist')
log('reading property file "%s"', pl)
const failed = (e: Error) => {
const msg = `Info.plist not found: ${pl}
${e.message}`
const err = new Error(msg) as NotInstalledError
err.notInstalled = true
throw err
}
return fs.readFile(pl, 'utf8')
.then(str => plist.parse(str))
.then(x => x[prop])
.catch((e) => {
const msg = `Info.plist not found: ${pl}
${e.message}`
const err = new Error(msg) as NotInstalledError
err.notInstalled = true
throw err
})
.then(plist.parse)
.then(prop(property))
.catch(failed)
}
export function find (id: string): Promise<string> {
const cmd = `mdfind 'kMDItemCFBundleIdentifier=="${id}"' | head -1`
log('looking for bundle id %s using command: %s', id, cmd)
const logFound = (str: string) => {
log('found %s at %s', id, str)
return str
}
const failedToFind = () => {
log('could not find %s', id)
const err = new Error(`Browser not installed: ${id}`) as NotInstalledError
err.notInstalled = true
throw err
}
return execa.shell(cmd)
.then(result => result.stdout)
.then((str: string) => {
log('found %s at %s', id, str)
return str
})
.catch(() => {
log('could not find %s', id)
const err = new Error(`Browser not installed: ${id}`) as NotInstalledError
err.notInstalled = true
throw err
})
.then(prop('stdout'))
.then(tap(logFound))
.catch(failedToFind)
}
+3 -1
View File
@@ -37,11 +37,13 @@
"typescript": "2.3.2"
},
"dependencies": {
"@types/ramda": "0.0.10",
"bluebird": "^3.3.5",
"debug": "^2.6.6",
"execa": "^0.6.3",
"fs-extra": "^3.0.0",
"lodash": "^4.11.1",
"plist": "^1.2.0"
"plist": "^1.2.0",
"ramda": "^0.23.0"
}
}