2016-11-14 19:12:24 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-11-14 19:12:24 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
const path = require('path');
|
|
|
|
const union = require('lodash').union;
|
|
|
|
const uniq = require('lodash').uniq;
|
|
|
|
const flatten = require('lodash').flatten;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter dependencies by name pattern
|
|
|
|
* @param {String} dependency Name of the dependency
|
|
|
|
* @return {Boolean} If dependency is a rnpm plugin
|
|
|
|
*/
|
2016-08-03 08:49:40 +00:00
|
|
|
const isRNPMPlugin = (dependency) => dependency.indexOf('rnpm-plugin-') === 0;
|
|
|
|
const isReactNativePlugin = (dependency) => dependency.indexOf('react-native-') === 0;
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2016-08-03 08:49:40 +00:00
|
|
|
const readPackage = (folder) => {
|
2016-05-20 11:52:08 +00:00
|
|
|
try {
|
2016-08-03 08:49:40 +00:00
|
|
|
return require(path.join(folder, 'package.json'));
|
2016-05-20 11:52:08 +00:00
|
|
|
} catch (e) {
|
2016-08-03 08:49:40 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const findPluginsInReactNativePackage = (pjson) => {
|
|
|
|
if (!pjson.rnpm || !pjson.rnpm.plugin) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(pjson.name, pjson.rnpm.plugin);
|
|
|
|
};
|
|
|
|
|
2018-02-08 17:45:16 +00:00
|
|
|
const findPlatformsInPackage = (pjson) => {
|
|
|
|
if (!pjson.rnpm || !pjson.rnpm.platform) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(pjson.name, pjson.rnpm.platform);
|
|
|
|
};
|
|
|
|
|
2016-08-03 08:49:40 +00:00
|
|
|
const findPluginInFolder = (folder) => {
|
|
|
|
const pjson = readPackage(folder);
|
|
|
|
|
|
|
|
if (!pjson) {
|
2018-02-08 17:45:16 +00:00
|
|
|
return {commands: [], platforms: []};
|
2016-05-20 11:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const deps = union(
|
|
|
|
Object.keys(pjson.dependencies || {}),
|
|
|
|
Object.keys(pjson.devDependencies || {})
|
|
|
|
);
|
|
|
|
|
2016-08-03 08:49:40 +00:00
|
|
|
return deps.reduce(
|
|
|
|
(acc, pkg) => {
|
2018-02-08 17:45:16 +00:00
|
|
|
let commands = acc.commands;
|
|
|
|
let platforms = acc.platforms;
|
2016-08-03 08:49:40 +00:00
|
|
|
if (isRNPMPlugin(pkg)) {
|
2018-02-08 17:45:16 +00:00
|
|
|
commands = commands.concat(pkg);
|
2016-08-03 08:49:40 +00:00
|
|
|
}
|
|
|
|
if (isReactNativePlugin(pkg)) {
|
|
|
|
const pkgJson = readPackage(path.join(folder, 'node_modules', pkg));
|
2018-02-08 17:45:16 +00:00
|
|
|
if (pkgJson) {
|
|
|
|
commands = commands.concat(findPluginsInReactNativePackage(pkgJson));
|
|
|
|
platforms = platforms.concat(findPlatformsInPackage(pkgJson));
|
2016-08-03 08:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-08 17:45:16 +00:00
|
|
|
return {commands: commands, platforms: platforms};
|
2016-08-03 08:49:40 +00:00
|
|
|
},
|
2018-02-08 17:45:16 +00:00
|
|
|
{commands: [], platforms: []}
|
2016-08-03 08:49:40 +00:00
|
|
|
);
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find plugins in package.json of the given folder
|
|
|
|
* @param {String} folder Path to the folder to get the package.json from
|
2018-02-08 17:45:16 +00:00
|
|
|
* @type {Object} Object of commands and platform plugins
|
2016-05-20 11:52:08 +00:00
|
|
|
*/
|
|
|
|
module.exports = function findPlugins(folders) {
|
2018-02-08 17:45:16 +00:00
|
|
|
const plugins = folders.map(findPluginInFolder);
|
|
|
|
return {
|
|
|
|
commands: uniq(flatten(plugins.map(p => p.commands))),
|
|
|
|
platforms: uniq(flatten(plugins.map(p => p.platforms)))
|
|
|
|
};
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|