Skip to content

Commit

Permalink
Traversing further into scope directories
Browse files Browse the repository at this point in the history
  • Loading branch information
skang-rmn committed May 2, 2018
1 parent e8aba99 commit 4fa6ba2
Showing 1 changed file with 102 additions and 93 deletions.
195 changes: 102 additions & 93 deletions packages/insomnia-app/app/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,129 +50,138 @@ export async function init (): Promise<void> {
await getPlugins(true);
}

export async function getPlugins (force: boolean = false): Promise<Array<Plugin>> {
if (force) {
plugins = null;
}

if (!plugins) {
const settings = await models.settings.getOrCreate();
const extraPaths = settings.pluginPath.split(':').filter(p => p).map(resolveHomePath);

// Make sure the default directories exist
mkdirp.sync(PLUGIN_PATH);

// Also look in node_modules folder in each directory
const basePaths = [PLUGIN_PATH, ...extraPaths];
const extendedPaths = basePaths.map(p => path.join(p, 'node_modules'));
const allPaths = [...basePaths, ...extendedPaths];

// Store plugins in a map so that plugins with the same
// name only get added once
// TODO: Make this more complex and have the latest version always win
const pluginMap: {[string]: Plugin} = {
// "name": "module"
};

for (const p of CORE_PLUGINS) {
const pluginJson = global.require(`${p}/package.json`);
const pluginModule = global.require(p);
pluginMap[pluginJson.name] = _initPlugin(pluginJson, pluginModule);
async function _traversePluginPath (pluginMap: Object, allPaths: Array<string>) {
for (const p of allPaths) {
if (!fs.existsSync(p)) {
continue;
}

for (const p of allPaths) {
if (!fs.existsSync(p)) {
continue;
}
for (const filename of fs.readdirSync(p)) {
try {
const modulePath = path.join(p, filename);
const packageJSONPath = path.join(modulePath, 'package.json');

for (const filename of fs.readdirSync(p)) {
try {
const modulePath = path.join(p, filename);
const packageJSONPath = path.join(modulePath, 'package.json');
// Only read directories
if (!fs.statSync(modulePath).isDirectory()) {
continue;
}

// Only read directories
if (!fs.statSync(modulePath).isDirectory()) {
continue;
}
// Is it a scoped directory?
if (filename.startsWith('@')) {
await _traversePluginPath(pluginMap, [modulePath]);
}

// Is it a Node module?
if (!fs.readdirSync(modulePath).includes('package.json')) {
continue;
}
// Is it a Node module?
if (!fs.readdirSync(modulePath).includes('package.json')) {
continue;
}

// Delete `require` cache if plugin has been required before
for (const p of Object.keys(global.require.cache)) {
if (p.indexOf(modulePath) === 0) {
delete global.require.cache[p];
}
// Delete `require` cache if plugin has been required before
for (const p of Object.keys(global.require.cache)) {
if (p.indexOf(modulePath) === 0) {
delete global.require.cache[p];
}
}

// Use global.require() instead of require() because Webpack wraps require()
const pluginJson = global.require(packageJSONPath);

// Not an Insomnia plugin because it doesn't have the package.json['insomnia']
if (!pluginJson.hasOwnProperty('insomnia')) {
continue;
}
// Use global.require() instead of require() because Webpack wraps require()
const pluginJson = global.require(packageJSONPath);

// Delete require cache entry and re-require
const module = global.require(modulePath);

pluginMap[pluginJson.name] = _initPlugin(pluginJson || {}, module, modulePath);
console.log(`[plugin] Loaded ${modulePath}`);
} catch (err) {
showError({
title: 'Plugin Error',
message: 'Failed to load plugin ' + filename,
error: err
});
// Not an Insomnia plugin because it doesn't have the package.json['insomnia']
if (!pluginJson.hasOwnProperty('insomnia')) {
continue;
}

// Delete require cache entry and re-require
const module = global.require(modulePath);

pluginMap[pluginJson.name] = _initPlugin(pluginJson || {}, module, modulePath);
console.log(`[plugin] Loaded ${modulePath}`);
} catch (err) {
showError({
title: 'Plugin Error',
message: 'Failed to load plugin ' + filename,
error: err
});
}
}
}
}

export async function getPlugins (force: boolean = false): Promise<Array<Plugin>> {
if (force) {
plugins = null;
}

if (!plugins) {
const settings = await models.settings.getOrCreate();
const extraPaths = settings.pluginPath.split(':').filter(p => p).map(resolveHomePath);

// Make sure the default directories exist
mkdirp.sync(PLUGIN_PATH);

// Also look in node_modules folder in each directory
const basePaths = [PLUGIN_PATH, ...extraPaths];
const extendedPaths = basePaths.map(p => path.join(p, 'node_modules'));
const allPaths = [...basePaths, ...extendedPaths];

// Store plugins in a map so that plugins with the same
// name only get added once
// TODO: Make this more complex and have the latest version always win
const pluginMap: {[string]: Plugin} = {
// "name": "module"
};

plugins = Object.keys(pluginMap).map(name => pluginMap[name]);
for (const p of CORE_PLUGINS) {
const pluginJson = global.require(`${p}/package.json`);
const pluginModule = global.require(p);
pluginMap[pluginJson.name] = _initPlugin(pluginJson, pluginModule);
}

return plugins;
_traversePluginPath(pluginMap, allPaths);

plugins = Object.keys(pluginMap).map(name => pluginMap[name]);
}

return plugins;
}

export async function getTemplateTags (): Promise<Array<TemplateTag>> {
let extensions = [];
for (const plugin of await getPlugins()) {
const templateTags = plugin.module.templateTags || [];
extensions = [
...extensions,
...templateTags.map(tt => ({plugin: plugin.name, templateTag: tt}))
];
}
for (const plugin of await getPlugins()) {
const templateTags = plugin.module.templateTags || [];
extensions = [
...extensions,
...templateTags.map(tt => ({plugin: plugin.name, templateTag: tt}))
];
}

return extensions;
return extensions;
}

export async function getRequestHooks (): Promise<Array<RequestHook>> {
let functions = [];
for (const plugin of await getPlugins()) {
const moreFunctions = plugin.module.requestHooks || [];
functions = [
...functions,
...moreFunctions.map(hook => ({plugin, hook}))
];
}
for (const plugin of await getPlugins()) {
const moreFunctions = plugin.module.requestHooks || [];
functions = [
...functions,
...moreFunctions.map(hook => ({plugin, hook}))
];
}

return functions;
return functions;
}

export async function getResponseHooks (): Promise<Array<ResponseHook>> {
let functions = [];
for (const plugin of await getPlugins()) {
const moreFunctions = plugin.module.responseHooks || [];
functions = [
...functions,
...moreFunctions.map(hook => ({plugin, hook}))
];
}
for (const plugin of await getPlugins()) {
const moreFunctions = plugin.module.responseHooks || [];
functions = [
...functions,
...moreFunctions.map(hook => ({plugin, hook}))
];
}

return functions;
return functions;
}

function _initPlugin (packageJSON: Object, module: any, path: ?string): Plugin {
Expand Down

0 comments on commit 4fa6ba2

Please sign in to comment.