2016-11-14 19:12:24 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-11-14 19:12:24 +00:00
|
|
|
*
|
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.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2016-11-14 19:12:24 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
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;
|
|
|
|
|
2018-09-13 11:28:10 +00:00
|
|
|
const RNPM_PLUGIN_PATTERNS = [/^rnpm-plugin-/, /^@(.*)\/rnpm-plugin-/];
|
|
|
|
|
|
|
|
const REACT_NATIVE_PLUGIN_PATTERNS = [
|
|
|
|
/^react-native-/,
|
|
|
|
/^@(.*)\/react-native-/,
|
|
|
|
/^@react-native(.*)\/(?!rnpm-plugin-)/,
|
|
|
|
];
|
|
|
|
|
2016-05-20 11:52:08 +00:00
|
|
|
/**
|
|
|
|
* Filter dependencies by name pattern
|
|
|
|
* @param {String} dependency Name of the dependency
|
|
|
|
* @return {Boolean} If dependency is a rnpm plugin
|
|
|
|
*/
|
2018-09-13 11:28:10 +00:00
|
|
|
const isRNPMPlugin = dependency =>
|
|
|
|
RNPM_PLUGIN_PATTERNS.some(pattern => pattern.test(dependency));
|
2018-05-11 19:43:49 +00:00
|
|
|
const isReactNativePlugin = dependency =>
|
2018-09-13 11:28:10 +00:00
|
|
|
REACT_NATIVE_PLUGIN_PATTERNS.some(pattern => pattern.test(dependency));
|
2016-05-20 11:52:08 +00:00
|
|
|
|
2018-05-11 19:43:49 +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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const findPluginsInReactNativePackage = pjson => {
|
2016-08-03 08:49:40 +00:00
|
|
|
if (!pjson.rnpm || !pjson.rnpm.plugin) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(pjson.name, pjson.rnpm.plugin);
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const findPlatformsInPackage = pjson => {
|
2018-02-08 17:45:16 +00:00
|
|
|
if (!pjson.rnpm || !pjson.rnpm.platform) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(pjson.name, pjson.rnpm.platform);
|
|
|
|
};
|
|
|
|
|
2018-08-30 23:19:38 +00:00
|
|
|
const getEmptyPluginConfig = () => ({
|
|
|
|
commands: [],
|
|
|
|
platforms: [],
|
|
|
|
haste: {
|
|
|
|
platforms: [],
|
|
|
|
providesModuleNodeModules: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const findHasteConfigInPackageAndConcat = (pjson, haste) => {
|
|
|
|
if (!pjson.rnpm || !pjson.rnpm.haste) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let pkgHaste = pjson.rnpm.haste;
|
|
|
|
|
|
|
|
if (pkgHaste.platforms) {
|
|
|
|
haste.platforms = haste.platforms.concat(pkgHaste.platforms);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pkgHaste.providesModuleNodeModules) {
|
|
|
|
haste.providesModuleNodeModules = haste.providesModuleNodeModules.concat(
|
|
|
|
pkgHaste.providesModuleNodeModules,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-11 19:43:49 +00:00
|
|
|
const findPluginInFolder = folder => {
|
2016-08-03 08:49:40 +00:00
|
|
|
const pjson = readPackage(folder);
|
|
|
|
|
|
|
|
if (!pjson) {
|
2018-08-30 23:19:38 +00:00
|
|
|
return getEmptyPluginConfig();
|
2016-05-20 11:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const deps = union(
|
|
|
|
Object.keys(pjson.dependencies || {}),
|
2018-05-11 19:43:49 +00:00
|
|
|
Object.keys(pjson.devDependencies || {}),
|
2016-05-20 11:52:08 +00:00
|
|
|
);
|
|
|
|
|
2018-08-30 23:19:38 +00:00
|
|
|
return deps.reduce((acc, pkg) => {
|
|
|
|
let commands = acc.commands;
|
|
|
|
let platforms = acc.platforms;
|
|
|
|
let haste = acc.haste;
|
|
|
|
if (isRNPMPlugin(pkg)) {
|
|
|
|
commands = commands.concat(pkg);
|
|
|
|
}
|
|
|
|
if (isReactNativePlugin(pkg)) {
|
|
|
|
const pkgJson = readPackage(path.join(folder, 'node_modules', pkg));
|
|
|
|
if (pkgJson) {
|
|
|
|
commands = commands.concat(findPluginsInReactNativePackage(pkgJson));
|
|
|
|
platforms = platforms.concat(findPlatformsInPackage(pkgJson));
|
|
|
|
findHasteConfigInPackageAndConcat(pkgJson, haste);
|
2016-08-03 08:49:40 +00:00
|
|
|
}
|
2018-08-30 23:19:38 +00:00
|
|
|
}
|
|
|
|
return {commands: commands, platforms: platforms, haste: haste};
|
|
|
|
}, getEmptyPluginConfig());
|
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))),
|
2018-05-11 19:43:49 +00:00
|
|
|
platforms: uniq(flatten(plugins.map(p => p.platforms))),
|
2018-08-30 23:19:38 +00:00
|
|
|
haste: {
|
|
|
|
platforms: uniq(flatten(plugins.map(p => p.haste.platforms))),
|
|
|
|
providesModuleNodeModules: uniq(
|
|
|
|
flatten(plugins.map(p => p.haste.providesModuleNodeModules)),
|
|
|
|
),
|
|
|
|
},
|
2018-02-08 17:45:16 +00:00
|
|
|
};
|
2016-05-20 11:52:08 +00:00
|
|
|
};
|