From 3aedbbbf968327156b0db1a105e55d5b43d5f774 Mon Sep 17 00:00:00 2001 From: Jean Lauliac Date: Wed, 31 May 2017 10:37:49 -0700 Subject: [PATCH] metro-bundler: ResolutionRequest: extract FileNameResolver Summary: I want to untangle `ResolutionRequest` once and for all, that starts by pulling stuff out :-) Reviewed By: cpojer Differential Revision: D5155316 fbshipit-source-id: a46ee9b40c6705edcac169adcfdffe25058ec810 --- .../DependencyGraph/FileNameResolver.js | 46 +++++++++++++++++++ .../DependencyGraph/ResolutionRequest.js | 30 +----------- 2 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 packager/src/node-haste/DependencyGraph/FileNameResolver.js diff --git a/packager/src/node-haste/DependencyGraph/FileNameResolver.js b/packager/src/node-haste/DependencyGraph/FileNameResolver.js new file mode 100644 index 000000000..bf0ab3856 --- /dev/null +++ b/packager/src/node-haste/DependencyGraph/FileNameResolver.js @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + * @format + */ + +'use strict'; + +const path = require('path'); + +export type Options = {| + +dirPath: string, + +doesFileExist: (filePath: string) => boolean, +|}; + +/** + * When resolving a single module we want to keep track of the list of paths + * we tried to find. This class is a way to aggregate all the tries easily. + */ +class FileNameResolver { + _options: Options; + _tentativeFileNames: Array; + + constructor(options: Options) { + this._options = options; + this._tentativeFileNames = []; + } + + getTentativeFileNames(): $ReadOnlyArray { + return this._tentativeFileNames; + } + + tryToResolveFileName(fileName: string): boolean { + this._tentativeFileNames.push(fileName); + const filePath = path.join(this._options.dirPath, fileName); + return this._options.doesFileExist(filePath); + } +} + +module.exports = FileNameResolver; diff --git a/packager/src/node-haste/DependencyGraph/ResolutionRequest.js b/packager/src/node-haste/DependencyGraph/ResolutionRequest.js index f8cc3c74c..f2795679c 100644 --- a/packager/src/node-haste/DependencyGraph/ResolutionRequest.js +++ b/packager/src/node-haste/DependencyGraph/ResolutionRequest.js @@ -13,6 +13,7 @@ 'use strict'; const AsyncTaskGroup = require('../lib/AsyncTaskGroup'); +const FileNameResolver = require('./FileNameResolver'); const MapWithDefaults = require('../lib/MapWithDefaults'); const debug = require('debug')('RNP:DependencyGraph'); @@ -839,35 +840,6 @@ function resolutionHash(modulePath, depName) { return `${path.resolve(modulePath)}:${depName}`; } -type FileNameResolverOptions = {| - +dirPath: string, - +doesFileExist: (filePath: string) => boolean, -|}; - -/** - * When resolving a single module we want to keep track of the list of paths - * we tried to find. - */ -class FileNameResolver { - _options: FileNameResolverOptions; - _tentativeFileNames: Array; - - constructor(options: FileNameResolverOptions) { - this._options = options; - this._tentativeFileNames = []; - } - - getTentativeFileNames(): $ReadOnlyArray { - return this._tentativeFileNames; - } - - tryToResolveFileName(fileName: string): boolean { - this._tentativeFileNames.push(fileName); - const filePath = path.join(this._options.dirPath, fileName); - return this._options.doesFileExist(filePath); - } -} - class UnableToResolveError extends Error { type: string; from: string;