Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

improved exception handler, flag to keep unreferenced files #180

Merged
merged 8 commits into from
Jun 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ dump/
_tmp.*

npm-debug.log

tsd-debug.log
57 changes: 34 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# TSD

[![Build Status](https://secure.travis-ci.org/DefinitelyTyped/tsd.svg)](http://travis-ci.org/DefinitelyTyped/tsd) [![NPM version](https://badge.fury.io/js/tsd.svg)](http://badge.fury.io/js/tsd) [![Dependency Status](https://david-dm.org/DefinitelyTyped/tsd.svg)](https://david-dm.org/DefinitelyTyped/tsd) [![devDependency Status](https://david-dm.org/DefinitelyTyped/tsd/dev-status.svg)](https://david-dm.org/DefinitelyTyped/tsd#info=devDependencies)
[![Build Status](https://travis-ci.org/DefinitelyTyped/tsd.svg?branch=master)](https://travis-ci.org/DefinitelyTyped/tsd) [![Build status](https://ci.appveyor.com/api/projects/status/v4w7p3qvcuxhte33/branch/master?svg=true)](https://ci.appveyor.com/project/Diullei/tsd/branch/master) [![NPM version](https://badge.fury.io/js/tsd.svg)](http://badge.fury.io/js/tsd) [![Dependency Status](https://david-dm.org/DefinitelyTyped/tsd.svg)](https://david-dm.org/DefinitelyTyped/tsd) [![devDependency Status](https://david-dm.org/DefinitelyTyped/tsd/dev-status.svg)](https://david-dm.org/DefinitelyTyped/tsd#info=devDependencies)

> TypeScript Definition manager for DefinitelyTyped

TSD is a package manager to search and install [TypeScript](http://www.typescriptlang.org/) definition files directly from the community driven [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) repository.
TSD is a package manager to search and install [TypeScript](http://www.typescriptlang.org/) definition files directly from the community driven [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) repository.


## Install
Expand All @@ -30,6 +30,9 @@ $ tsd query angularjs/
$ tsd query jquery.*
# glob on mac/linux
$ tsd query "jquery.*"

#install all definitions from tsd.json
$ tsd install
````

### Commands
Expand Down Expand Up @@ -161,6 +164,14 @@ Install `mocha`, `chai` and `sinon` definitions all at once, with resolve refere
$ tsd install mocha chai sinon -ros
````

Install all definitions from `tsd.json`:

````bash
$ tsd install
````

> NOTE: `tsd install` will work like `tsd reinstall --save --overwrite`

#### Reinstall definitions

Reset the definitions to the commits listed in `tsd.json`:
Expand Down Expand Up @@ -232,7 +243,7 @@ $ tsd rebundle
````


#### Rate-limit
#### Rate-limit

Print current Github rate-limit info

Expand Down Expand Up @@ -282,7 +293,7 @@ other/plugin.d.ts
Notice the pattern, and ignore the `.d.ts` extension:

````html
<project>/<module><semver>.d.ts
<project>/<module><semver>.d.ts
````

Select definitions using only the module name:
Expand Down Expand Up @@ -366,7 +377,7 @@ $ tsd query d3 -d "<2012-01-01"

### Commit filter

Use the `--commit` / `-c` option to supply sha1-hash of a commit (find a commit hash using `--history`), for convenience a shortened sha1 hash is supported.
Use the `--commit` / `-c` option to supply sha1-hash of a commit (find a commit hash using `--history`), for convenience a shortened sha1 hash is supported.

````bash
$ tsd query youtube --history
Expand Down Expand Up @@ -404,7 +415,7 @@ An example configuration the 'node.js' definition installed would look like this
"installed": {
"node/node.d.ts": {
"commit": "6834f97fb33561a3ad40695084da2b660efaee29"
}
}
}
}
````
Expand All @@ -429,7 +440,7 @@ The `tsd.d.ts` file refers every definition that is installed with `--save` for

````
/// <reference path="../typings/tsd.d.ts" />
````
````

By default it is created in the typings folder but the name and location are configurable in `tsd.json`. When adding new references TSD will check the existing references and respects re-ordering (ordering is important for inter-dependent definitions).

Expand Down Expand Up @@ -478,7 +489,7 @@ You can create this token on Github.com:

1. Go to https://github.com/settings/tokens/new
3. *Deselect* all scopes to create a token with just basic authentication.
1. (verify you *really* deselected all scopes)
1. (verify you *really* deselected all scopes)
5. (wonder why these presets were set??)
2. Enter a identifying name, something like "`TSD Turbo 5000`"
5. Create the token.
Expand All @@ -487,13 +498,13 @@ You can create this token on Github.com:

Change or revoke the token at any time on https://github.com/settings/applications

Note: keep in mind the `.tsdrc` file is *not* secured. Don't use a token with additional scope unless you know what you are doing.
Note: keep in mind the `.tsdrc` file is *not* secured. Don't use a token with additional scope unless you know what you are doing.

The bare 'no scope' token is *relatively* harmless as it gives 'read-only access to public information', same as any non-authenticated access. But it does *identify* any requests done with it as being *yours*, so it is still *your* responsibility to keep the token private.

## Usage as module
TSD can be used as a JavaScript npm dependency:

TSD can be used as a JavaScript npm dependency:

````js
var tsd = require('tsd');
Expand All @@ -513,9 +524,9 @@ TSD uses Promise/A+ by [bluebird](https://github.com/petkaantonov/bluebird). :po

API export is somewhat experimental; take care to lock versions and test on upgrade. If you plan to use TSD as module in a tool or project then feel free to [leave a message](https://github.com/DefinitelyTyped/tsd/issues) and coordinate stuff.

### API docs
### API docs

Not yet.
Not yet.

## Notes

Expand Down Expand Up @@ -564,27 +575,27 @@ It works well but is not used much in the current DefinitelyTyped repository. Th

### What is the location of the cache folders?

The cache is stored in the users home directory (like `$ npm`). Use `$ tsd settings` to view the current paths. Use the `--cacheDir` to override the cache directory, or `--cacheMode` to modify caching behaviour.
The cache is stored in the users home directory (like `$ npm`). Use `$ tsd settings` to view the current paths. Use the `--cacheDir` to override the cache directory, or `--cacheMode` to modify caching behaviour.

### I have a suggestion or contribution

Feel free to leave a [ticket](https://github.com/DefinitelyTyped/tsd/issues). Questions and contributions for the definition files go [here](https://github.com/borisyankov/DefinitelyTyped/issues).

## History

### v0.6.x ( > 2014-10)
### v0.6.x ( > 2014-10)

* Long delayed release of major overhaul: migrated code to external module style and reworked many features, subsystems and dependencies. Development in [dev/next](https://github.com/DefinitelyTyped/tsd/tree/dev/next) branch.

* Interesting changes
* Interesting changes
* added `install` command to CLI
* improved module-name pattern matching
* refreshed CLI interface, formatting, help
* reworked http download/cache
* switched many modules, added `definition-header`
* replaced ugly node-gyp sub-dependency

### v0.5.x ( > 2013-08)
### v0.5.x ( > 2013-08)

* Current release versions. See the [release tags](https://github.com/DefinitelyTyped/tsd/releases) fore more details.

Expand Down Expand Up @@ -670,9 +681,9 @@ Contributions are very welcome; please discuss larger changes in a [ticket](http

The TSD CLI tool collects definition usage information, like the queries made to the repo and the definitions that get installed. The information collected amounts to about same level of detail as services like npm or github would collect (maybe even less as we don't track a user id).

The API does not track anything.
The API does not track anything.

To store this TSD uses [Google Analytics](http://www.google.com/analytics/) in the excellent [universal-analytics](https://npmjs.org/package/universal-analytics) package. We might at some point publish some anonymised aggregate stats to the DefinitelyTyped website.
To store this TSD uses [Google Analytics](http://www.google.com/analytics/) in the excellent [universal-analytics](https://npmjs.org/package/universal-analytics) package. We might at some point publish some anonymised aggregate stats to the DefinitelyTyped website.

Changes to the policy should be announced in release notes, and ideally ask confirmation on the first CLI use.

Expand All @@ -681,9 +692,9 @@ Keep in mind we're just devs like you and are working on this in our spare time;

## Security

Please close read the relevant sections of the readme, especially on OAuth 'scope'.
Please close read the relevant sections of the readme, especially on OAuth 'scope'.

The optional [Github OAuth token](http://developer.github.com/v3/oauth/) is only used to authenticate with the Github API. The token is not stored anywhere but the local machine. It is your responsibility to keep your token safe.
The optional [Github OAuth token](http://developer.github.com/v3/oauth/) is only used to authenticate with the Github API. The token is not stored anywhere but the local machine. It is your responsibility to keep your token safe.

Using an OAuth token with additional scope is *neither advised nor supported*, even though it could make TSD work with private repositories. But it might also leak repo or content names to analytics or leave a bare http cache in your temp dir. If this bothers you please review the license and/or leave a message.

Expand All @@ -692,12 +703,12 @@ Using an OAuth token with additional scope is *neither advised nor supported*, e

Copyright (c) 2014 by [Bart van der Schoor](https://github.com/Bartvds) @ [DefinitelyTyped](https://github.com/DefinitelyTyped)

Licensed under the [Apache License, Version 2.0](https://raw.github.com/DefinitelyTyped/tsd/master/LICENSE.txt).
Licensed under the [Apache License, Version 2.0](https://raw.github.com/DefinitelyTyped/tsd/master/LICENSE.txt).

* note: there is some imported MIT licensed code by myself, [Bart van der Schoor](https://github.com/Bartvds)

Copyright (c) 2012 by [Diullei Gomes](https://github.com/Diullei).

Licensed under the MIT License.
Licensed under the MIT License.

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/DefinitelyTyped/tsd/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"assertion-error": "1.0.0",
"bl": "^0.9.3",
"bluebird": "~1.2.4",
"chalk": "^1.0.0",
"colors": "^1.1.0",
"deep-freeze": "0.0.1",
"definition-header": "~0.1.0",
Expand Down
12 changes: 10 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,13 @@

// note: it is critical this file has unix line-endings

require('./bootstrap');
require('./tsd/CLI').runARGV(process.argv);
var errHandler = require('./tsd/util/error-handler');

process.on('uncaughtException', function(err) {
errHandler.handler(err);
});

setTimeout(function() {
require('./bootstrap');
require('./tsd/CLI').runARGV(process.argv);
}, 500);
7 changes: 6 additions & 1 deletion src/http/CacheStreamLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,13 @@ class CacheStreamLoader {

// check status
if (res.statusCode < 200 || res.statusCode >= 400) {
var errMsg = 'unexpected status code: ' + res.statusCode + ' on: ' + this.request.url;
if (res.statusCode === 403) {
errMsg = 'EGITIHAPI, ' + errMsg;
}

// TODO add custom errors
reject(new Error('unexpected status code: ' + res.statusCode + ' on: ' + this.request.url));
reject(new Error(errMsg));
return;
}
if (res.statusCode === 304) {
Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
"./tsd/support/PackageLinker.ts",
"./tsd/util/DateComp.ts",
"./tsd/util/defUtil.ts",
"./tsd/util/error-handler.ts",
"./xm/_ref.d.ts",
"./xm/assert.ts",
"./xm/assertVar.ts",
Expand Down
16 changes: 11 additions & 5 deletions src/tsd/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,17 @@ class API {
}).then(() => {
return this.saveBundles(res.written.values(), options);
}).then(() => {
this.core.installer.removeUnusedReferences(
this.context.config.getInstalled(), this.core.context.config.toJSON().path).then((removedList: string[]) => {
options.overwriteFiles = options.saveBundle = true;
return this.saveBundles(this.context.config.getInstalledAsDefVersionList(), options);
});
if (!options.keepUnreferencedDefs) {
this.core.installer.removeUnusedReferences(
this.context.config.getInstalled(), this.core.context.config.toJSON().path).then((removedList: string[]) => {
options.overwriteFiles = options.saveBundle = true;
return this.saveBundles(this.context.config.getInstalledAsDefVersionList(), options);
});
} else {
options.overwriteFiles = options.saveBundle = true;
return this.saveBundles(this.context.config.getInstalledAsDefVersionList(), options);
}
return null;
}).return(res);
}

Expand Down
23 changes: 16 additions & 7 deletions src/tsd/CLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

'use strict';

import errHandler = require('./util/error-handler');

import path = require('path');
import fs = require('fs');

import Promise = require('bluebird');
import VError = require('verror');
Expand Down Expand Up @@ -172,6 +175,7 @@ export function getExpose(): Expose {
job.options.overwriteFiles = ctx.getOpt(Opt.overwrite);
job.options.resolveDependencies = ctx.getOpt(Opt.resolve);
job.options.addToBundles = ctx.getOpt(Opt.bundle);
job.options.keepUnreferencedDefs = ctx.getOpt(Opt.keepUnreferenced);

if (ctx.hasOpt(Opt.cacheMode)) {
job.api.core.useCacheMode(ctx.getOpt(Opt.cacheMode));
Expand Down Expand Up @@ -218,13 +222,12 @@ export function getExpose(): Expose {
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

var expose = new Expose(output);

function reportError(err: any, head: boolean = true): void {
tracker.error(err);
print.reportError(err, head);
}
errHandler.handler(err);
};

function link(job: Job): Promise<PackageDefinition[]> {
return job.api.link(job.api.context.paths.startCwd).then((packages: PackageDefinition[]) => {
Expand Down Expand Up @@ -314,7 +317,6 @@ export function getExpose(): Expose {
return getAPIJob(ctx).then((job: Job) => {
output.line();
output.info(true).span('running').space().accent(cmd.name).ln();

return job.api.reinstall(job.options).then((result: InstallResult) => {
print.installResult(result);

Expand Down Expand Up @@ -439,8 +441,8 @@ export function getExpose(): Expose {
cmd.label = 'install definitions using one or more globbing patterns.';
cmd.examples = [
['tsd install mocha', 'install mocha'],
['tsd install angularjs/', 'install full angularjs bundle']
// ['tsd install', 'perform reinstall command']
['tsd install angularjs/', 'install full angularjs bundle'],
['tsd install', 'perform reinstall command']
];
cmd.variadic = ['...pattern'];
cmd.groups = [Group.query];
Expand Down Expand Up @@ -551,7 +553,7 @@ export function getExpose(): Expose {
expose.defineCommand((cmd: ExposeCommand) => {
cmd.name = 'reinstall';
cmd.label = 're-install definitions from config';
cmd.options = [Opt.overwrite, Opt.save];
cmd.options = [Opt.overwrite, Opt.save, Opt.keepUnreferenced];
cmd.groups = [Group.manage];
cmd.execute = (ctx: ExposeContext) => {
return executeReinstall(ctx, cmd);
Expand Down Expand Up @@ -658,6 +660,13 @@ export function getExpose(): Expose {

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// trying to remove tsd-debug.log if exists
try {
if (fs.existsSync(path.resolve(process.cwd(), 'tsd-debug.log'))) {
fs.unlinkSync(path.resolve(process.cwd(), 'tsd-debug.log'));
}
} catch (e) { /*...*/ }

/*
runARGV: run raw cli arguments, like process.argv
*/
Expand Down
1 change: 1 addition & 0 deletions src/tsd/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Options {
overwriteFiles: boolean = false;
saveToConfig: boolean = false;
saveBundle: boolean = false;
keepUnreferencedDefs: boolean = false;
addToBundles: string[] = [];

static fromJSON(json: Object): Options {
Expand Down
8 changes: 8 additions & 0 deletions src/tsd/cli/addCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ function addCommon(expose: Expose, print: CLIPrinter, style: StyleMap): void {
opt.placeholder = 'name';
opt.enum = [Action.install, Action.browse, Action.visit]; // , Action.compare, Action.update, Action.open];
});

expose.defineOption((opt: ExposeOption) => {
opt.name = Opt.keepUnreferenced;
opt.short = 'k';
opt.description = 'keep unreferenced typings when using reinstal command';
opt.type = 'flag';
opt.default = false;
});
}

export = addCommon;
1 change: 1 addition & 0 deletions src/tsd/cli/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export module Opt {
export var info = 'info';
export var history = 'history';
export var services = 'services';
export var keepUnreferenced = 'keep-unreferenced';
}
objectUtils.lockPrimitives(Opt);

Expand Down
Loading