Plugin folders re-structuring

Summary:
Here I'm changing plugin repository structure to allow re-using of shared packages between both public and fb-internal plugins, and to ensure that public plugins has their own yarn.lock as this will be required to implement reproducible jobs checking plugin compatibility with released flipper versions.

Please note that there are a lot of moved files in this diff, make sure to click "Expand all" to see all that actually changed (there are not much of them actually).

New proposed structure for plugin packages:
```
- root
- node_modules - modules included into Flipper: flipper, flipper-plugin, react, antd, emotion
-- plugins
 --- node_modules - modules used by both public and fb-internal plugins (shared libs will be linked here, see D27034936)
 --- public
---- node_modules - modules used by public plugins
---- pluginA
----- node_modules - modules used by plugin A exclusively
---- pluginB
----- node_modules - modules used by plugin B exclusively
 --- fb
---- node_modules - modules used by fb-internal plugins
---- pluginC
----- node_modules - modules used by plugin C exclusively
---- pluginD
----- node_modules - modules used by plugin D exclusively
```
I've moved all public plugins under dir "plugins/public" and excluded them from root yarn workspaces. Instead, they will have their own yarn workspaces config and yarn.lock and they will use flipper modules as peer dependencies.

Reviewed By: mweststrate

Differential Revision: D27034108

fbshipit-source-id: c2310e3c5bfe7526033f51b46c0ae40199fd7586
This commit is contained in:
Anton Nikolaev
2021-04-09 05:15:14 -07:00
committed by Facebook GitHub Bot
parent 32bf4c32c2
commit b3274a8450
137 changed files with 2133 additions and 371 deletions

View File

