api.js 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.makeTagsFilter = makeTagsFilter;
exports.makeAppFilter = makeAppFilter;
exports.makeMatchFilter = makeMatchFilter;
exports.makeCustomFilter = makeCustomFilter;
exports.logkitty = logkitty;
Object.defineProperty(exports, "Entry", {
  enumerable: true,
  get: function () {
    return _types.Entry;
  }
});
Object.defineProperty(exports, "AndroidPriority", {
  enumerable: true,
  get: function () {
    return _constants.Priority;
  }
});
Object.defineProperty(exports, "IosPriority", {
  enumerable: true,
  get: function () {
    return _constants2.Priority;
  }
});
Object.defineProperty(exports, "formatEntry", {
  enumerable: true,
  get: function () {
    return _formatters.formatEntry;
  }
});
Object.defineProperty(exports, "formatError", {
  enumerable: true,
  get: function () {
    return _formatters.formatError;
  }
});

var _events = require("events");

var _types = require("./types");

var _AndroidFilter = _interopRequireDefault(require("./android/AndroidFilter"));

var _AndroidParser = _interopRequireDefault(require("./android/AndroidParser"));

var _adb = require("./android/adb");

var _constants = require("./android/constants");

var _IosParser = _interopRequireDefault(require("./ios/IosParser"));

var _IosFilter = _interopRequireDefault(require("./ios/IosFilter"));

var _simulator = require("./ios/simulator");

var _errors = require("./errors");

var _constants2 = require("./ios/constants");

var _formatters = require("./formatters");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* Common */

/* Android */

/* iOS */

/* Exports */
function makeTagsFilter(...tags) {
  return (platform, minPriority) => {
    const filter = platform === 'android' ? new _AndroidFilter.default(minPriority) : new _IosFilter.default(minPriority);
    filter.setFilterByTag(tags);
    return filter;
  };
}

function makeAppFilter(appIdentifier) {
  return (platform, minPriority, adbPath) => {
    if (platform !== 'android') {
      throw new Error('App filter is only available for Android');
    }

    const filter = new _AndroidFilter.default(minPriority);
    filter.setFilterByApp(appIdentifier, adbPath);
    return filter;
  };
}

function makeMatchFilter(...regexes) {
  return (platform, minPriority) => {
    const filter = platform === 'android' ? new _AndroidFilter.default(minPriority) : new _IosFilter.default(minPriority);
    filter.setFilterByMatch(regexes);
    return filter;
  };
}

function makeCustomFilter(...patterns) {
  return (platform, minPriority) => {
    if (platform !== 'android') {
      throw new Error('Custom filter is only available for Android');
    }

    const filter = new _AndroidFilter.default(minPriority);
    filter.setCustomFilter(patterns);
    return filter;
  };
}

function logkitty(options) {
  const {
    platform,
    adbPath,
    priority,
    filter: createFilter
  } = options;
  const emitter = new _events.EventEmitter();

  if (!['ios', 'android'].some(availablePlatform => availablePlatform === platform)) {
    throw new Error(`Platform ${platform} is not supported`);
  }

  const parser = platform === 'android' ? new _AndroidParser.default() : new _IosParser.default();
  let filter;

  if (createFilter) {
    filter = createFilter(platform, priority, adbPath);
  } else {
    filter = platform === 'android' ? new _AndroidFilter.default(priority) : new _IosFilter.default(priority);
  }

  const loggingProcess = platform === 'android' ? (0, _adb.runAndroidLoggingProcess)(adbPath) : (0, _simulator.runSimulatorLoggingProcess)();
  process.on('exit', () => {
    loggingProcess.kill();
    emitter.emit('exit');
  });
  loggingProcess.stderr.on('data', errorData => {
    if (platform === 'ios' && errorData.toString().includes('No devices are booted.')) {
      emitter.emit('error', new _errors.CodeError(_errors.ERR_IOS_NO_SIMULATORS_BOOTED, 'No simulators are booted.'));
    } else {
      emitter.emit('error', new Error(errorData.toString()));
    }
  });
  loggingProcess.stdout.on('data', raw => {
    let entryToLog;

    try {
      const messages = parser.splitMessages(raw.toString());
      const entries = parser.parseMessages(messages);
      entries.forEach(entry => {
        if (filter.shouldInclude(entry)) {
          entryToLog = entry;
        }
      });
    } catch (error) {
      emitter.emit('error', error);
    }

    if (entryToLog) {
      emitter.emit('entry', entryToLog);
    }
  });
  loggingProcess.stdout.on('error', error => {
    emitter.emit('error', error);
    emitter.emit('exit');
  });
  return emitter;
}
//# sourceMappingURL=api.js.map