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

Commit

Permalink
Improve HTTP query support, infer name from .d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Mar 11, 2016
1 parent 109ad56 commit 477f029
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 92 deletions.
8 changes: 6 additions & 2 deletions scripts/bootstrap.js
Expand Up @@ -27,11 +27,15 @@ if (!which('tsc')) {

if (exec('tsc', { silent: true }).code !== 0) {
console.log('tsc completed build as expected')
echo('')
console.log('')
}

require('../')
.install({ cwd: process.cwd() })
.then(function () {
echo('Success!')
console.log('Success!')
})
.catch(function (err) {
console.log(err.toString())
console.log(err.stack)
})
5 changes: 2 additions & 3 deletions src/init.ts
@@ -1,10 +1,9 @@
import Promise = require('any-promise')
import extend = require('xtend')
import { join } from 'path'
import { join, basename } from 'path'
import { ConfigJson } from './interfaces'
import { writeJson, isFile, readJson } from './utils/fs'
import { CONFIG_FILE } from './utils/config'
import { inferDefinitionName } from './utils/path'

const TSD_JSON_FILE = 'tsd.json'
const DEFINITELYTYPED_REPO = 'DefinitelyTyped/DefinitelyTyped'
Expand Down Expand Up @@ -71,7 +70,7 @@ function upgradeTsdJson (tsdJson: TsdJson, config?: ConfigJson): ConfigJson {

Object.keys(tsdJson.installed).forEach(function (path) {
const dependency = tsdJson.installed[path]
const name = inferDefinitionName(path)
const name = basename(path, '.d.ts')
const location = `github:${repo}/${path}#${dependency.commit}`

typingsJson.ambientDependencies[name] = location
Expand Down
16 changes: 2 additions & 14 deletions src/install.spec.ts
Expand Up @@ -67,7 +67,7 @@ test('install', t => {
const DEPENDENCY = '@scope/test=file:custom_typings/definition.d.ts'
const REGISTRY_DEPENDENCY = 'registry:dt/node@>=4.0'
const PEER_DEPENDENCY = 'file:custom_typings/named/typings.json'
const AMBIENT_DEPENDENCY = 'ambient-test=file:custom_typings/ambient.d.ts'
const AMBIENT_DEPENDENCY = 'file:custom_typings/ambient.d.ts'
const FIXTURE_DIR = join(__dirname, '__test__/install-dependency-fixture')
const CONFIG = join(FIXTURE_DIR, CONFIG_FILE)

Expand Down Expand Up @@ -131,24 +131,12 @@ test('install', t => {
node: 'registry:dt/node#4.0.0+20160226132328'
},
ambientDevDependencies: {
'ambient-test': 'file:custom_typings/ambient.d.ts'
ambient: 'file:custom_typings/ambient.d.ts'
}
})
})
})

t.test('reject install if name is missing', t => {
const DEPENDENCY = 'file:custom_typings/definition.d.ts'
const FIXTURE_DIR = join(__dirname, '__test__/install-dependency-fixture')

t.plan(1)

return installDependencyRaw(DEPENDENCY, { cwd: FIXTURE_DIR, emitter })
.catch(err => {
t.ok(/^Unable to install dependency/.test(err.message))
})
})

t.test('install empty', t => {
const FIXTURE_DIR = join(__dirname, '__test__/install-empty')

Expand Down
30 changes: 26 additions & 4 deletions src/lib/compile.spec.ts
Expand Up @@ -622,7 +622,7 @@ test('compile', t => {
raw: undefined,
postmessage: undefined,
ambient: false,
typings: 'http://example.com/index.d.ts',
main: 'http://example.com/index.d.ts?query=test',
dependencies: {},
devDependencies: {},
peerDependencies: {},
Expand All @@ -633,13 +633,35 @@ test('compile', t => {
const emitter = new EventEmitter()

nock('http://example.com')
.get('/index.d.ts')
.get('/index.d.ts?query=test')
.matchHeader('User-Agent', /^typings\/\d+\.\d+\.\d+ node\/v\d+\.\d+\.\d+.*$/)
.reply(200, 'export const helloWorld: string')
.reply(200, 'export * from "./test"')

nock('http://example.com')
.get('/test.d.ts?query=test')
.reply(200, 'export const test: boolean')

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}\n`)
t.equal(result.main, [
'declare module \'~test/test\' {',
'export const test: boolean',
'}',
'declare module \'test/test\' {',
'export * from \'~test/test\';',
'}',
'',
'declare module \'~test/index\' {',
'export * from \'~test/test\'',
'}',
'declare module \'test/index\' {',
'export * from \'~test/index\';',
'}',
'declare module \'test\' {',
'export * from \'~test/index\';',
'}',
''
].join('\n'))
})
})

Expand Down
16 changes: 8 additions & 8 deletions src/lib/compile.ts
Expand Up @@ -6,7 +6,7 @@ import { join, relative, basename } from 'path'
import { DependencyTree, Overrides, Emitter } from '../interfaces'
import { readFileFrom } from '../utils/fs'
import { EOL, normalizeEOL } from '../utils/path'
import { resolveFrom, relativeTo, isHttp, isModuleName, normalizeSlashes, fromDefinition, normalizeToDefinition, toDefinition } from '../utils/path'
import { resolveFrom, relativeTo, isHttp, isModuleName, normalizeSlashes, pathFromDefinition, normalizeToDefinition, toDefinition } from '../utils/path'
import { REFERENCE_REGEXP } from '../utils/references'
import { PROJECT_NAME, CONFIG_FILE, DEPENDENCY_SEPARATOR } from '../utils/config'
import { resolveDependency } from '../utils/parse'
Expand Down Expand Up @@ -75,7 +75,7 @@ interface CompileOptions extends Options {
/**
* Resolve override paths.
*/
function resolveFromWithModuleNameOverride (src: string, to: string | boolean): string {
function resolveFromOverride (src: string, to: string | boolean): string {
if (typeof to === 'string') {
if (isModuleName(to)) {
const [moduleName, modulePath] = getModuleNameParts(to)
Expand Down Expand Up @@ -130,8 +130,8 @@ function getStringifyOptions (
overrides[mainDefinition] = browserDefinition
} else {
for (const key of Object.keys(browser)) {
const from = resolveFromWithModuleNameOverride(tree.src, key) as string
const to = resolveFromWithModuleNameOverride(tree.src, browser[key])
const from = resolveFromOverride(tree.src, key) as string
const to = resolveFromOverride(tree.src, browser[key])

overrides[from] = to
}
Expand Down Expand Up @@ -413,10 +413,10 @@ function importPath (path: string, name: string, options: StringifyOptions) {
return name
}

return `${prefix}${DEPENDENCY_SEPARATOR}${modulePath ? fromDefinition(resolved) : resolved}`
return `${prefix}${DEPENDENCY_SEPARATOR}${modulePath ? pathFromDefinition(resolved) : resolved}`
}

const relativePath = relativeTo(tree.src, fromDefinition(resolved))
const relativePath = relativeTo(tree.src, pathFromDefinition(resolved))

return normalizeSlashes(join(prefix, relativePath))
}
Expand Down Expand Up @@ -545,8 +545,8 @@ function stringifyFile (path: string, rawContents: string, rawPath: string, opti
return meta + declareText(parent ? moduleName : name, moduleText)
}

const modulePath = importPath(path, fromDefinition(path), options)
const prettyPath = normalizeSlashes(join(name, relativeTo(tree.src, fromDefinition(path))))
const modulePath = importPath(path, pathFromDefinition(path), options)
const prettyPath = normalizeSlashes(join(name, relativeTo(tree.src, pathFromDefinition(path))))
const declared = declareText(modulePath, moduleText)

if (!isEntry) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/fs.ts
Expand Up @@ -19,7 +19,7 @@ import { join, dirname } from 'path'
import { parse as parseUrl } from 'url'
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, EOL, detectEOL, normalizeEOL } from './path'
import { isHttp, EOL, detectEOL, normalizeEOL } from './path'
import { parseReferences, stringifyReferences } from './references'
import { ConfigJson } from '../interfaces'
import { CompiledOutput } from '../lib/compile'
Expand Down

0 comments on commit 477f029

Please sign in to comment.