@@ -0,0 +1,61 @@
/**
* 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, AndroidDevice, IOSDevice} from 'flipper';
import {AppMatchPattern} from '../types';
import {remote} from 'electron';
let patternsPath: string | undefined;
function getPatternsBasePath() {
return (patternsPath =
patternsPath ?? path.join(remote.app.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 instanceof AndroidDevice) {
filename = 'facebook-match-patterns-android.json';
} else if (device instanceof IOSDevice) {
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'));
}
});
};

View File

@@ -0,0 +1,103 @@
/**
* 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 {
URI,
Bookmark,
AutoCompleteProvider,
AutoCompleteLineItem,
AppMatchPattern,
} from '../types';
export function DefaultProvider(): AutoCompleteProvider {
return {
icon: 'caution',
matchPatterns: new Map<string, URI>(),
};
}
export const bookmarksToAutoCompleteProvider = (
bookmarks: Map<URI, Bookmark>,
) => {
const autoCompleteProvider = {
icon: 'bookmark',
matchPatterns: new Map<string, URI>(),
} as AutoCompleteProvider;
bookmarks.forEach((bookmark, uri) => {
const matchPattern = bookmark.commonName + ' - ' + uri;
autoCompleteProvider.matchPatterns.set(matchPattern, uri);
});
return autoCompleteProvider;
};
export const appMatchPatternsToAutoCompleteProvider = (
appMatchPatterns: Array<AppMatchPattern>,
) => {
const autoCompleteProvider = {
icon: 'mobile',
matchPatterns: new Map<string, URI>(),
};
appMatchPatterns.forEach((appMatchPattern) => {
const matchPattern =
appMatchPattern.className + ' - ' + appMatchPattern.pattern;
autoCompleteProvider.matchPatterns.set(
matchPattern,
appMatchPattern.pattern,
);
});
return autoCompleteProvider;
};
export const filterMatchPatterns = (
matchPatterns: Map<string, URI>,
query: URI,
maxItems: number,
) => {
const filteredPatterns = new Map<string, URI>();
for (const [pattern, uri] of matchPatterns) {
if (filteredPatterns.size >= maxItems) {
break;
} else if (pattern.toLowerCase().includes(query.toLowerCase())) {
filteredPatterns.set(pattern, uri);
}
}
return filteredPatterns;
};
const filterProvider = (
provider: AutoCompleteProvider,
query: string,
maxItems: number,
) => {
return {
...provider,
matchPatterns: filterMatchPatterns(provider.matchPatterns, query, maxItems),
};
};
export const filterProvidersToLineItems = (
providers: Array<AutoCompleteProvider>,
query: string,
maxItems: number,
) => {
let itemsLeft = maxItems;
const lineItems = new Array<AutoCompleteLineItem>(0);
for (const provider of providers) {
const filteredProvider = filterProvider(provider, query, itemsLeft);
filteredProvider.matchPatterns.forEach((uri, matchPattern) => {
lineItems.push({
icon: provider.icon,
matchPattern,
uri,
});
});
itemsLeft -= filteredProvider.matchPatterns.size;
}
return lineItems;
};

View File

@@ -0,0 +1,104 @@
/**
* 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 {Bookmark} from '../types';
const FLIPPER_NAVIGATION_PLUGIN_DB = 'flipper_navigation_plugin_db';
const FLIPPER_NAVIGATION_PLUGIN_DB_VERSION = 1;
const BOOKMARKS_KEY = 'bookmarks';
const createBookmarksObjectStore = (db: IDBDatabase) => {
return new Promise<void>((resolve, reject) => {
if (!db.objectStoreNames.contains(BOOKMARKS_KEY)) {
const bookmarksObjectStore = db.createObjectStore(BOOKMARKS_KEY, {
keyPath: 'uri',
});
bookmarksObjectStore.transaction.oncomplete = () => resolve();
bookmarksObjectStore.transaction.onerror = () =>
reject(bookmarksObjectStore.transaction.error);
} else {
resolve();
}
});
};
const initializeNavigationPluginDB = (db: IDBDatabase) => {
return Promise.all([createBookmarksObjectStore(db)]);
};
const openNavigationPluginDB: () => Promise<IDBDatabase> = () => {
return new Promise((resolve, reject) => {
const openRequest = window.indexedDB.open(
FLIPPER_NAVIGATION_PLUGIN_DB,
FLIPPER_NAVIGATION_PLUGIN_DB_VERSION,
);
openRequest.onupgradeneeded = () => {
const db = openRequest.result;
initializeNavigationPluginDB(db).then(() => resolve(db));
};
openRequest.onerror = () => reject(openRequest.error);
openRequest.onsuccess = () => resolve(openRequest.result);
});
};
export const writeBookmarkToDB = (bookmark: Bookmark) => {
return new Promise<void>((resolve, reject) => {
openNavigationPluginDB()
.then((db: IDBDatabase) => {
const bookmarksObjectStore = db
.transaction(BOOKMARKS_KEY, 'readwrite')
.objectStore(BOOKMARKS_KEY);
const request = bookmarksObjectStore.put(bookmark);
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
})
.catch(reject);
});
};
export const readBookmarksFromDB: () => Promise<Map<string, Bookmark>> = () => {
return new Promise((resolve, reject) => {
const bookmarks = new Map();
openNavigationPluginDB()
.then((db: IDBDatabase) => {
const bookmarksObjectStore = db
.transaction(BOOKMARKS_KEY)
.objectStore(BOOKMARKS_KEY);
const request = bookmarksObjectStore.openCursor();
request.onsuccess = () => {
const cursor = request.result;
if (cursor) {
const bookmark = cursor.value;
bookmarks.set(bookmark.uri, bookmark);
cursor.continue();
} else {
resolve(bookmarks);
}
};
request.onerror = () => reject(request.error);
})
.catch(reject);
});
};
export const removeBookmarkFromDB: (uri: string) => Promise<void> = (uri) => {
return new Promise<void>((resolve, reject) => {
openNavigationPluginDB()
.then((db: IDBDatabase) => {
const bookmarksObjectStore = db
.transaction(BOOKMARKS_KEY, 'readwrite')
.objectStore(BOOKMARKS_KEY);
const request = bookmarksObjectStore.delete(uri);
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
})
.catch(reject);
});
};

View File

@@ -0,0 +1,91 @@
/**
* 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 querystring from 'querystring';
export const validateParameter = (value: string, parameter: string) => {
return (
value &&
(parameterIsNumberType(parameter) ? !isNaN(parseInt(value, 10)) : true) &&
(parameterIsBooleanType(parameter)
? value === 'true' || value === 'false'
: true)
);
};
export const filterOptionalParameters = (uri: string) => {
return uri.replace(/[/&]?([^&?={}\/]*=)?{\?.*?}/g, '');
};
export const parseURIParameters = (query: string) => {
// get parameters from query string and store in Map
const parameters = query.split('?').splice(1).join('');
const parametersObj = querystring.parse(parameters);
const parametersMap = new Map<string, string>();
for (const key in parametersObj) {
parametersMap.set(key, parametersObj[key] as string);
}
return parametersMap;
};
export const parameterIsNumberType = (parameter: string) => {
const regExp = /^{(#|\?#)/g;
return regExp.test(parameter);
};
export const parameterIsBooleanType = (parameter: string) => {
const regExp = /^{(!|\?!)/g;
return regExp.test(parameter);
};
export const replaceRequiredParametersWithValues = (
uri: string,
values: Array<string>,
) => {
const parameterRegExp = /{[^?]*?}/g;
const replaceRegExp = /{[^?]*?}/;
let newURI = uri;
let index = 0;
let match = parameterRegExp.exec(uri);
while (match != null) {
newURI = newURI.replace(replaceRegExp, values[index]);
match = parameterRegExp.exec(uri);
index++;
}
return newURI;
};
export const getRequiredParameters = (uri: string) => {
const parameterRegExp = /{[^?]*?}/g;
const matches: Array<string> = [];
let match = parameterRegExp.exec(uri);
while (match != null) {
if (match[0]) {
matches.push(match[0]);
}
match = parameterRegExp.exec(uri);
}
return matches;
};
export const liveEdit = (uri: string, formValues: Array<string>) => {
const parameterRegExp = /({[^?]*?})/g;
const uriArray = uri.split(parameterRegExp);
return uriArray.reduce((acc, uriComponent, idx) => {
if (idx % 2 === 0 || !formValues[(idx - 1) / 2]) {
return acc + uriComponent;
} else {
return acc + formValues[(idx - 1) / 2];
}
});
};
export const stripQueryParameters = (uri: string) => {
return uri.replace(/\?.*$/g, '');
};