Skip to content

Commit ebd8b2a

Browse files
Jean Lauliacfacebook-github-bot
Jean Lauliac
authored andcommitted
packager: Package.js: make read()-based API sync
Reviewed By: davidaurelio Differential Revision: D4745885 fbshipit-source-id: 3d327e5ca91fcbe7ec1d30ff8e6135b415074aa4
1 parent d5288e7 commit ebd8b2a

File tree

2 files changed

+73
-76
lines changed

2 files changed

+73
-76
lines changed

packager/src/node-haste/DependencyGraph/ResolutionRequest.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class ResolutionRequest {
232232

233233
let p = fromModule.getPackage();
234234
if (p) {
235-
p = p.redirectRequire(toModuleName);
235+
p = Promise.resolve(p.redirectRequire(toModuleName));
236236
} else {
237237
p = Promise.resolve(toModuleName);
238238
}
@@ -470,13 +470,11 @@ class ResolutionRequest {
470470

471471
const packageJsonPath = path.join(potentialDirPath, 'package.json');
472472
if (this._hasteFS.exists(packageJsonPath)) {
473-
return this._moduleCache.getPackage(packageJsonPath)
474-
.getMain().then(
475-
main => this._tryResolve(
476-
() => this._loadAsFile(main, fromModule, toModule),
477-
() => this._loadAsDir(main, fromModule, toModule)
478-
)
479-
);
473+
const main = this._moduleCache.getPackage(packageJsonPath).getMain();
474+
return this._tryResolve(
475+
() => this._loadAsFile(main, fromModule, toModule),
476+
() => this._loadAsDir(main, fromModule, toModule),
477+
);
480478
}
481479

482480
return this._loadAsFile(

packager/src/node-haste/Package.js

+67-68
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ const path = require('path');
1717

1818
import type Cache from './Cache';
1919

20+
type PackageContent = {
21+
name: string,
22+
'react-native': mixed,
23+
browser: mixed,
24+
main: ?string,
25+
};
26+
2027
class Package {
2128

2229
path: string;
2330
root: string;
2431
type: string;
2532
_cache: Cache;
2633

27-
_reading: Promise<{
28-
name: string,
29-
'react-native': mixed,
30-
browser: mixed,
31-
main: ?string,
32-
}>;
34+
_content: ?PackageContent;
3335

3436
constructor({file, cache}: {
3537
file: string,
@@ -39,106 +41,103 @@ class Package {
3941
this.root = path.dirname(this.path);
4042
this.type = 'Package';
4143
this._cache = cache;
44+
this._content = null;
4245
}
4346

4447
getMain() {
45-
return this.read().then(json => {
46-
var replacements = getReplacements(json);
47-
if (typeof replacements === 'string') {
48-
return path.join(this.root, replacements);
49-
}
48+
const json = this.read();
49+
var replacements = getReplacements(json);
50+
if (typeof replacements === 'string') {
51+
return path.join(this.root, replacements);
52+
}
5053

51-
let main = json.main || 'index';
54+
let main = json.main || 'index';
5255

53-
if (replacements && typeof replacements === 'object') {
54-
main = replacements[main] ||
55-
replacements[main + '.js'] ||
56-
replacements[main + '.json'] ||
57-
replacements[main.replace(/(\.js|\.json)$/, '')] ||
58-
main;
59-
}
56+
if (replacements && typeof replacements === 'object') {
57+
main = replacements[main] ||
58+
replacements[main + '.js'] ||
59+
replacements[main + '.json'] ||
60+
replacements[main.replace(/(\.js|\.json)$/, '')] ||
61+
main;
62+
}
6063

61-
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
62-
return path.join(this.root, main);
63-
});
64+
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
65+
return path.join(this.root, main);
6466
}
6567

6668
isHaste() {
6769
return this._cache.get(this.path, 'package-haste', () =>
68-
this.read().then(json => !!json.name)
70+
Promise.resolve(!!this.read().name)
6971
);
7072
}
7173

7274
getName(): Promise<string> {
7375
return this._cache.get(this.path, 'package-name', () =>
74-
this.read().then(json => json.name)
76+
Promise.resolve(this.read().name)
7577
);
7678
}
7779

7880
invalidate() {
7981
this._cache.invalidate(this.path);
8082
}
8183

82-
redirectRequire(name: string) {
83-
return this.read().then(json => {
84-
var replacements = getReplacements(json);
84+
redirectRequire(name: string): string | false {
85+
const json = this.read();
86+
const replacements = getReplacements(json);
8587

86-
if (!replacements || typeof replacements !== 'object') {
87-
return name;
88-
}
88+
if (!replacements || typeof replacements !== 'object') {
89+
return name;
90+
}
8991

90-
if (!isAbsolutePath(name)) {
91-
const replacement = replacements[name];
92-
// support exclude with "someDependency": false
93-
return replacement === false
94-
? false
95-
: replacement || name;
96-
}
92+
if (!isAbsolutePath(name)) {
93+
const replacement = replacements[name];
94+
// support exclude with "someDependency": false
95+
return replacement === false
96+
? false
97+
/* $FlowFixMe: type of replacements is not being validated */
98+
: replacement || name;
99+
}
97100

98-
let relPath = './' + path.relative(this.root, name);
99-
if (path.sep !== '/') {
100-
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
101-
}
101+
let relPath = './' + path.relative(this.root, name);
102+
if (path.sep !== '/') {
103+
relPath = relPath.replace(new RegExp('\\' + path.sep, 'g'), '/');
104+
}
102105

103-
let redirect = replacements[relPath];
106+
let redirect = replacements[relPath];
104107

105-
// false is a valid value
108+
// false is a valid value
109+
if (redirect == null) {
110+
redirect = replacements[relPath + '.js'];
106111
if (redirect == null) {
107-
redirect = replacements[relPath + '.js'];
108-
if (redirect == null) {
109-
redirect = replacements[relPath + '.json'];
110-
}
112+
redirect = replacements[relPath + '.json'];
111113
}
114+
}
112115

113-
// support exclude with "./someFile": false
114-
if (redirect === false) {
115-
return false;
116-
}
116+
// support exclude with "./someFile": false
117+
if (redirect === false) {
118+
return false;
119+
}
117120

118-
if (redirect) {
119-
return path.join(
120-
this.root,
121-
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
122-
redirect
123-
);
124-
}
121+
if (redirect) {
122+
return path.join(
123+
this.root,
124+
/* $FlowFixMe: `getReplacements` doesn't validate the return value. */
125+
redirect
126+
);
127+
}
125128

126-
return name;
127-
});
129+
return name;
128130
}
129131

130-
read() {
131-
if (!this._reading) {
132-
this._reading = new Promise(
133-
resolve => resolve(JSON.parse(fs.readFileSync(this.path, 'utf8')))
134-
);
132+
read(): PackageContent {
133+
if (this._content == null) {
134+
this._content = JSON.parse(fs.readFileSync(this.path, 'utf8'));
135135
}
136-
137-
return this._reading;
136+
return this._content;
138137
}
139138
}
140139

141-
function getReplacements(pkg) {
140+
function getReplacements(pkg: PackageContent): mixed {
142141
let rn = pkg['react-native'];
143142
let browser = pkg.browser;
144143
if (rn == null) {

0 commit comments

Comments
 (0)