Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
72
src/plugins/index.js
Normal file
72
src/plugins/index.js
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright 2018-present Facebook.
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
* @format
|
||||
*/
|
||||
|
||||
import {GK} from 'sonar';
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import * as Sonar from 'sonar';
|
||||
import {SonarBasePlugin} from '../plugin.js';
|
||||
|
||||
const plugins = new Map();
|
||||
|
||||
// expose Sonar and exact globally for dynamically loaded plugins
|
||||
window.React = React;
|
||||
window.ReactDOM = ReactDOM;
|
||||
window.Sonar = Sonar;
|
||||
|
||||
const addIfNotAdded = plugin => {
|
||||
if (!plugins.has(plugin.name)) {
|
||||
plugins.set(plugin.name, plugin);
|
||||
}
|
||||
};
|
||||
|
||||
let disabledPlugins = [];
|
||||
try {
|
||||
disabledPlugins =
|
||||
JSON.parse(window.process.env.CONFIG || '{}').disabledPlugins || [];
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
// Load dynamic plugins
|
||||
try {
|
||||
JSON.parse(window.process.env.PLUGINS || '[]').forEach(addIfNotAdded);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
// DefaultPlugins that are included in the bundle.
|
||||
// List of defaultPlugins is written at build time
|
||||
let bundledPlugins = [];
|
||||
try {
|
||||
bundledPlugins = window.electronRequire('./defaultPlugins/index.json');
|
||||
} catch (e) {}
|
||||
bundledPlugins
|
||||
.map(plugin => ({
|
||||
...plugin,
|
||||
out: './' + plugin.out,
|
||||
}))
|
||||
.forEach(addIfNotAdded);
|
||||
|
||||
export default Array.from(plugins.values())
|
||||
.map(plugin => {
|
||||
if (
|
||||
(plugin.gatekeeper && !GK.get(plugin.gatekeeper)) ||
|
||||
disabledPlugins.indexOf(plugin.name) > -1
|
||||
) {
|
||||
return null;
|
||||
} else {
|
||||
try {
|
||||
return window.electronRequire(plugin.out);
|
||||
} catch (e) {
|
||||
console.error(plugin, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
})
|
||||
.filter(Boolean)
|
||||
.filter(plugin => plugin.prototype instanceof SonarBasePlugin);
|
||||
Reference in New Issue
Block a user