28 Commits

Author SHA1 Message Date
e05f7d32fd 1.0.16 2017-05-13 11:03:40 +02:00
5f0ddc6600 update tests and README 2017-05-13 11:03:24 +02:00
d312d6ae5a 1.0.15 2017-05-13 10:34:38 +02:00
8ca254595d 1.0.14 2017-05-13 10:34:19 +02:00
7661ae506d update ci 2017-05-13 10:34:08 +02:00
4e8dd83839 1.0.13 2017-05-13 10:33:38 +02:00
7ab1a4eec7 update README 2017-05-13 10:33:34 +02:00
3098fbec89 1.0.12 2016-12-11 11:01:07 +01:00
1caa8a7c31 improve README 2016-12-11 11:00:58 +01:00
c689a3c349 update npmextra.json 2016-12-10 22:15:22 +01:00
a8a0778708 1.0.11 2016-12-10 22:12:38 +01:00
82ebb181a0 update README 2016-12-10 22:12:24 +01:00
adabcb3c4a 1.0.10 2016-12-10 19:46:52 +01:00
32db51c49a update to latest standards 2016-12-10 19:46:47 +01:00
62b91adf0e 1.0.9 2016-10-14 04:08:43 +02:00
2f6a56c857 improve README 2016-10-14 04:08:38 +02:00
988cf907a4 1.0.8 2016-10-14 03:28:18 +02:00
18d79cb403 improve README 2016-10-14 03:27:52 +02:00
1d2c073206 1.0.7 2016-10-14 03:24:32 +02:00
fa85e689df add npm as default module loader 2016-10-14 03:24:29 +02:00
e5d18a75f8 1.0.6 2016-10-14 03:07:41 +02:00
87c9556064 now resolving paths with npm 2016-10-14 03:07:37 +02:00
f4b8766dae 1.0.5 2016-10-12 15:41:12 +02:00
ecaaaee54a add keywords, add better path resolution 2016-10-12 15:41:09 +02:00
46693c4bef 1.0.4 2016-10-12 14:15:31 +02:00
cd7361bb96 improve README 2016-10-12 14:15:27 +02:00
d3b4a252e9 1.0.3 2016-10-12 14:10:02 +02:00
abc9e7888e improve README 2016-10-12 14:09:58 +02:00
14 changed files with 455 additions and 75 deletions

View File

@ -1,20 +1,31 @@
image: hosttoday/ht-docker-node:npmts # gitzone standard
image: hosttoday/ht-docker-node:npmci
cache:
paths:
- .yarn/
key: "$CI_BUILD_STAGE"
stages: stages:
- test - test
- release - release
- trigger - trigger
- page - pages
testLEGACY: testLEGACY:
stage: test stage: test
script: script:
- npmci test legacy - npmci test legacy
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
allow_failure: true allow_failure: true
testLTS: testLTS:
stage: test stage: test
script: script:
- npmci test lts - npmci test lts
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
@ -22,8 +33,10 @@ testSTABLE:
stage: test stage: test
script: script:
- npmci test stable - npmci test stable
coverage: /\d+.?\d+?\%\s*coverage/
tags: tags:
- docker - docker
release: release:
stage: release stage: release
script: script:
@ -32,6 +45,7 @@ release:
- tags - tags
tags: tags:
- docker - docker
trigger: trigger:
stage: trigger stage: trigger
script: script:
@ -40,11 +54,15 @@ trigger:
- tags - tags
tags: tags:
- docker - docker
pages: pages:
image: hosttoday/ht-docker-node:npmpage image: hosttoday/ht-docker-node:npmci
stage: page stage: pages
script: script:
- npmci command npmpage --host gitlab - npmci command yarn global add npmpage
- npmci command npmpage
tags:
- docker
only: only:
- tags - tags
artifacts: artifacts:

View File

