From f4ca92013e0081912be824fd45218de39fc08afd Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 24 May 2017 12:00:43 -0400 Subject: [PATCH] laucher: add Ramda and refactor code a little --- packages/launcher/index.js | 6 +++- packages/launcher/lib/darwin/util.ts | 52 +++++++++++++++++----------- packages/launcher/package.json | 4 ++- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/packages/launcher/index.js b/packages/launcher/index.js index ececac3273..5d033e3a1e 100644 --- a/packages/launcher/index.js +++ b/packages/launcher/index.js @@ -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) } diff --git a/packages/launcher/lib/darwin/util.ts b/packages/launcher/lib/darwin/util.ts index 498769caee..9512cfa31f 100644 --- a/packages/launcher/lib/darwin/util.ts +++ b/packages/launcher/lib/darwin/util.ts @@ -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 { 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) } diff --git a/packages/launcher/package.json b/packages/launcher/package.json index a5f5d512d5..15fdf16bec 100644 --- a/packages/launcher/package.json +++ b/packages/launcher/package.json @@ -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" } }