11import extend = require( 'xtend' )
22import Promise = require( 'any-promise' )
3- import { dirname } from 'path'
3+ import { dirname , join } from 'path'
44import { EventEmitter } from 'events'
55import { resolveDependency , resolveTypeDependencies } from './lib/dependencies'
66import compile , { Options as CompileOptions , CompiledOutput } from './lib/compile'
7- import { findProject } from './utils/find'
8- import { transformConfig , mkdirp , touch , writeFile , transformDtsFile } from './utils/fs'
9- import { getTypingsLocation , getDependencyLocation } from './utils/path'
7+ import { findProject , findUp } from './utils/find'
8+ import { transformConfig , mkdirp , touch , writeFile , transformDtsFile , readJson } from './utils/fs'
9+ import { getTypingsLocation , getDependencyLocation , resolveFrom } from './utils/path'
1010import { parseDependency , parseDependencyExpression } from './utils/parse'
1111import { DependencyTree , Dependency , DependencyBranch , Emitter } from './interfaces'
1212
@@ -108,7 +108,8 @@ function installTo (expression: InstallExpression, options: InstallDependencyOpt
108108 const { cwd, ambient } = options
109109 const emitter = options . emitter || new EventEmitter ( )
110110
111- return resolveDependency ( dependency , { cwd, emitter, dev : false , peer : false , ambient : false } )
111+ return checkTypings ( dependency , options )
112+ . then ( ( ) => resolveDependency ( dependency , { cwd, emitter, dev : false , peer : false , ambient : false } ) )
112113 . then ( tree => {
113114 const name = expression . name || dependency . meta . name || tree . name
114115
@@ -173,7 +174,7 @@ function writeToConfig (name: string, raw: string, options: InstallDependencyOpt
173174/**
174175 * Write a dependency to the filesytem.
175176 */
176- export function writeDependency ( output : CompiledOutput , options : CompileOptions ) : Promise < CompiledOutput > {
177+ function writeDependency ( output : CompiledOutput , options : CompileOptions ) : Promise < CompiledOutput > {
177178 const location = getDependencyLocation ( options )
178179
179180 // Execute the dependency creation flow.
@@ -189,3 +190,31 @@ export function writeDependency (output: CompiledOutput, options: CompileOptions
189190 create ( location . browserPath , location . browserFile , output . browser , location . browserDtsFile )
190191 ] ) . then ( ( ) => output )
191192}
193+
194+ /**
195+ * Find existing `typings` that TypeScript supports.
196+ */
197+ function checkTypings ( dependency : Dependency , options : InstallDependencyOptions ) {
198+ const { type, meta } = dependency
199+
200+ // TypeScript only support NPM, as of today.
201+ if ( type === 'registry' && meta . source === 'npm' ) {
202+ return findUp ( options . cwd , join ( 'node_modules' , meta . name , 'package.json' ) )
203+ . then ( path => {
204+ return readJson ( path )
205+ . then ( packageJson => {
206+ if ( packageJson && typeof packageJson . typings === 'string' ) {
207+ options . emitter . emit ( 'hastypings' , {
208+ name : meta . name ,
209+ source : meta . source ,
210+ path : path ,
211+ typings : resolveFrom ( path , packageJson . typings )
212+ } )
213+ }
214+ } )
215+ } )
216+ . catch ( err => undefined )
217+ }
218+
219+ return Promise . resolve ( )
220+ }
0 commit comments