Files
flipper/desktop/plugins/public/navigation/util/appMatchPatterns.tsx
Michel Weststrate 2d838efd4d Separate device in server and client version [2/n]
Summary:
This stack takes care of handling care of moving all device interactions over the (possible) async channel FlipperServer. The FlipperServer interface (see previous diff) allows listening to specific server events using `on`, and emit commands to be executed by the server by using `exec` (e.g. `exec('take-screenshot', serial) => Promise<buffer>`).

FlipperServerImpl implements this interface on the server side.

The device implementations are split as follows

```
server / backend process:

ServerDevice
- iOSDevice
- AndroidDevice
- MetroDevice
- DummyDevice
- Mac/Windows Device

frontend / ui:

BaseDevice: a normal connected, device, implements device apis as they already existed
- ArchivedDevice (note that this doesn't have a server counterpart)
- TestDevice (for unit tests, with stubbed backend communication)

```

All features of devices are for simplicity unified (since the deviations are small), where specific device types might not implement certain features like taking screenshots or running shell commands.

To avoid making this diff unnecessarily big, some open Todo's will be addressed later in this stack, and it shouldn't be landed alone.

Reviewed By: timur-valiev

Differential Revision: D30909346

fbshipit-source-id: cce0bee94fdd5db59bebe3577a6084219a038719
2021-09-22 09:03:32 -07:00

60 lines
1.6 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import fs from 'fs';
import path from 'path';
import {BaseDevice, getAppPath} from 'flipper';
import {AppMatchPattern} from '../types';
let patternsPath: string | undefined;
function getPatternsBasePath() {
return (patternsPath = patternsPath ?? path.join(getAppPath(), 'facebook'));
}
const extractAppNameFromSelectedApp = (selectedApp: string | null) => {
if (selectedApp == null) {
return null;
} else {
return selectedApp.split('#')[0];
}
};
export const getAppMatchPatterns = (
selectedApp: string | null,
device: BaseDevice,
) => {
return new Promise<Array<AppMatchPattern>>((resolve, reject) => {
const appName = extractAppNameFromSelectedApp(selectedApp);
if (appName === 'Facebook') {
let filename: string;
if (device.os === 'Android') {
filename = 'facebook-match-patterns-android.json';
} else if (device.os === 'iOS') {
filename = 'facebook-match-patterns-ios.json';
} else {
return;
}
const patternsFilePath = path.join(getPatternsBasePath(), filename);
fs.readFile(patternsFilePath, (err, data) => {
if (err) {
reject(err);
} else {
resolve(JSON.parse(data.toString()));
}
});
} else if (appName != null) {
console.log('No rule for app ' + appName);
resolve([]);
} else {
reject(new Error('selectedApp was null'));
}
});
};