Files
flipper/desktop/plugins/public/navigation/util/appMatchPatterns.tsx
Michel Weststrate 3882357579 Factor out realDevice [7/n]
Summary: `device.realDevice` was the escape hatch used in Sandy plugins to give access to device specific features like taking screenshots, clearing logs or accessing `adb`. Since in decapitated Flipper that won't be possible anymore (since plugins run in the client but device implementations on the server), all escape hatches have been bridged in this stack, and we can get of the `realDevice` interaction, by explicitly exposing those cases, which makes it type safe as well.

Reviewed By: passy

Differential Revision: D31079509

fbshipit-source-id: c9ec2e044d0dec0ccb1de287cf424907b198f818
2021-09-22 09:03:33 -07:00

61 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 {getAppPath} from 'flipper';
import {AppMatchPattern} from '../types';
import {Device} from 'flipper-plugin';
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: Device,
) => {
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'));
}
});
};