@ -2,14 +2,15 @@
simplifies lazy loading with TypeScript simplifies lazy loading with TypeScript
## Availabililty ## Availabililty
[![npm](https://push.rocks/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartsystem) [![npm](https://pushrocks.gitlab.io/assets/repo-button-npm.svg)](https://www.npmjs.com/package/smartsystem)
[![git](https://push.rocks/assets/repo-button-git.svg)](https://gitlab.com/pushrocks/smartsystem) [![git](https://pushrocks.gitlab.io/assets/repo-button-git.svg)](https://GitLab.com/pushrocks/smartsystem)
[![git](https://push.rocks/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartsystem) [![git](https://pushrocks.gitlab.io/assets/repo-button-mirror.svg)](https://github.com/pushrocks/smartsystem)
[![docs](https://push.rocks/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartsystem/) [![docs](https://pushrocks.gitlab.io/assets/repo-button-docs.svg)](https://pushrocks.gitlab.io/smartsystem/)
## Status for master ## Status for master
[![build status](https://gitlab.com/pushrocks/smartsystem/badges/master/build.svg)](https://gitlab.com/pushrocks/smartsystem/commits/master) [![build status](https://GitLab.com/pushrocks/smartsystem/badges/master/build.svg)](https://GitLab.com/pushrocks/smartsystem/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartsystem/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartsystem/commits/master) [![coverage report](https://GitLab.com/pushrocks/smartsystem/badges/master/coverage.svg)](https://GitLab.com/pushrocks/smartsystem/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/smartsystem.svg)](https://www.npmjs.com/package/smartsystem)
[![Dependency Status](https://david-dm.org/pushrocks/smartsystem.svg)](https://david-dm.org/pushrocks/smartsystem) [![Dependency Status](https://david-dm.org/pushrocks/smartsystem.svg)](https://david-dm.org/pushrocks/smartsystem)
[![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartsystem/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartsystem/master/dependencies/npm) [![bitHound Dependencies](https://www.bithound.io/github/pushrocks/smartsystem/badges/dependencies.svg)](https://www.bithound.io/github/pushrocks/smartsystem/master/dependencies/npm)
[![bitHound Code](https://www.bithound.io/github/pushrocks/smartsystem/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartsystem) [![bitHound Code](https://www.bithound.io/github/pushrocks/smartsystem/badges/code.svg)](https://www.bithound.io/github/pushrocks/smartsystem)
@ -20,25 +21,44 @@ simplifies lazy loading with TypeScript
## Usage ## Usage
We recommend the use of TypeScript for best Intellisense We recommend the use of TypeScript for best Intellisense
```typescript smartsystem supports both npm and SystemJs as module loader.
```javascript
import { LazyModule } from 'smartsystem' import { LazyModule } from 'smartsystem'
import * as myPluginType from 'myPlugin' // plugin does not get loaded here at runtime // plugin does not get loaded here at runtime
let myPluginLazy = new LazyModule<typeof myPlugin>('myPlugin') import * as _myPlugin from 'myPlugin'
import * as anotherPluginType from 'anotherPlugin' // plugin does not get loaded here at runtime // define the lazy module
let anotherPluginPromised = LazyModule<typeof anotherPlugin>('anotherPlugin') let myLazyModule = new LazyModule<typeof _myPlugin>('myPlugin', __dirname)
myPluginLazy.whenLoaded.then(myPlugin => { // set the loader, defaults to npm anyway, can be systemjs alternatively
myLazyModule.setLoader('npm')
// another plugin
import * as _anotherPlugin from 'anotherPlugin' // plugin does not get loaded here at runtime
// define lazy module
let anotherLazyModule = new LazyModule<typeof _anotherPlugin>('anotherPlugin', __dirname)
anotherPluginLazy.setLoader('systemjs') // sets the loader to systemjs
myLazyModule.whenLoaded.then(myPlugin => {
/* do something with myPlugin. /* do something with myPlugin.
myPlugin receives the typings flow from LazyModule class myPlugin receives the typings flow from LazyModule class
This does NOT load the module during runtime This does NOT load the module during runtime
The promise whenLoaded will be resolved whenever load() is called for the first time */ The promise whenLoaded will be resolved whenever load() is called for the first time */
}) })
myPluginLazy.load().then(myPlugin => { myLazyModule.load().then(myPlugin => {
/* do something with myPlugin. /* do something with myPlugin.
myPlugin receives the typings flow from LazyModule class myPlugin receives the typings flow from LazyModule class
This DOES LOAD the module */ This DOES LOAD the module */
}) })
``` ```
For further information read the linked docs at the top of this README.
> MIT licensed | **&copy;** [Lossless GmbH](https://lossless.gmbh)
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://push.rocks)

5
dist/index.d.ts vendored
View File

@ -4,13 +4,16 @@ import * as q from 'q';
/** /**
* defines a LazyModule * defines a LazyModule
*/ */
export declare type TLoader = 'npm' | 'systemjs';
export declare class LazyModule<T> { export declare class LazyModule<T> {
name: string; name: string;
nameIsPath: boolean; nameIsPath: boolean;
cwd: string; cwd: string;
whenLoaded: q.Promise<T>; whenLoaded: q.Promise<T>;
loader: TLoader;
private whenLoadedDeferred; private whenLoadedDeferred;
constructor(nameArg: string, cwdArg?: string); constructor(nameArg: string, cwdArg: string);
setLoader(loaderArg: TLoader): void;
/** /**
* loads the module * loads the module
*/ */

30
dist/index.js vendored
View File

@ -1,8 +1,9 @@
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("typings-global"); require("typings-global");
const path = require("path");
const q = require("q"); const q = require("q");
const lik_1 = require("lik"); const lik_1 = require("lik");
let systemjs = require('systemjs');
class Smartsystem { class Smartsystem {
constructor() { constructor() {
this.lazyModules = new lik_1.Objectmap(); this.lazyModules = new lik_1.Objectmap();
@ -16,29 +17,46 @@ class Smartsystem {
} }
// create the internal smartsystem // create the internal smartsystem
let smartsystem = new Smartsystem(); let smartsystem = new Smartsystem();
/**
* defines a LazyModule
*/
class LazyModule { class LazyModule {
constructor(nameArg, cwdArg = process.cwd()) { constructor(nameArg, cwdArg) {
this.loader = 'npm';
if (!cwdArg) {
throw new Error('You must specify a directory to resolve from!');
}
this.name = nameArg; this.name = nameArg;
this.cwd = cwdArg; this.cwd = cwdArg;
smartsystem.addLazyModule(this); // add module to smartsystem instance smartsystem.addLazyModule(this); // add module to smartsystem instance
this.nameIsPath = /\.\//.test(this.name); // figure out if name is path this.nameIsPath = /\.\//.test(this.name); // figure out if name is path
if (this.nameIsPath) {
this.name = path.join(this.cwd, this.name);
}
this.whenLoadedDeferred = q.defer(); this.whenLoadedDeferred = q.defer();
this.whenLoaded = this.whenLoadedDeferred.promise; this.whenLoaded = this.whenLoadedDeferred.promise;
} }
setLoader(loaderArg) {
this.loader = loaderArg;
}
/** /**
* loads the module * loads the module
*/ */
load() { load() {
let done = q.defer(); let done = q.defer();
let loadedModule; let loadedModule;
if (this.loader === 'npm') {
loadedModule = require(this.name);
done.resolve(loadedModule);
}
else if (this.loader === 'systemjs') {
let systemjs = require('systemjs');
systemjs.import(this.name).then((m) => { systemjs.import(this.name).then((m) => {
loadedModule = m; loadedModule = m;
this.whenLoadedDeferred.resolve(loadedModule); this.whenLoadedDeferred.resolve(loadedModule);
done.resolve(loadedModule); done.resolve(loadedModule);
}).catch(err => { console.log(err); }); }).catch(err => { console.log(err); });
}
else {
throw Error('loader not supported');
}
return done.promise; return done.promise;
} }
/** /**
@ -48,4 +66,4 @@ class LazyModule {
} }
} }
exports.LazyModule = LazyModule; exports.LazyModule = LazyModule;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsMEJBQXVCO0FBRXZCLHVCQUFzQjtBQUN0Qiw2QkFBK0I7QUFDL0IsSUFBSSxRQUFRLEdBQUcsT0FBTyxDQUFDLFVBQVUsQ0FBQyxDQUFBO0FBRWxDO0lBQUE7UUFDSSxnQkFBVyxHQUFHLElBQUksZUFBUyxFQUFtQixDQUFBO0lBUWxELENBQUM7SUFORzs7T0FFRztJQUNILGFBQWEsQ0FBQyxhQUE4QjtRQUN4QyxJQUFJLENBQUMsV0FBVyxDQUFDLEdBQUcsQ0FBQyxhQUFhLENBQUMsQ0FBQTtJQUN2QyxDQUFDO0NBQ0o7QUFFRCxrQ0FBa0M7QUFDbEMsSUFBSSxXQUFXLEdBQUcsSUFBSSxXQUFXLEVBQUUsQ0FBQTtBQUVuQzs7R0FFRztBQUNIO0lBTUksWUFBWSxPQUFlLEVBQUUsTUFBTSxHQUFXLE9BQU8sQ0FBQyxHQUFHLEVBQUU7UUFDdkQsSUFBSSxDQUFDLElBQUksR0FBRyxPQUFPLENBQUE7UUFDbkIsSUFBSSxDQUFDLEdBQUcsR0FBRyxNQUFNLENBQUE7UUFDakIsV0FBVyxDQUFDLGFBQWEsQ0FBQyxJQUFJLENBQUMsQ0FBQSxDQUFDLHFDQUFxQztRQUNyRSxJQUFJLENBQUMsVUFBVSxHQUFHLE1BQU0sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxDQUFBLENBQUMsNkJBQTZCO1FBQ3RFLElBQUksQ0FBQyxrQkFBa0IsR0FBRyxDQ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0osTUFBTSxLQUFLLENBQUMsc0JBQXNCLENBQUMsQ0FBQTtRQUN2QyxDQUFDO1FBQ0QsTUFBTSxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUE7SUFDdkIsQ0FBQztJQUVEOztPQUVHO0lBQ0gsUUFBUSxDQUFDLEdBQUcsSUFBdUI7SUFFbkMsQ0FBQztDQUNKO0FBdERELGdDQXNEQyJ9

7
npmextra.json Normal file
View File

@ -0,0 +1,7 @@
{
"npmci": {
"globalNpmTools": [
"npmts"
]
}
}

View File

@ -1,7 +1,7 @@
{ {
"name": "smartsystem", "name": "smartsystem",
"version": "1.0.2", "version": "1.0.16",
"description": "wraps systemjs for smarter workflows", "description": "simplifies lazy loading with TypeScript",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
"scripts": { "scripts": {
@ -11,6 +11,11 @@
"type": "git", "type": "git",
"url": "git+ssh://git@gitlab.com/pushrocks/smartsystem.git" "url": "git+ssh://git@gitlab.com/pushrocks/smartsystem.git"
}, },
"keywords": [
"TypeScript",
"lazy",
"module loader"
],
"author": "Lossless GmbH", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"bugs": { "bugs": {
@ -18,15 +23,13 @@
}, },
"homepage": "https://gitlab.com/pushrocks/smartsystem#README", "homepage": "https://gitlab.com/pushrocks/smartsystem#README",
"dependencies": { "dependencies": {
"@types/q": "0.0.32", "@types/q": "1.0.0",
"lik": "^1.0.24", "lik": "^1.0.30",
"q": "^1.4.1", "q": "^1.5.0",
"systemjs": "^0.19.39", "systemjs": "^0.20.12",
"typings-global": "^1.0.14" "typings-global": "^1.0.16"
}, },
"devDependencies": { "devDependencies": {
"@types/should": "^8.1.30", "tapbundle": "^1.0.12"
"should": "^11.1.1",
"typings-test": "^1.0.3"
} }
} }

View File

@ -1,4 +1,4 @@
"use strict"; "use strict";
exports.exportedTestBoolean = true; exports.exportedTestBoolean = true;
console.log('moduleExample loaded successfully'); console.log('moduleExample loaded successfully');
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kdWxlRXhhbXBsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIm1vZHVsZUV4YW1wbGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFXLFFBQUEsbUJBQW1CLEdBQUcsSUFBSSxDQUFBO0FBQ3JDLE9BQU8sQ0FBQyxHQUFHLENBQUMsbUNBQW1DLENBQUMsQ0FBQSJ9 //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibW9kdWxlRXhhbXBsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIm1vZHVsZUV4YW1wbGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFXLFFBQUEsbUJBQW1CLEdBQUcsSUFBSSxDQUFBO0FBRXJDLE9BQU8sQ0FBQyxHQUFHLENBQUMsbUNBQW1DLENBQUMsQ0FBQSJ9

View File

@ -1,2 +1,3 @@
export let exportedTestBoolean = true export let exportedTestBoolean = true
import * as q from 'q'
console.log('moduleExample loaded successfully') console.log('moduleExample loaded successfully')

1
test/test.d.ts vendored
View File

@ -1 +0,0 @@
import 'typings-test';

View File

@ -1,13 +0,0 @@
"use strict";
require("typings-test");
const smartsystem = require("../dist/index");
describe('smartsystem', function () {
it('should load a module lazily', function (done) {
let lazyModuleExample = new smartsystem.LazyModule('./test/moduleExample.js');
lazyModuleExample.load().then(m => {
console.log(m.exportedTestBoolean);
done();
});
});
});
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLHdCQUFxQjtBQUVyQiw2Q0FBNEM7QUFJNUMsUUFBUSxDQUFDLGFBQWEsRUFBRTtJQUNwQixFQUFFLENBQUMsNkJBQTZCLEVBQUUsVUFBVSxJQUFJO1FBQzVDLElBQUksaUJBQWlCLEdBQUcsSUFBSSxXQUFXLENBQUMsVUFBVSxDQUF3Qix5QkFBeUIsQ0FBQyxDQUFBO1FBRXBHLGlCQUFpQixDQUFDLElBQUksRUFBRSxDQUFDLElBQUksQ0FBQyxDQUFDO1lBQzNCLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDLG1CQUFtQixDQUFDLENBQUE7WUFDbEMsSUFBSSxFQUFFLENBQUE7UUFDVixDQUFDLENBQUMsQ0FBQTtJQUNOLENBQUMsQ0FBQyxDQUFBO0FBQ04sQ0FBQyxDQUFDLENBQUEifQ==

View File

@ -1,16 +1,13 @@
import 'typings-test' import { tap, expect } from 'tapbundle'
import * as should from 'should'
import * as smartsystem from '../dist/index' import * as smartsystem from '../dist/index'
import * as _moduleExample from './assets/moduleExample'
import * as _moduleExample from './moduleExample' tap.test('should load a module lazily', async (tools) => {
let lazyModuleExample = new smartsystem.LazyModule<typeof _moduleExample>('./assets/moduleExample.js', __dirname)
describe('smartsystem', function () { await lazyModuleExample.load().then(async m => {
it('should load a module lazily', function (done) {
let lazyModuleExample = new smartsystem.LazyModule<typeof _moduleExample>('./test/moduleExample.js')
lazyModuleExample.load().then(m => {
console.log(m.exportedTestBoolean) console.log(m.exportedTestBoolean)
done()
})
}) })
}) })
tap.start()

View File

@ -1,8 +1,8 @@
import 'typings-global' import 'typings-global'
import * as path from 'path'
import * as q from 'q' import * as q from 'q'
import { Objectmap } from 'lik' import { Objectmap } from 'lik'
let systemjs = require('systemjs')
class Smartsystem { class Smartsystem {
lazyModules = new Objectmap<LazyModule<any>>() lazyModules = new Objectmap<LazyModule<any>>()
@ -21,32 +21,54 @@ let smartsystem = new Smartsystem()
/** /**
* defines a LazyModule * defines a LazyModule
*/ */
export type TLoader = 'npm' | 'systemjs'
export class LazyModule<T> { export class LazyModule<T> {
name: string name: string
nameIsPath: boolean nameIsPath: boolean
cwd: string cwd: string
whenLoaded: q.Promise<T> whenLoaded: q.Promise<T>
loader: TLoader = 'npm'
private whenLoadedDeferred: q.Deferred<T> private whenLoadedDeferred: q.Deferred<T>
constructor(nameArg: string, cwdArg: string = process.cwd()) { constructor(nameArg: string, cwdArg: string) {
if (!cwdArg) {
throw new Error('You must specify a directory to resolve from!')
}
this.name = nameArg this.name = nameArg
this.cwd = cwdArg this.cwd = cwdArg
smartsystem.addLazyModule(this) // add module to smartsystem instance smartsystem.addLazyModule(this) // add module to smartsystem instance
this.nameIsPath = /\.\//.test(this.name) // figure out if name is path this.nameIsPath = /\.\//.test(this.name) // figure out if name is path
if (this.nameIsPath) {
this.name = path.join(this.cwd, this.name)
}
this.whenLoadedDeferred = q.defer<T>() this.whenLoadedDeferred = q.defer<T>()
this.whenLoaded = this.whenLoadedDeferred.promise this.whenLoaded = this.whenLoadedDeferred.promise
} }
setLoader(loaderArg: TLoader) {
this.loader = loaderArg
}
/** /**
* loads the module * loads the module
*/ */
load(): q.Promise<T> { load(): q.Promise<T> {
let done = q.defer<T>() let done = q.defer<T>()
let loadedModule: T let loadedModule: T
if (this.loader === 'npm') {
loadedModule = require(this.name)
done.resolve(loadedModule)
} else if (this.loader === 'systemjs') {
let systemjs = require('systemjs')
systemjs.import(this.name).then((m) => { systemjs.import(this.name).then((m) => {
loadedModule = m loadedModule = m
this.whenLoadedDeferred.resolve(loadedModule) this.whenLoadedDeferred.resolve(loadedModule)
done.resolve(loadedModule) done.resolve(loadedModule)
}).catch(err => { console.log(err) }) }).catch(err => { console.log(err) })
} else {
throw Error('loader not supported')
}
return done.promise return done.promise
} }

305
yarn.lock Normal file
View File

@ -0,0 +1,305 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/chai-as-promised@0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/chai-as-promised/-/chai-as-promised-0.0.29.tgz#43d52892aa998e185a3de3e2477edb8573be1d77"
dependencies:
"@types/chai" "*"
"@types/promises-a-plus" "*"
"@types/chai-string@^1.1.30":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@types/chai-string/-/chai-string-1.1.30.tgz#4d8744b31a5a2295fc01c981ed1e2d4c8a070f0a"
dependencies:
"@types/chai" "*"
"@types/chai@*", "@types/chai@^3.4.35":
version "3.5.2"
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-3.5.2.tgz#c11cd2817d3a401b7ba0f5a420f35c56139b1c1e"
"@types/lodash@^4.14.62":
version "4.14.64"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.64.tgz#979cf3a3d4a368670840bf9b3e448dc33ffe84ee"
"@types/minimatch@2.x.x":
version "2.0.29"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-2.0.29.tgz#5002e14f75e2d71e564281df0431c8c1b4a2a36a"
"@types/promises-a-plus@*":
version "0.0.27"
resolved "https://registry.yarnpkg.com/@types/promises-a-plus/-/promises-a-plus-0.0.27.tgz#c64651134614c84b8f5d7114ce8901d36a609780"
"@types/q@1.0.0", "@types/q@1.x.x":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.0.0.tgz#57e5465d665b370d4217e69b344b20faa6b724f5"
ansi-256-colors@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/ansi-256-colors/-/ansi-256-colors-1.1.0.tgz#910de50efcc7c09e3d82f2f87abd6b700c18818a"
assertion-error@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/assertion-error/-/assertion-error-1.0.2.tgz#13ca515d86206da0bac66e834dd397d87581094c"
balanced-match@^0.4.1:
version "0.4.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-0.4.2.tgz#cb3f3e3c732dc0f01ee70b403f302e61d7709838"
beautycolor@^1.0.7:
version "1.0.7"
resolved "https://registry.yarnpkg.com/beautycolor/-/beautycolor-1.0.7.tgz#a4715738ac4c8221371e9cbeb5a6cc6d11ecbf7c"
dependencies:
ansi-256-colors "^1.1.0"
typings-global "^1.0.14"
bindings@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.2.1.tgz#14ad6113812d2d37d72e67b4cacb4bb726505f11"
brace-expansion@^1.1.7:
version "1.1.7"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.7.tgz#3effc3c50e000531fb720eaff80f0ae8ef23cf59"
dependencies:
balanced-match "^0.4.1"
concat-map "0.0.1"
chai-as-promised@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/chai-as-promised/-/chai-as-promised-6.0.0.tgz#1a02a433a6f24dafac63b9c96fa1684db1aa8da6"
dependencies:
check-error "^1.0.2"
chai-string@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/chai-string/-/chai-string-1.3.0.tgz#df6139f294391b1035be5606f60a843b3a5041e7"
chai@^3.5.0:
version "3.5.0"
resolved "https://registry.yarnpkg.com/chai/-/chai-3.5.0.tgz#4d02637b067fe958bdbfdd3a40ec56fef7373247"
dependencies:
assertion-error "^1.0.1"
deep-eql "^0.1.3"
type-detect "^1.0.0"
check-error@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
deep-eql@^0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-0.1.3.tgz#ef558acab8de25206cd713906d74e56930eb69f2"
dependencies:
type-detect "0.1.1"
early@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/early/-/early-2.1.1.tgz#841e23254ea5dc54d8afaeee82f5ab65c00ee23c"
dependencies:
beautycolor "^1.0.7"
smartq "^1.1.1"
typings-global "^1.0.16"
fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
glob@^7.0.0:
version "7.1.1"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.2"
once "^1.3.0"
path-is-absolute "^1.0.0"
inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
dependencies:
once "^1.3.0"
wrappy "1"
inherits@2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
interpret@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.0.3.tgz#cbc35c62eeee73f19ab7b10a801511401afc0f90"
leakage@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/leakage/-/leakage-0.2.0.tgz#9e7a8cc1d241d8c8427e348769e192e172fd8733"
dependencies:
left-pad "^1.1.3"
memwatch-next "^0.3.0"
minimist "^1.2.0"
pretty-bytes "^4.0.2"
left-pad@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.3.tgz#612f61c033f3a9e08e939f1caebeea41b6f3199a"
lik@^1.0.30:
version "1.0.30"
resolved "https://registry.yarnpkg.com/lik/-/lik-1.0.30.tgz#488485088fc0dca9d08ba9744796d1dbf6b1eca4"
dependencies:
"@types/lodash" "^4.14.62"
"@types/minimatch" "2.x.x"
"@types/q" "1.x.x"
lodash "^4.17.4"
minimatch "^3.0.3"
q "^1.5.0"
rxjs "^5.3.0"
typings-global "^1.0.14"
lodash@^4.17.4:
version "4.17.4"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae"
memwatch-next@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/memwatch-next/-/memwatch-next-0.3.0.tgz#2111050f9a906e0aa2d72a4ec0f0089c78726f8f"
dependencies:
bindings "^1.2.1"
nan "^2.3.2"
minimatch@^3.0.2, minimatch@^3.0.3:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
brace-expansion "^1.1.7"
minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
nan@^2.3.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.6.2.tgz#e4ff34e6c95fdfb5aecc08de6596f43605a7db45"
once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
dependencies:
wrappy "1"
path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
path-parse@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"
pretty-bytes@^4.0.2:
version "4.0.2"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-4.0.2.tgz#b2bf82e7350d65c6c33aa95aaa5a4f6327f61cd9"
q@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.0.tgz#dd01bac9d06d30e6f219aecb8253ee9ebdc308f1"
rechoir@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
dependencies:
resolve "^1.1.6"
resolve@^1.1.6:
version "1.3.3"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.3.3.tgz#655907c3469a8680dc2de3a275a8fdd69691f0e5"
dependencies:
path-parse "^1.0.5"
rxjs@^5.3.0:
version "5.4.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-5.4.0.tgz#a7db14ab157f9d7aac6a56e655e7a3860d39bf26"
dependencies:
symbol-observable "^1.0.1"
semver@^5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
shelljs@^0.7.7:
version "0.7.7"
resolved "https://registry.yarnpkg.com/shelljs/-/shelljs-0.7.7.tgz#b2f5c77ef97148f4b4f6e22682e10bba8667cff1"
dependencies:
glob "^7.0.0"
interpret "^1.0.0"
rechoir "^0.6.2"
smartchai@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/smartchai/-/smartchai-1.0.3.tgz#de6d010bb8b5aef24cb70b31a5f5334e8c41b72f"
dependencies:
"@types/chai" "^3.4.35"
"@types/chai-as-promised" "0.0.29"
"@types/chai-string" "^1.1.30"
chai "^3.5.0"
chai-as-promised "^6.0.0"
chai-string "^1.3.0"
smartdelay@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/smartdelay/-/smartdelay-1.0.1.tgz#687f8bcc09d7c62c9c5a8a1771c1aba3aff54156"
dependencies:
typings-global "^1.0.14"
smartq@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/smartq/-/smartq-1.1.1.tgz#efb358705260d41ae18aef7ffd815f7b6fe17dd3"
dependencies:
typed-promisify "^0.3.0"
typings-global "^1.0.14"
symbol-observable@^1.0.1:
version "1.0.4"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.4.tgz#29bf615d4aa7121bdd898b22d4b3f9bc4e2aa03d"
systemjs@^0.20.12:
version "0.20.12"
resolved "https://registry.yarnpkg.com/systemjs/-/systemjs-0.20.12.tgz#135cc471280448453347829ebb9639a09a67f101"
tapbundle@^1.0.12:
version "1.0.12"
resolved "https://registry.yarnpkg.com/tapbundle/-/tapbundle-1.0.12.tgz#71d29273aad280f5c4e15b2700430b2456a5d364"
dependencies:
early "^2.1.1"
leakage "^0.2.0"
smartchai "^1.0.3"
smartdelay "^1.0.1"
smartq "^1.1.1"
typings-global "^1.0.16"
type-detect@0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-0.1.1.tgz#0ba5ec2a885640e470ea4e8505971900dac58822"
type-detect@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-1.0.0.tgz#762217cc06db258ec48908a1298e8b95121e8ea2"
typed-promisify@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/typed-promisify/-/typed-promisify-0.3.0.tgz#1ba0af5e444c87d8047406f18ce49092a1191853"
typings-global@^1.0.14, typings-global@^1.0.16:
version "1.0.16"
resolved "https://registry.yarnpkg.com/typings-global/-/typings-global-1.0.16.tgz#489b71781af24268750c2899316400a5e482961f"
dependencies:
semver "^5.3.0"
shelljs "^0.7.7"
wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"