Adapt interface of node-haste duplicates

Summary: Adapts mocked / duplicated functionality from `node-haste` to match the new synchronous return types in the original.

Reviewed By: jeanlauliac

Differential Revision: D4819137

fbshipit-source-id: 183316adc3fae161ad9999bf72bccb8218ef8941
This commit is contained in:
David Aurelio 2017-04-03 09:55:28 -07:00 committed by Facebook Github Bot
parent 015700ea7c
commit 6bdc5e9f21
5 changed files with 82 additions and 78 deletions

View File

@ -24,9 +24,9 @@ module.exports = class Module {
constructor(
path: string,
moduleCache: ModuleCache,
info: Promise<TransformedFile>,
info: TransformedFile,
) {
this.hasteID = info.then(({hasteID}) => hasteID);
this.hasteID = Promise.resolve(info.hasteID);
this.moduleCache = moduleCache;
this.name = this.hasteID.then(name => name || getName(path));
this.path = path;

View File

@ -16,22 +16,20 @@ const Package = require('./Package');
import type {PackageData, TransformedFile} from '../types.flow';
type GetFn<T> = (path: string) => Promise<T>;
type GetClosestPackageFn = (filePath: string) => ?string;
module.exports = class ModuleCache {
_getClosestPackage: GetClosestPackageFn;
getPackageData: GetFn<PackageData>;
getTransformedFile: GetFn<TransformedFile>;
getTransformedFile: string => TransformedFile;
modules: Map<string, Module>;
packages: Map<string, Package>;
constructor(getClosestPackage: GetClosestPackageFn, getTransformedFile: GetFn<TransformedFile>) {
constructor(
getClosestPackage: GetClosestPackageFn,
getTransformedFile: string => TransformedFile,
) {
this._getClosestPackage = getClosestPackage;
this.getTransformedFile = getTransformedFile;
this.getPackageData = path => getTransformedFile(path).then(
f => f.package || Promise.reject(new Error(`"${path}" does not exist`))
);
this.modules = new Map();
this.packages = new Map();
}
@ -58,6 +56,14 @@ module.exports = class ModuleCache {
return p;
}
getPackageData(path: string): PackageData {
const pkg = this.getTransformedFile(path).package;
if (!pkg) {
throw new Error(`"${path}" does not exist`);
}
return pkg;
}
getPackageOf(filePath: string) {
const candidate = this._getClosestPackage(filePath);
return candidate != null ? this.getPackage(candidate) : null;

View File

@ -17,12 +17,12 @@ const path = require('path');
import type {PackageData} from '../types.flow';
module.exports = class Package {
data: Promise<PackageData>;
data: PackageData;
path: string;
root: string;
type: 'Package';
constructor(packagePath: string, data: Promise<PackageData>) {
constructor(packagePath: string, data: PackageData) {
this.data = data;
this.path = packagePath;
this.root = path.dirname(packagePath);
@ -31,80 +31,76 @@ module.exports = class Package {
getMain() {
// Copied from node-haste/Package.js
return this.data.then(data => {
const replacements = getReplacements(data);
if (typeof replacements === 'string') {
return path.join(this.root, replacements);
}
const replacements = getReplacements(this.data);
if (typeof replacements === 'string') {
return path.join(this.root, replacements);
}
let main = getMain(data);
let main = getMain(this.data);
if (replacements && typeof replacements === 'object') {
main = replacements[main] ||
replacements[main + '.js'] ||
replacements[main + '.json'] ||
replacements[main.replace(/(\.js|\.json)$/, '')] ||
main;
}
if (replacements && typeof replacements === 'object') {
main = replacements[main] ||
replacements[main + '.js'] ||
replacements[main + '.json'] ||
replacements[main.replace(/(\.js|\.json)$/, '')] ||
main;
}
return path.join(this.root, main);
});
return path.join(this.root, main);
}
getName() {
return this.data.then(p => nullthrows(p.name));
return Promise.resolve(nullthrows(this.data.name));
}
isHaste() {
return this.data.then(p => !!p.name);
return Promise.resolve(!!this.data.name);
}
redirectRequire(name: string) {
// Copied from node-haste/Package.js
return this.data.then(data => {
const replacements = getReplacements(data);
if (!replacements || typeof replacements !== 'object') {
return name;
}
if (!path.isAbsolute(name)) {
const replacement = replacements[name];
// support exclude with "someDependency": false
return replacement === false
? false
: replacement || name;
}
let relPath = './' + path.relative(this.root, name);
if (path.sep !== '/') {
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
}
let redirect = replacements[relPath];
// false is a valid value
if (redirect == null) {
redirect = replacements[relPath + '.js'];
if (redirect == null) {
redirect = replacements[relPath + '.json'];
}
}
// support exclude with "./someFile": false
if (redirect === false) {
return false;
}
if (redirect) {
return path.join(
this.root,
redirect
);
}
const replacements = getReplacements(this.data);
if (!replacements || typeof replacements !== 'object') {
return name;
});
}
if (!path.isAbsolute(name)) {
const replacement = replacements[name];
// support exclude with "someDependency": false
return replacement === false
? false
: replacement || name;
}
let relPath = './' + path.relative(this.root, name);
if (path.sep !== '/') {
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
}
let redirect = replacements[relPath];
// false is a valid value
if (redirect == null) {
redirect = replacements[relPath + '.js'];
if (redirect == null) {
redirect = replacements[relPath + '.json'];
}
}
// support exclude with "./someFile": false
if (redirect === false) {
return false;
}
if (redirect) {
return path.join(
this.root,
redirect
);
}
return name;
}
};

View File

@ -33,14 +33,13 @@ export type Package = {
path: Path,
root: Path,
type: 'Package',
getMain(): Promise<Path>,
getMain(): Path,
getName(): Promise<ModuleID>,
isHaste(): Promise<boolean>,
redirectRequire(id: ModuleID): Promise<Path | false>,
redirectRequire(id: ModuleID): Path | false,
};
// when changing this to `type`, the code does not typecheck any more
export interface ModuleCache {
export type ModuleCache = {
getAssetModule(path: Path): Module,
getModule(path: Path): Module,
getPackage(path: Path): Package,

View File

@ -63,10 +63,13 @@ exports.createResolveFn = function(options: ResolveOptions): ResolveFn {
transformedFiles,
} = options;
const files = Object.keys(transformedFiles);
const getTransformedFile =
path => Promise.resolve(
transformedFiles[path] || Promise.reject(new Error(`"${path} does not exist`))
);
function getTransformedFile(path) {
const result = transformedFiles[path];
if (!result) {
throw new Error(`"${path} does not exist`);
}
return result;
}
const helpers = new DependencyGraphHelpers({
assetExts,