From 6ba700f5b534520f43bca03ce364bed34aa7def9 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Sat, 5 Mar 2016 18:16:44 -0800 Subject: [PATCH] Add an `emitter` for sending messages to clients --- src/bundle.spec.ts | 6 +- src/bundle.ts | 8 +- src/init.ts | 2 +- src/install.spec.ts | 19 ++-- src/install.ts | 18 +++- src/{interfaces/main.ts => interfaces.ts} | 43 ++++++++ src/lib/compile.spec.ts | 53 +++++++--- src/lib/compile.ts | 115 +++------------------- src/lib/dependencies.spec.ts | 7 +- src/lib/dependencies.ts | 37 +++++-- src/typings.ts | 1 + src/utils/fs.ts | 2 +- src/utils/parse.ts | 2 +- 13 files changed, 171 insertions(+), 142 deletions(-) rename src/{interfaces/main.ts => interfaces.ts} (63%) diff --git a/src/bundle.spec.ts b/src/bundle.spec.ts index 0f422b9..694011d 100644 --- a/src/bundle.spec.ts +++ b/src/bundle.spec.ts @@ -5,6 +5,9 @@ import { join } from 'path' import { bundle } from './bundle' import { VERSION } from './typings' import { rimraf } from './utils/fs' +import { EventEmitter } from 'events' + +const emitter = new EventEmitter() test('bundle', t => { t.test('bundle everything', t => { @@ -16,7 +19,8 @@ test('bundle', t => { cwd: FIXTURE_DIR, name: 'example', out: join(FIXTURE_DIR, 'out'), - ambient: false + ambient: false, + emitter }) }) .then(function (data) { diff --git a/src/bundle.ts b/src/bundle.ts index 44c8732..73158b9 100644 --- a/src/bundle.ts +++ b/src/bundle.ts @@ -1,8 +1,10 @@ import Promise = require('any-promise') import { resolve, join } from 'path' +import { EventEmitter } from 'events' import { resolveAllDependencies } from './lib/dependencies' import compile, { CompiledOutput } from './lib/compile' import { writeFile, mkdirp } from './utils/fs' +import { Emitter } from './interfaces' /** * Bundle configuration options. @@ -12,6 +14,7 @@ export interface BundleOptions { cwd: string ambient: boolean out: string + emitter?: Emitter } /** @@ -19,12 +22,13 @@ export interface BundleOptions { */ export function bundle (options: BundleOptions): Promise { const { cwd, ambient, out } = options + const emitter = options.emitter || new EventEmitter() if (out == null) { return Promise.reject(new TypeError('Out directory is required for bundle')) } - return resolveAllDependencies({ cwd, dev: false, ambient }) + return resolveAllDependencies({ cwd, dev: false, ambient, emitter }) .then(tree => { const name = options.name || tree.name @@ -34,7 +38,7 @@ export function bundle (options: BundleOptions): Promise { )) } - return compile(tree, { cwd, name, ambient, meta: true }) + return compile(tree, { cwd, name, ambient, emitter, meta: true }) }) .then((output: CompiledOutput) => { const path = resolve(cwd, out) diff --git a/src/init.ts b/src/init.ts index a0cab2e..cb6918a 100644 --- a/src/init.ts +++ b/src/init.ts @@ -1,7 +1,7 @@ import Promise = require('any-promise') import extend = require('xtend') import { join } from 'path' -import { ConfigJson } from './interfaces/main' +import { ConfigJson } from './interfaces' import { writeJson, isFile, readJson } from './utils/fs' import { CONFIG_FILE } from './utils/config' import { inferDefinitionName } from './utils/path' diff --git a/src/install.spec.ts b/src/install.spec.ts index b5de5ab..a143c37 100644 --- a/src/install.spec.ts +++ b/src/install.spec.ts @@ -2,12 +2,15 @@ import test = require('blue-tape') import Promise = require('any-promise') import { EOL } from 'os' import { join, relative } from 'path' +import { EventEmitter } from 'events' import { install, installDependency } from './install' import { readFile, readConfig, writeFile, rimraf } from './utils/fs' import { CONFIG_FILE } from './utils/config' import { VERSION } from './typings' test('install', t => { + const emitter = new EventEmitter() + t.test('install everything', t => { const FIXTURE_DIR = join(__dirname, '__test__/install-fixture') @@ -15,7 +18,7 @@ test('install', t => { .then(() => { return install({ cwd: FIXTURE_DIR, - production: false + emitter }) }) .then(function () { @@ -74,17 +77,20 @@ test('install', t => { installDependency(DEPENDENCY, { cwd: FIXTURE_DIR, saveDev: true, - name: '@scope/test' + name: '@scope/test', + emitter }), installDependency(AMBIENT_DEPENDENCY, { cwd: FIXTURE_DIR, saveDev: true, ambient: true, - name: 'ambient-test' + name: 'ambient-test', + emitter }), installDependency(PEER_DEPENDENCY, { cwd: FIXTURE_DIR, - savePeer: true + savePeer: true, + emitter }) ]) }) @@ -112,7 +118,7 @@ test('install', t => { t.plan(1) - return installDependency(DEPENDENCY, { cwd: FIXTURE_DIR }) + return installDependency(DEPENDENCY, { cwd: FIXTURE_DIR, emitter }) .catch(err => { t.ok(/^Unable to install dependency/.test(err.message)) }) @@ -123,7 +129,8 @@ test('install', t => { return install({ cwd: FIXTURE_DIR, - production: false + dev: true, + emitter }) .then(function () { return Promise.all([ diff --git a/src/install.ts b/src/install.ts index cae0728..6410dc8 100644 --- a/src/install.ts +++ b/src/install.ts @@ -1,13 +1,14 @@ import extend = require('xtend') import Promise = require('any-promise') import { dirname } from 'path' +import { EventEmitter } from 'events' import { resolveDependency, resolveTypeDependencies } from './lib/dependencies' import compile, { Options as CompileOptions, CompiledOutput } from './lib/compile' import { findProject } from './utils/find' import { transformConfig, mkdirp, touch, writeFile, transformDtsFile } from './utils/fs' import { getTypingsLocation, getDependencyLocation } from './utils/path' import { parseDependency } from './utils/parse' -import { DependencyTree, Dependency, DependencyBranch } from './interfaces/main' +import { DependencyTree, Dependency, DependencyBranch, Emitter } from './interfaces' /** * Options for installing a new dependency. @@ -19,6 +20,7 @@ export interface InstallDependencyOptions { ambient?: boolean name?: string cwd: string + emitter?: Emitter source?: string } @@ -27,14 +29,18 @@ export interface InstallDependencyOptions { */ export interface InstallOptions { cwd: string - production: boolean + dev?: boolean + emitter?: Emitter } /** * Install all dependencies on the current project. */ export function install (options: InstallOptions): Promise<{ tree: DependencyTree }> { - return resolveTypeDependencies({ cwd: options.cwd, dev: !options.production, ambient: true, peer: true }) + const { cwd, dev } = options + const emitter = options.emitter || new EventEmitter() + + return resolveTypeDependencies({ cwd, emitter, ambient: true, peer: true, dev: options.dev !== false }) .then(tree => { const cwd = dirname(tree.src) const queue: Array> = [] @@ -43,7 +49,7 @@ export function install (options: InstallOptions): Promise<{ tree: DependencyTre for (const name of Object.keys(deps)) { const tree = deps[name] - queue.push(installDependencyTree(tree, { cwd, name, ambient, meta: true })) + queue.push(installDependencyTree(tree, { cwd, name, ambient, emitter, meta: true })) } } @@ -88,8 +94,9 @@ export function installDependency (dependency: string, options: InstallDependenc function installTo (location: string, options: InstallDependencyOptions): Promise { const dependency = parseDependency(location) const { cwd, ambient } = options + const emitter = options.emitter || new EventEmitter() - return resolveDependency(dependency, { cwd, dev: false, peer: false, ambient: false }) + return resolveDependency(dependency, { cwd, emitter, dev: false, peer: false, ambient: false }) .then(tree => { const name = options.name || tree.name @@ -101,6 +108,7 @@ function installTo (location: string, options: InstallDependencyOptions): Promis cwd, name, ambient, + emitter, meta: true }) .then(result => { diff --git a/src/interfaces/main.ts b/src/interfaces.ts similarity index 63% rename from src/interfaces/main.ts rename to src/interfaces.ts index ad628bc..a712c9e 100644 --- a/src/interfaces/main.ts +++ b/src/interfaces.ts @@ -1,3 +1,5 @@ +import { EventEmitter } from 'events' + /** * A dependency string is a string that maps to a resource. For example, * "file:foo/bar" or "npm:typescript". @@ -85,3 +87,44 @@ export interface DependencyTree { export interface DependencyBranch { [name: string]: DependencyTree } + +/** + * Custom event emitter for tracking Typings changes. + */ +export interface Emitter extends EventEmitter { + on (event: 'reference', listener: (e: ReferenceEvent) => any): this + on (event: 'resolve', listener: (e: ResolveEvent) => any): this + on (event: 'resolved', listener: (e: ResolvedEvent) => any): this + on (event: string, listener: Function): this + + emit (event: 'reference', e: ReferenceEvent): boolean + emit (event: 'resolve', e: ResolveEvent): boolean + emit (event: 'resolved', e: ResolvedEvent): boolean + emit (event: string, ...args: any[]): boolean +} + +/** + * Emit stripped references. + */ +export interface ReferenceEvent { + name: string + path: string + raw: string + src: string +} + +/** + * Emit when resolving a dependency. + */ +export interface ResolveEvent { + src: string + raw: string + parent?: DependencyTree +} + +/** + * Emit when the dependency is resolved. + */ +export interface ResolvedEvent extends ResolveEvent { + tree: DependencyTree +} \ No newline at end of file diff --git a/src/lib/compile.spec.ts b/src/lib/compile.spec.ts index f3dacae..bfe5c87 100644 --- a/src/lib/compile.spec.ts +++ b/src/lib/compile.spec.ts @@ -2,8 +2,9 @@ import test = require('blue-tape') import nock = require('nock') import { EOL } from 'os' import { join, relative } from 'path' +import { EventEmitter } from 'events' import compile from './compile' -import { DependencyTree } from '../interfaces/main' +import { DependencyTree } from '../interfaces' import { CONFIG_FILE } from '../utils/config' import { VERSION } from '../typings' import { resolveTypeDependencies, resolveNpmDependencies } from './dependencies' @@ -82,7 +83,9 @@ test('compile', t => { ;(root as any).dependencies.browser = browser ;(a as any).dependencies.dep = dep - return compile(root, { name: 'root', cwd: __dirname, ambient: false, meta: true }) + const emitter = new EventEmitter() + + return compile(root, { name: 'root', cwd: __dirname, ambient: false, meta: true, emitter }) .then((result) => { t.equal(result.main, [ `// Generated by typings`, @@ -179,7 +182,9 @@ test('compile', t => { ambientDevDependencies: {} } - return compile(file, { name: 'foobar', cwd: __dirname, ambient: false, meta: false }) + const emitter = new EventEmitter() + + return compile(file, { name: 'foobar', cwd: __dirname, ambient: false, meta: false, emitter }) .then(results => { t.equal(results.main, [ 'declare module \'foobar/file\' {', @@ -217,7 +222,9 @@ test('compile', t => { ambientDevDependencies: {} } - return compile(file, { name: 'test', cwd: __dirname, ambient: false, meta: false }) + const emitter = new EventEmitter() + + return compile(file, { name: 'test', cwd: __dirname, ambient: false, meta: false, emitter }) .then(results => { t.equal(results.main, [ 'declare module \'test/index\' {', @@ -246,7 +253,9 @@ test('compile', t => { ambientDevDependencies: {} } - return compile(file, { name: 'test', cwd: __dirname, ambient: false, meta: false }) + const emitter = new EventEmitter() + + return compile(file, { name: 'test', cwd: __dirname, ambient: false, meta: false, emitter }) .then(results => { t.equal(results.main, [ 'declare module \'test/import\' {', @@ -301,7 +310,9 @@ test('compile', t => { ;(node as any).dependencies.fs = fs - return compile(node, { name: 'name', cwd: __dirname, ambient: true, meta: false }) + const emitter = new EventEmitter() + + return compile(node, { name: 'name', cwd: __dirname, ambient: true, meta: false, emitter }) .then(result => { t.equal(result.main, [ 'declare module \'fs\' {', @@ -329,7 +340,9 @@ test('compile', t => { ambientDevDependencies: {} } - return compile(node, { name: 'name', cwd: __dirname, ambient: true, meta: true }) + const emitter = new EventEmitter() + + return compile(node, { name: 'name', cwd: __dirname, ambient: true, meta: true, emitter }) .then(result => { const contents = [ `// Generated by typings`, @@ -363,9 +376,11 @@ test('compile', t => { ambientDevDependencies: {} } + const emitter = new EventEmitter() + t.plan(1) - return compile(node, { name: 'test', cwd: __dirname, ambient: false, meta: false }) + return compile(node, { name: 'test', cwd: __dirname, ambient: false, meta: false, emitter }) .catch(function (result) { t.equal(result.message, 'Unable to read typings for "test". You should check the path is correct') }) @@ -384,9 +399,11 @@ test('compile', t => { ambientDevDependencies: {} } + const emitter = new EventEmitter() + t.plan(1) - return compile(main, { name: 'main', cwd: __dirname, ambient: false, meta: false }) + return compile(main, { name: 'main', cwd: __dirname, ambient: false, meta: false, emitter }) .catch(function (error) { t.ok(/^Unable to resolve entry "\.d\.ts" file for "main"/.test(error.message)) }) @@ -417,11 +434,13 @@ test('compile', t => { ambientDevDependencies: {} } + const emitter = new EventEmitter() + ;(main as any).dependencies.test = dependency t.plan(1) - return compile(main, { name: 'main', cwd: __dirname, ambient: false, meta: false }) + return compile(main, { name: 'main', cwd: __dirname, ambient: false, meta: false, emitter }) .catch(function (error) { t.ok(/^Unable to read typings for "test"/.test(error.message)) }) @@ -429,9 +448,10 @@ test('compile', t => { t.test('override dependency with local file', t => { const FIXTURE_DIR = join(FIXTURES_DIR, 'compile-module-file-override') + const emitter = new EventEmitter() - return resolveNpmDependencies({ cwd: FIXTURE_DIR, dev: false }) - .then(x => compile(x, { name: 'main', cwd: __dirname, ambient: false, meta: false })) + return resolveNpmDependencies({ cwd: FIXTURE_DIR, dev: false, emitter }) + .then(x => compile(x, { name: 'main', cwd: __dirname, ambient: false, meta: false, emitter })) .then(result => { t.equal(result.browser, [ 'declare module \'main/override\' {', @@ -455,9 +475,10 @@ test('compile', t => { t.test('resolve and compile local file override with dependency', t => { const FIXTURE_DIR = join(FIXTURES_DIR, 'compile-file-module-override') + const emitter = new EventEmitter() - return resolveNpmDependencies({ cwd: FIXTURE_DIR, dev: false }) - .then(x => compile(x, { name: 'main', cwd: __dirname, ambient: false, meta: false })) + return resolveNpmDependencies({ cwd: FIXTURE_DIR, dev: false, emitter }) + .then(x => compile(x, { name: 'main', cwd: __dirname, ambient: false, meta: false, emitter })) .then(result => { t.equal(result.main, [ 'declare module \'main/imported\' {', @@ -502,12 +523,14 @@ test('compile', t => { ambientDevDependencies: {} } + const emitter = new EventEmitter() + nock('http://example.com') .get('/index.d.ts') .matchHeader('User-Agent', /^typings\/\d+\.\d+\.\d+ node\/v\d+\.\d+\.\d+.*$/) .reply(200, 'export const helloWorld: string') - return compile(node, { name: 'test', cwd: __dirname, ambient: false, meta: false }) + return compile(node, { name: 'test', cwd: __dirname, ambient: false, meta: false, emitter }) .then(function (result) { t.equal(result.main, "declare module 'test' {\nexport const helloWorld: string\n}") }) diff --git a/src/lib/compile.ts b/src/lib/compile.ts index 66d2822..a80dabc 100644 --- a/src/lib/compile.ts +++ b/src/lib/compile.ts @@ -4,7 +4,7 @@ import has = require('has') import Promise = require('any-promise') import { EOL } from 'os' import { join, relative } from 'path' -import { DependencyTree, Overrides } from '../interfaces/main' +import { DependencyTree, Overrides, Emitter } from '../interfaces' import { readFileFrom } from '../utils/fs' import { resolveFrom, relativeTo, isHttp, isModuleName, normalizeSlashes, fromDefinition, normalizeToDefinition, toDefinition } from '../utils/path' import { REFERENCE_REGEXP } from '../utils/references' @@ -21,31 +21,7 @@ export interface Options { name: string ambient: boolean meta: boolean -} - -/** - * References collected by compilation. - */ -interface Reference { - path: string - raw: string - src: string - name: string -} - -/** - * The result of compiling a dependency tree. - */ -interface CompiledResult { - contents: string - references: Reference[] -} - -/** - * Compiled reference map with usages. - */ -export interface ReferenceMap { - [path: string]: Array<{ name: string; main: boolean; browser: boolean }> + emitter: Emitter } /** @@ -55,7 +31,6 @@ export interface CompiledOutput { tree: DependencyTree main: string browser: string - references: ReferenceMap } /** @@ -71,56 +46,12 @@ export default function compile (tree: DependencyTree, options: Options): Promis .then(([main, browser]) => { return { tree, - main: main.contents, - browser: browser.contents, - references: mergeReferences(main.references, browser.references) + main, + browser } }) } -/** - * Create a reference map with the original sources. - */ -function mergeReferences (main: Reference[], browser: Reference[]): ReferenceMap { - const map: ReferenceMap = {} - - // Add each entry to the map, deduping as we go. - function addEntry (entry: Reference, browser: boolean) { - const { path, raw, src, name } = entry - const location = resolveDependency(raw, relativeTo(src, path)) - const values = map[location] || (map[location] = []) - - for (const value of values) { - if (value.name === name) { - // Set "browser" or "main" flags. - if (browser) { - value.browser = true - } else { - value.main = true - } - - return - } - } - - values.push({ - name, - main: !browser, - browser - }) - } - - for (const entry of main) { - addEntry(entry, false) - } - - for (const entry of browser) { - addEntry(entry, true) - } - - return map -} - /** * Extends the default options with different compilation settings. */ @@ -218,14 +149,14 @@ function getStringifyOptions ( /** * Compile a dependency tree to a single definition. */ -function compileDependencyTree (tree: DependencyTree, options: CompileOptions): Promise { +function compileDependencyTree (tree: DependencyTree, options: CompileOptions): Promise { return compileDependencyPath(null, getStringifyOptions(tree, options, undefined)) } /** * Compile a dependency for a path, with pre-created stringify options. */ -function compileDependencyPath (path: string, options: StringifyOptions): Promise { +function compileDependencyPath (path: string, options: StringifyOptions): Promise { const { tree, entry } = options // Fallback to resolving the entry file. @@ -315,23 +246,20 @@ function getDependency (name: string, options: StringifyOptions): DependencyTree /** * Stringify a dependency file. */ -function stringifyDependencyPath (path: string, options: StringifyOptions): Promise { +function stringifyDependencyPath (path: string, options: StringifyOptions): Promise { const resolved = getPath(path, options) - const { tree, ambient, cwd, browser, name, files, meta, entry } = options + const { tree, ambient, cwd, browser, name, files, meta, entry, emitter } = options const { raw, src } = tree // Load a dependency path. function loadByModuleName (path: string) { const [moduleName, modulePath] = getModuleNameParts(path) - const compileOptions = { cwd, browser, files, name: moduleName, ambient: false, meta } + const compileOptions = { cwd, browser, files, emitter, name: moduleName, ambient: false, meta } const stringifyOptions = cachedStringifyOptions(moduleName, compileOptions, options) // When no options are returned, the dependency is missing. if (!stringifyOptions) { - return Promise.resolve({ - contents: null, - references: [] - }) + return Promise.resolve(null) } return compileDependencyPath(modulePath, stringifyOptions) @@ -394,31 +322,20 @@ function stringifyDependencyPath (path: string, options: StringifyOptions): Prom }) return Promise.all(imports) - .then(imports => { + .then(imports => { const stringified = stringifyFile(resolved, rawContents, path, options) - let references = referencedFiles.map(path => ({ name, path, raw, src })) - let contents: string[] = [] - - for (const imported of imports) { - // Some dependencies and imports are skipped. - if (imported) { - references = references.concat(imported.references) - - if (imported.contents != null) { - contents.push(imported.contents) - } - } + for (const path of referencedFiles) { + emitter.emit('reference', { name, path, raw, src }) } + const contents = imports.filter(x => x != null) + // Push the current file at the end of the contents. // This builds the stringified file with dependencies first. contents.push(stringified) - return { - contents: contents.join(EOL + EOL), - references - } + return contents.join(EOL + EOL) }) }, function (cause) { diff --git a/src/lib/dependencies.spec.ts b/src/lib/dependencies.spec.ts index 9e216df..75ee64b 100644 --- a/src/lib/dependencies.spec.ts +++ b/src/lib/dependencies.spec.ts @@ -1,9 +1,11 @@ import test = require('blue-tape') import { join } from 'path' +import { EventEmitter } from 'events' import { resolveAllDependencies } from './dependencies' -import { DependencyTree, DependencyBranch } from '../interfaces/main' +import { DependencyTree, DependencyBranch } from '../interfaces' const RESOLVE_FIXTURE_DIR = join(__dirname, '__test__/fixtures/resolve') +const emitter = new EventEmitter() test('dependencies', t => { t.test('resolve fixture', t => { @@ -112,7 +114,8 @@ test('dependencies', t => { return resolveAllDependencies({ cwd: RESOLVE_FIXTURE_DIR, - dev: true + dev: true, + emitter }) .then((result) => { function removeParentReferenceFromDependencies (dependencies: DependencyBranch) { diff --git a/src/lib/dependencies.ts b/src/lib/dependencies.ts index 8a12956..40341c9 100644 --- a/src/lib/dependencies.ts +++ b/src/lib/dependencies.ts @@ -9,7 +9,7 @@ import { parseDependency } from '../utils/parse' import { findUp, findConfigFile } from '../utils/find' import { isDefinition, isHttp } from '../utils/path' import { CONFIG_FILE, PROJECT_NAME } from '../utils/config' -import { Dependency, DependencyBranch, DependencyTree } from '../interfaces/main' +import { Dependency, DependencyBranch, DependencyTree, Emitter } from '../interfaces' import TypingsError from './error' /** @@ -35,6 +35,7 @@ const DEFAULT_DEPENDENCY: DependencyTree = { */ export interface Options { cwd: string + emitter: Emitter dev?: boolean peer?: boolean ambient?: boolean @@ -125,13 +126,19 @@ function resolveFileDependency (location: string, raw: string, options: Options, return resolveTypeDependencyFrom(src, raw, options, parent) } - // Resolve direct typings using `typings` property. - return Promise.resolve(extend(DEFAULT_DEPENDENCY, { + options.emitter.emit('resolve', { src, raw, parent }) + + const tree: DependencyTree = extend(DEFAULT_DEPENDENCY, { typings: src, src, raw, parent - })) + }) + + options.emitter.emit('resolved', { src, tree, raw, parent }) + + // Resolve direct typings using `typings` property. + return Promise.resolve(tree) } /** @@ -164,6 +171,8 @@ function resolveBowerDependencyFrom ( ): Promise { checkCircularDependency(parent, src) + options.emitter.emit('resolve', { src, raw, parent }) + return readJson(src) .then( function (bowerJson: any = {}) { @@ -191,6 +200,8 @@ function resolveBowerDependencyFrom ( tree.dependencies = dependencies tree.devDependencies = devDependencies + options.emitter.emit('resolved', { src, tree, raw, parent }) + return mergeDependencies(tree, typedPackage) }) }, @@ -225,11 +236,11 @@ function resolveBowerDependencyMap ( parent: DependencyTree ): Promise { const keys = Object.keys(dependencies) - const { cwd } = options + const { cwd, emitter } = options return Promise.all(keys.map(function (name) { const modulePath = resolve(componentPath, name, 'bower.json') - const resolveOptions: Options = { dev: false, ambient: false, peer: false, cwd } + const resolveOptions: Options = { dev: false, ambient: false, peer: false, cwd, emitter } return resolveBowerDependencyFrom(modulePath, `bower:${name}`, componentPath, resolveOptions, parent) })) @@ -257,13 +268,15 @@ export function resolveNpmDependencies (options: Options): Promise { checkCircularDependency(parent, src) + options.emitter.emit('resolve', { src, raw, parent }) + return readJson(src) .then( function (packageJson: any = {}) { const tree = extend(DEFAULT_DEPENDENCY, { name: packageJson.name, version: packageJson.version, - main: packageJson.main || 'index.js', + main: packageJson.main, browser: packageJson.browser, typings: packageJson.typings, browserTypings: packageJson.browserTypings, @@ -287,6 +300,8 @@ function resolveNpmDependencyFrom (src: string, raw: string, options: Options, p tree.devDependencies = devDependencies tree.peerDependencies = peerDependencies + options.emitter.emit('resolved', { src, tree, raw, parent }) + return mergeDependencies(tree, typedPackage) }) }, @@ -304,7 +319,7 @@ function resolveNpmDependencyMap (src: string, dependencies: any, options: Optio const keys = Object.keys(dependencies) return Promise.all(keys.map(function (name) { - const resolveOptions: Options = { dev: false, peer: false, ambient: false, cwd } + const resolveOptions: Options = { dev: false, peer: false, ambient: false, cwd, emitter: options.emitter } return resolveNpmDependency(join(name, 'package.json'), `npm:${name}`, resolveOptions, parent) })) @@ -332,6 +347,8 @@ export function resolveTypeDependencies (options: Options): Promise( function (config) { @@ -370,6 +387,8 @@ function resolveTypeDependencyFrom (src: string, raw: string, options: Options, tree.ambientDependencies = ambientDependencies tree.ambientDevDependencies = ambientDevDependencies + options.emitter.emit('resolved', { src, tree, raw, parent }) + return tree }) }, @@ -394,7 +413,7 @@ function resolveTypeDependencyMap (src: string, dependencies: any, options: Opti const keys = Object.keys(dependencies) return Promise.all(keys.map(function (name) { - const resolveOptions: Options = { dev: false, ambient: false, peer: false, cwd } + const resolveOptions: Options = { dev: false, ambient: false, peer: false, cwd, emitter: options.emitter } return resolveDependency(parseDependency(dependencies[name]), resolveOptions, parent) })) diff --git a/src/typings.ts b/src/typings.ts index 47bfb21..ba7370b 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -3,5 +3,6 @@ export * from './uninstall' export * from './init' export * from './bundle' export * from './search' +export * from './interfaces' export const VERSION = require('../package.json').version diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 10acdfa..ec427b4 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -21,7 +21,7 @@ import template = require('string-template') import { CONFIG_FILE, TYPINGS_DIR, DTS_MAIN_FILE, DTS_BROWSER_FILE, PRETTY_PROJECT_NAME, HOMEPAGE } from './config' import { isHttp, toDefinition } from './path' import { parseReferences, stringifyReferences } from './references' -import { ConfigJson } from '../interfaces/main' +import { ConfigJson } from '../interfaces' import { CompiledOutput } from '../lib/compile' import rc from './rc' import debug from './debug' diff --git a/src/utils/parse.ts b/src/utils/parse.ts index 4bbe6a9..75d5f3a 100644 --- a/src/utils/parse.ts +++ b/src/utils/parse.ts @@ -1,7 +1,7 @@ import invariant = require('invariant') import { parse, format, resolve as resolveUrl } from 'url' import { normalize, join, basename, dirname } from 'path' -import { Dependency } from '../interfaces/main' +import { Dependency } from '../interfaces' import { CONFIG_FILE } from './config' import { isDefinition, normalizeSlashes, inferDefinitionName, sanitizeDefinitionName } from './path' import rc from './rc'