import 'typings-global' import * as path from 'path' import * as smartq from 'smartq' import { Objectmap } from 'lik' class Smartsystem { lazyModules = new Objectmap>() /** * add lazyModule to Smartsystem */ addLazyModule (lazyModuleArg: LazyModule) { this.lazyModules.add(lazyModuleArg) } } // create the internal smartsystem let smartsystem = new Smartsystem() /** * defines a LazyModule */ export type TLoader = 'npm' | 'systemjs' export class LazyModule { name: string cwd: string whenLoaded: Promise private nameIsPath: boolean private whenLoadedDeferred: smartq.Deferred constructor (nameArg: string, cwdArg: string) { if (!cwdArg) { throw new Error('You must specify a directory to resolve from!') } this.name = nameArg this.cwd = cwdArg smartsystem.addLazyModule(this) // add module to smartsystem instance this.nameIsPath = /\.\//.test(this.name) // figure out if name is path if (this.nameIsPath) { this.name = path.join(this.cwd, this.name) } this.whenLoadedDeferred = smartq.defer() this.whenLoaded = this.whenLoadedDeferred.promise } /** * loads the module */ async load (): Promise { let loadedModule: T = require(this.name) this.whenLoadedDeferred.resolve(loadedModule) return loadedModule } }