2018-04-25 14:00:46 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-04-25 14:00:46 +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 20:32:37 +00:00
|
|
|
* @format
|
2018-04-25 14:00:46 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const path = require('path');
|
2018-08-30 23:19:38 +00:00
|
|
|
const findPlugins = require('../local-cli/core/findPlugins');
|
2018-04-25 14:00:46 +00:00
|
|
|
|
2018-09-27 20:08:00 +00:00
|
|
|
const REACT_NATIVE_CI = process.cwd() === path.resolve(__dirname, '..');
|
|
|
|
|
|
|
|
let pluginsPath;
|
|
|
|
|
|
|
|
if (REACT_NATIVE_CI) {
|
|
|
|
pluginsPath = '..';
|
|
|
|
} else {
|
|
|
|
pluginsPath = '../../../';
|
|
|
|
}
|
|
|
|
|
|
|
|
const plugins = findPlugins([path.resolve(__dirname, pluginsPath)]);
|
2018-08-30 23:19:38 +00:00
|
|
|
|
|
|
|
// Detect out-of-tree platforms and add them to the whitelists
|
|
|
|
const pluginRoots /*: Array<
|
|
|
|
string,
|
|
|
|
> */ = plugins.haste.providesModuleNodeModules.map(
|
|
|
|
name => path.resolve(__dirname, '../../', name) + path.sep,
|
|
|
|
);
|
|
|
|
|
|
|
|
const pluginNameReducers /*: Array<
|
|
|
|
[RegExp, string],
|
|
|
|
> */ = plugins.haste.platforms.map(name => [
|
|
|
|
new RegExp(`^(.*)\.(${name})$`),
|
|
|
|
'$1',
|
|
|
|
]);
|
|
|
|
|
|
|
|
const ROOTS = [path.resolve(__dirname, '..') + path.sep, ...pluginRoots];
|
2018-04-25 14:00:46 +00:00
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
const BLACKLISTED_PATTERNS /*: Array<RegExp> */ = [
|
2018-07-17 23:26:47 +00:00
|
|
|
/.*[\\\/]__(mocks|tests)__[\\\/].*/,
|
|
|
|
/^Libraries[\\\/]Animated[\\\/]src[\\\/]polyfills[\\\/].*/,
|
|
|
|
/^Libraries[\\\/]Renderer[\\\/]fb[\\\/].*/,
|
2018-04-25 14:00:46 +00:00
|
|
|
];
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
const WHITELISTED_PREFIXES /*: Array<string> */ = [
|
2018-04-25 14:00:46 +00:00
|
|
|
'IntegrationTests',
|
|
|
|
'Libraries',
|
|
|
|
'ReactAndroid',
|
|
|
|
'RNTester',
|
|
|
|
];
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
const NAME_REDUCERS /*: Array<[RegExp, string]> */ = [
|
2018-04-25 14:00:46 +00:00
|
|
|
// extract basename
|
2018-07-17 23:26:47 +00:00
|
|
|
[/^(?:.*[\\\/])?([a-zA-Z0-9$_.-]+)$/, '$1'],
|
2018-04-25 14:00:46 +00:00
|
|
|
// strip .js/.js.flow suffix
|
|
|
|
[/^(.*)\.js(\.flow)?$/, '$1'],
|
2018-08-30 23:19:38 +00:00
|
|
|
// strip platform suffix
|
|
|
|
[/^(.*)\.(android|ios|native)$/, '$1'],
|
|
|
|
// strip plugin platform suffixes
|
|
|
|
...pluginNameReducers,
|
2018-04-25 14:00:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const haste = {
|
|
|
|
/*
|
|
|
|
* @return {string|void} hasteName for module at filePath; or undefined if
|
|
|
|
* filePath is not a haste module
|
|
|
|
*/
|
|
|
|
getHasteName(
|
2018-05-11 20:32:37 +00:00
|
|
|
filePath /*: string */,
|
|
|
|
sourceCode /*: ?string */,
|
|
|
|
) /*: string | void */ {
|
2018-04-25 14:00:46 +00:00
|
|
|
if (!isHastePath(filePath)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasteName = NAME_REDUCERS.reduce(
|
|
|
|
(name, [pattern, replacement]) => name.replace(pattern, replacement),
|
2018-05-11 20:32:37 +00:00
|
|
|
filePath,
|
2018-04-25 14:00:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return hasteName;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-05-11 20:32:37 +00:00
|
|
|
function isHastePath(filePath /*: string */) /*: boolean */ {
|
2018-04-25 14:00:46 +00:00
|
|
|
if (!filePath.endsWith('.js') && !filePath.endsWith('.js.flow')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:05:31 +00:00
|
|
|
const root = ROOTS.find(r => filePath.startsWith(r));
|
|
|
|
if (!root) {
|
2018-04-25 14:00:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:05:31 +00:00
|
|
|
filePath = filePath.substr(root.length);
|
2018-04-25 14:00:46 +00:00
|
|
|
if (BLACKLISTED_PATTERNS.some(pattern => pattern.test(filePath))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return WHITELISTED_PREFIXES.some(prefix => filePath.startsWith(prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = haste;
|