2018-04-25 14:00:46 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
*
|
|
|
|
* 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-07-02 18:05:31 +00:00
|
|
|
const ROOTS = [
|
|
|
|
path.resolve(__dirname, '..') + path.sep,
|
|
|
|
path.resolve(__dirname, '../../react-native-windows') + path.sep,
|
|
|
|
];
|
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'],
|
|
|
|
// strip .android/.ios/.native/.web suffix
|
2018-07-02 18:05:31 +00:00
|
|
|
[/^(.*)\.(android|ios|native|web|windows)$/, '$1'],
|
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;
|