@@ -79,9 +79,12 @@ TsBasicCompiler = class TsBasicCompiler {
79
79
} ;
80
80
}
81
81
82
+ // Reads "files" property of the config.
83
+ // Filter out everything except declaration files
84
+ // in the "typings" folder.
82
85
parsedConfig . typings = [ ] ;
83
86
if ( tsconfig . files ) {
84
- parsedConfig . typings = this . _parseTypings ( tsconfig . files ) ;
87
+ parsedConfig . typings = this . filterTypings ( tsconfig . files ) ;
85
88
}
86
89
87
90
return parsedConfig ;
@@ -106,14 +109,11 @@ TsBasicCompiler = class TsBasicCompiler {
106
109
return result . options ;
107
110
}
108
111
109
- // Parses "files" property of the config
110
- // sifting out everything except declaration files
111
- // in the typings folder.
112
- _parseTypings ( files ) {
113
- check ( files , Array ) ;
112
+ filterTypings ( filePaths ) {
113
+ check ( filePaths , Array ) ;
114
114
115
- return files . filter ( file => {
116
- return this . _typingsRegEx . test ( file ) ;
115
+ return filePaths . filter ( filePath => {
116
+ return this . _typingsRegEx . test ( filePath ) ;
117
117
} ) ;
118
118
}
119
119
@@ -137,45 +137,87 @@ TsBasicCompiler = class TsBasicCompiler {
137
137
let cfgFile = _ . first ( files . filter ( file => this . isConfigFile ( file ) ) ) ;
138
138
if ( cfgFile ) {
139
139
let cfgHash = cfgFile . getSourceHash ( ) ;
140
+ // If config has changed,
141
+ // create and apply new one.
140
142
if ( cfgHash !== this . _cfgHash ) {
141
143
this . _tsconfig = this . _createConfig ( cfgFile . getContentsAsString ( ) ) ;
142
144
this . _cfgHash = cfgHash ;
143
145
}
144
146
}
145
147
}
146
148
149
+ // Gets standardized declaration file path, i.e.,
150
+ // path that contains package name inside.
151
+ // if package "foo" has a declaration file typings/foo.d.ts,
152
+ // then standardized path will be typings/foo/foo.d.ts.
153
+ _getStandardTypingsFilePath ( file ) {
154
+ let filePath = file . getPathInPackage ( ) ;
155
+ let dirPath = ts . getDirectoryPath ( ts . normalizePath ( filePath ) ) ;
156
+ let pkgName = file . getPackageName ( ) ;
157
+ if ( pkgName . indexOf ( ':' ) != - 1 ) {
158
+ pkgName = pkgName . split ( ':' ) [ 1 ] ;
159
+ }
160
+ let pkgTest = new RegExp ( `.*\/${ pkgName } (\/.+|$)` ) ;
161
+ if ( pkgTest . test ( dirPath ) === false ) {
162
+ let pkgDirPath = ts . combinePaths ( dirPath , pkgName ) ;
163
+ let fileName = ts . getBaseFileName ( filePath ) ;
164
+ filePath = ts . combinePaths ( pkgDirPath , fileName ) ;
165
+ }
166
+
167
+ return filePath ;
168
+ }
169
+
170
+ // Copies declaration files from packages to apps.
171
+ // Allows only files from the "typings" folder in packages
172
+ // and copy them to the "typings" folder in apps as well.
147
173
processTypings ( files ) {
148
174
let dtFiles = files . filter ( file => {
149
- return this . isDeclarationFile ( file ) &&
150
- this . isPackageFile ( file ) &&
151
- ! this . _typingsMap . has ( path ) ;
175
+ // Check if it's a package declaration file.
176
+ let isPkgTypings = this . isDeclarationFile ( file ) &&
177
+ this . isPackageFile ( file ) ;
178
+
179
+ if ( isPkgTypings ) {
180
+ let path = file . getPathInPackage ( ) ;
181
+ // Check if the file is in the "typings" folder.
182
+ if ( ! this . _typingsRegEx . test ( path ) ) {
183
+ console . log ( 'Typings path ${path} doesn\'t start with "typings"' ) ;
184
+ return false ;
185
+ }
186
+
187
+ // Check if it's not been processed.
188
+ return ! this . _typingsMap . has ( path ) ;
189
+ }
190
+
191
+ return false ;
152
192
} ) ;
193
+
153
194
let missingFiles = [ ] ;
154
195
for ( let file of dtFiles ) {
155
- let path = file . getPathInPackage ( ) ;
156
- // Resolve typings file relatively the current app folder.
157
- if ( ! fs . existsSync ( path ) ) {
158
- missingFiles . push ( file ) ;
196
+ let filePath = this . _getStandardTypingsFilePath ( file ) ;
197
+
198
+ // Resolve typings file relatively to the current app folder.
199
+ if ( ! fs . existsSync ( filePath ) ) {
200
+ missingFiles . push ( { filePath, file } ) ;
159
201
}
160
202
}
203
+
161
204
if ( missingFiles . length ) {
162
- missingFiles . forEach ( file => {
163
- this . _createTypings ( file ) ;
164
- this . _typingsMap . set ( file . getPathInPackage ( ) ) ;
205
+ missingFiles . forEach ( ( { filePath , file} ) => {
206
+ this . _createTypings ( filePath , file ) ;
207
+ this . _typingsMap . set ( filePath ) ;
165
208
} ) ;
166
209
167
210
// Report about newly installed typings.
168
211
console . log ( chalk . green ( '***** New typings have been added *****' ) ) ;
169
- missingFiles . forEach ( file => {
170
- console . log ( chalk . green ( file . getPathInPackage ( ) ) ) ;
212
+ missingFiles . forEach ( ( { filePath , file} ) => {
213
+ console . log ( chalk . green ( filePath ) ) ;
171
214
} ) ;
172
215
console . log ( chalk . green (
173
216
'Add typings in tsconfig.json or by references in files.' ) ) ;
174
217
}
175
218
}
176
219
177
- _createTypings ( file ) {
178
- let filePath = file . getPathInPackage ( ) ;
220
+ _createTypings ( filePath , file ) {
179
221
let dirPath = ts . getDirectoryPath ( filePath ) ;
180
222
if ( ! fs . existsSync ( dirPath ) ) {
181
223
mkdirp . sync ( Plugin . convertToOSPath ( dirPath ) ) ;
@@ -218,8 +260,7 @@ TsBasicCompiler = class TsBasicCompiler {
218
260
}
219
261
220
262
isDeclarationFile ( file ) {
221
- return TypeScript . isDeclarationFile (
222
- file . getBasename ( ) ) ;
263
+ return TypeScript . isDeclarationFile ( file . getBasename ( ) ) ;
223
264
}
224
265
225
266
isConfigFile ( file ) {
0 commit comments