Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 2ba8f8e

Browse files
committed
Check for packaged typings during install step
1 parent 718462c commit 2ba8f8e

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

src/install.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import extend = require('xtend')
22
import Promise = require('any-promise')
3-
import { dirname } from 'path'
3+
import { dirname, join } from 'path'
44
import { EventEmitter } from 'events'
55
import { resolveDependency, resolveTypeDependencies } from './lib/dependencies'
66
import 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'
1010
import { parseDependency, parseDependencyExpression } from './utils/parse'
1111
import { 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+
}

src/interfaces.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export interface Emitter extends EventEmitter {
113113
on (event: 'enoent', listener: (e: EnoentEvent) => any): this
114114
on (event: 'compile', listener: (e: CompileEvent) => any): this
115115
on (event: 'compiled', listener: (e: CompiledEvent) => any): this
116+
on (event: 'hastypings', listener: (e: HasTypingsEvent) => any): this
116117
on (event: string, listener: Function): this
117118

118119
emit (event: 'reference', e: ReferenceEvent): boolean
@@ -121,6 +122,7 @@ export interface Emitter extends EventEmitter {
121122
emit (event: 'enoent', e: EnoentEvent): boolean
122123
emit (event: 'compile', e: CompileEvent): boolean
123124
emit (event: 'compiled', e: CompiledEvent): boolean
125+
emit (event: 'hastypings', e: HasTypingsEvent): boolean
124126
emit (event: string, ...args: any[]): boolean
125127
}
126128

@@ -173,4 +175,14 @@ export interface CompileEvent {
173175
*/
174176
export interface CompiledEvent extends CompileEvent {
175177
contents: string
178+
}
179+
180+
/**
181+
* Emit a "hastypings" event when native typings exist during install.
182+
*/
183+
export interface HasTypingsEvent {
184+
source: string
185+
name: string
186+
path: string
187+
typings: string
176188
}

0 commit comments

Comments
 (0)