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

Commit

Permalink
Support name and source flags for installation
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jun 15, 2016
1 parent 7151bf0 commit b1cfdd1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -51,6 +51,7 @@
"has": "^1.0.1",
"invariant": "^2.2.0",
"is-absolute": "^0.2.3",
"listify": "^1.0.0",
"lockfile": "^1.0.1",
"make-error-cause": "^1.1.0",
"mkdirp": "^0.5.1",
Expand Down
4 changes: 3 additions & 1 deletion src/install.ts
Expand Up @@ -33,6 +33,8 @@ export interface InstallDependencyOptions {
savePeer?: boolean
global?: boolean
cwd: string
name?: string
source?: string
emitter?: Emitter
}

Expand Down Expand Up @@ -145,7 +147,7 @@ export function installDependencyRaw (raw: string, options: InstallDependencyOpt
*/
export function installDependenciesRaw (raw: string[], options: InstallDependencyOptions): Promise<InstallResult[]> {
return new Promise(resolve => {
const expressions = raw.map(x => parseDependencyExpression(x))
const expressions = raw.map(x => parseDependencyExpression(x, options))

return resolve(installDependencies(expressions, options))
})
Expand Down
6 changes: 4 additions & 2 deletions src/lib/compile.spec.ts
Expand Up @@ -577,7 +577,8 @@ test('compile', t => {
t.equal(
result.message,
'Attempted to compile "test" as an external module, ' +
'but it looks like a global module.'
'but it looks like a global module. ' +
'Did you want to remove the global flag?'
)
})
})
Expand Down Expand Up @@ -605,7 +606,8 @@ test('compile', t => {
t.equal(
result.message,
'Attempted to compile "test" as a global module, ' +
'but it looks like an external module.'
'but it looks like an external module. ' +
'Did you want to enable the global flag?'
)
})
})
Expand Down
6 changes: 4 additions & 2 deletions src/lib/compile.ts
Expand Up @@ -494,7 +494,8 @@ function stringifySourceFile (sourceFile: ts.SourceFile, options: StringifyOptio
if (isExternal) {
throw new TypingsError(
`Attempted to compile "${name}" as a global ` +
`module, but it looks like an external module.`
`module, but it looks like an external module. ` +
`Did you want to enable the global flag?`
)
}

Expand All @@ -503,7 +504,8 @@ function stringifySourceFile (sourceFile: ts.SourceFile, options: StringifyOptio
if (!isExternal && !(parentModule && parentModule.isExternal)) {
throw new TypingsError(
`Attempted to compile "${name}" as an external module, ` +
`but it looks like a global module.`
`but it looks like a global module. ` +
`Did you want to remove the global flag?`
)
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/lib/dependencies.ts
@@ -1,4 +1,5 @@
import extend = require('xtend')
import listify = require('listify')
import invariant = require('invariant')
import zipObject = require('zip-object')
import Promise = require('any-promise')
Expand All @@ -9,6 +10,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 { search } from '../search'
import { Dependency, DependencyBranch, Dependencies, DependencyTree, Emitter } from '../interfaces'
import TypingsError from './error'

Expand Down Expand Up @@ -110,12 +112,23 @@ function resolveDependencyRegistry (dependency: Dependency, options: Options) {
function (error) {
// Wrap 404 responses in user prompt.
if (error.code === 'EINVALIDSTATUS' && error.status === 404) {
const message = `Unable to find "${meta.name}" ("${meta.source}") in the registry. ` +
`Did you want to try searching another source? ` +
`Also, if you want contribute these typings, please help us: ` +
`https://github.com/typings/registry`
return search({ name: meta.name })
.then(res => {
let message = `Unable to find "${meta.name}" ("${meta.source}") in the registry.`

return Promise.reject(new TypingsError(message, error))
if (res.total > 0) {
const plur = res.total === 1 ? 'source' : 'sources'

message += `\nHowever, we found "${meta.name}" for ${res.total} other ${plur}: `
message += `${listify(res.results.map(x => JSON.stringify(x.source)))}`
message += `\nYou can install these using the "source" option.`
}

message += '\nWe could use your help adding these typings to the registry: '
message += 'https://github.com/typings/registry'

return Promise.reject(new TypingsError(message, error))
})
}

return Promise.reject(error)
Expand Down
20 changes: 13 additions & 7 deletions src/utils/parse.ts
Expand Up @@ -223,17 +223,23 @@ export function resolveDependency (raw: string, filename: string) {
throw new TypeError(`Unable to resolve dependency from "${raw}"`)
}

/**
* Options to use when parsing a dependency string.
*/
export interface ParseDependencyOptions {
name?: string
source?: string
}

/**
* Parse and expand the CLI dependency expression.
*/
export function parseDependencyExpression (raw: string) {
export function parseDependencyExpression (raw: string, options?: ParseDependencyOptions) {
const [, name, scheme, registry] = /^(?:([^=!:#]+)=)?(?:([\w]+\:.+)|((?:[\w]+\~)?.+))$/.exec(raw)

const location = scheme || expandRegistry(registry)

return {
name,
location
name: name || options.name,
location: scheme || expandRegistry(registry)
}
}

Expand Down Expand Up @@ -265,13 +271,13 @@ export function buildDependencyExpression (type: string, meta: DependencyMeta):
/**
* Parse the registry dependency string.
*/
export function expandRegistry (raw: string) {
export function expandRegistry (raw: string, options: ParseDependencyOptions = {}) {
if (typeof raw !== 'string') {
throw new TypeError(`Expected registry name to be a string, not ${typeof raw}`)
}

const indexOf = raw.indexOf('~')
let source = rc.defaultSource
let source = options.source || rc.defaultSource
let name: string

if (indexOf === -1) {
Expand Down
2 changes: 1 addition & 1 deletion typings.json
Expand Up @@ -9,7 +9,7 @@
"has": "github:typings/typed-has#baa49a87ea7b1b837b7c3179faa6ea69263a78a6",
"invariant": "github:typings/typed-invariant#58403cee078ebef52112c4227c4d23a97821ef5c",
"is-absolute": "github:typings/typed-is-absolute#229fe8976b51bb721e92da2353e34bceaae0c108",
"listify": "github:typings/typed-listify#ff7fcd328b8285b38c9e941945bde33e8f249147",
"listify": "registry:npm/listify#1.0.0+20160211003958",
"lockfile": "github:typings/typed-lockfile#de6a705d24efa52c62cd0da4eadfeb3d642058bd",
"log-update": "github:typings/typed-log-update#e0e02e19ab2465c505337bdbb9f535a1d69fb817",
"make-error-cause": "npm:make-error-cause",
Expand Down

0 comments on commit b1cfdd1

Please sign in to comment.