@@ -17,19 +17,21 @@ const path = require('path');
17
17
18
18
import type Cache from './Cache' ;
19
19
20
+ type PackageContent = {
21
+ name : string ,
22
+ 'react-native' : mixed ,
23
+ browser : mixed ,
24
+ main : ?string ,
25
+ } ;
26
+
20
27
class Package {
21
28
22
29
path : string ;
23
30
root : string ;
24
31
type : string ;
25
32
_cache : Cache ;
26
33
27
- _reading : Promise < {
28
- name : string ,
29
- 'react-native' : mixed ,
30
- browser : mixed ,
31
- main : ?string ,
32
- } > ;
34
+ _content : ?PackageContent ;
33
35
34
36
constructor ( { file, cache} : {
35
37
file : string ,
@@ -39,106 +41,103 @@ class Package {
39
41
this . root = path . dirname ( this . path ) ;
40
42
this . type = 'Package' ;
41
43
this . _cache = cache ;
44
+ this . _content = null ;
42
45
}
43
46
44
47
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
+ }
50
53
51
- let main = json . main || 'index' ;
54
+ let main = json . main || 'index' ;
52
55
53
- if ( replacements && typeof replacements === 'object' ) {
54
- main = replacements [ main ] ||
55
- replacements [ main + '.js' ] ||
56
- replacements [ main + '.json' ] ||
57
- replacements [ main . replace ( / ( \. j s | \. j s o n ) $ / , '' ) ] ||
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 ( / ( \. j s | \. j s o n ) $ / , '' ) ] ||
61
+ main ;
62
+ }
60
63
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 ) ;
64
66
}
65
67
66
68
isHaste ( ) {
67
69
return this . _cache . get ( this . path , 'package-haste' , ( ) =>
68
- this . read ( ) . then ( json => ! ! json . name )
70
+ Promise . resolve ( ! ! this . read ( ) . name )
69
71
) ;
70
72
}
71
73
72
74
getName ( ) : Promise < string > {
73
75
return this . _cache . get ( this . path , 'package-name' , ( ) =>
74
- this . read ( ) . then ( json => json . name )
76
+ Promise . resolve ( this . read ( ) . name )
75
77
) ;
76
78
}
77
79
78
80
invalidate ( ) {
79
81
this . _cache . invalidate ( this . path ) ;
80
82
}
81
83
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 ) ;
85
87
86
- if ( ! replacements || typeof replacements !== 'object' ) {
87
- return name ;
88
- }
88
+ if ( ! replacements || typeof replacements !== 'object' ) {
89
+ return name ;
90
+ }
89
91
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
+ }
97
100
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
+ }
102
105
103
- let redirect = replacements [ relPath ] ;
106
+ let redirect = replacements [ relPath ] ;
104
107
105
- // false is a valid value
108
+ // false is a valid value
109
+ if ( redirect == null ) {
110
+ redirect = replacements [ relPath + '.js' ] ;
106
111
if ( redirect == null ) {
107
- redirect = replacements [ relPath + '.js' ] ;
108
- if ( redirect == null ) {
109
- redirect = replacements [ relPath + '.json' ] ;
110
- }
112
+ redirect = replacements [ relPath + '.json' ] ;
111
113
}
114
+ }
112
115
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
+ }
117
120
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
+ }
125
128
126
- return name ;
127
- } ) ;
129
+ return name ;
128
130
}
129
131
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' ) ) ;
135
135
}
136
-
137
- return this . _reading ;
136
+ return this . _content ;
138
137
}
139
138
}
140
139
141
- function getReplacements ( pkg ) {
140
+ function getReplacements ( pkg : PackageContent ) : mixed {
142
141
let rn = pkg [ 'react-native' ] ;
143
142
let browser = pkg . browser ;
144
143
if ( rn == null ) {
0 commit comments