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

Commit

Permalink
Add an emitter for sending messages to clients
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Mar 6, 2016
1 parent 41f3b6e commit 6ba700f
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 142 deletions.
6 changes: 5 additions & 1 deletion src/bundle.spec.ts
Expand Up @@ -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 => {
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions 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.
Expand All @@ -12,19 +14,21 @@ export interface BundleOptions {
cwd: string
ambient: boolean
out: string
emitter?: Emitter
}

/**
* Bundle the current typings project into a single ambient definition.
*/
export function bundle (options: BundleOptions): Promise<CompiledOutput> {
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

Expand All @@ -34,7 +38,7 @@ export function bundle (options: BundleOptions): Promise<CompiledOutput> {
))
}

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)
Expand Down
2 changes: 1 addition & 1 deletion 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'
Expand Down
19 changes: 13 additions & 6 deletions src/install.spec.ts
Expand Up @@ -2,20 +2,23 @@ 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')

return rimraf(join(FIXTURE_DIR, 'typings'))
.then(() => {
return install({
cwd: FIXTURE_DIR,
production: false
emitter
})
})
.then(function () {
Expand Down Expand Up @@ -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
})
])
})
Expand Down Expand Up @@ -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))
})
Expand All @@ -123,7 +129,8 @@ test('install', t => {

return install({
cwd: FIXTURE_DIR,
production: false
dev: true,
emitter
})
.then(function () {
return Promise.all([
Expand Down
18 changes: 13 additions & 5 deletions 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.
Expand All @@ -19,6 +20,7 @@ export interface InstallDependencyOptions {
ambient?: boolean
name?: string
cwd: string
emitter?: Emitter
source?: string
}

Expand All @@ -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<Promise<any>> = []
Expand All @@ -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 }))
}
}

Expand Down Expand Up @@ -88,8 +94,9 @@ export function installDependency (dependency: string, options: InstallDependenc
function installTo (location: string, options: InstallDependencyOptions): Promise<CompiledOutput> {
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

Expand All @@ -101,6 +108,7 @@ function installTo (location: string, options: InstallDependencyOptions): Promis
cwd,
name,
ambient,
emitter,
meta: true
})
.then(result => {
Expand Down
43 changes: 43 additions & 0 deletions src/interfaces/main.ts → 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".
Expand Down Expand Up @@ -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
}

0 comments on commit 6ba700f

Please sign in to comment.