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
This commit is contained in:
Jean Lauliac 2017-05-31 10:37:49 -07:00 committed by Facebook Github Bot
parent 3fecc28912
commit 3aedbbbf96
2 changed files with 47 additions and 29 deletions

View File

@ -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<string>;
constructor(options: Options) {
this._options = options;
this._tentativeFileNames = [];
}
getTentativeFileNames(): $ReadOnlyArray<string> {
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;

View File

@ -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<string>;
constructor(options: FileNameResolverOptions) {
this._options = options;
this._tentativeFileNames = [];
}
getTentativeFileNames(): $ReadOnlyArray<string> {
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;