smartsystem/dist/index.js

65 lines
4.3 KiB
JavaScript
Raw Normal View History

2016-10-11 23:41:30 +00:00
"use strict";
2016-10-12 12:07:54 +00:00
require("typings-global");
const path = require("path");
2016-10-11 23:41:30 +00:00
const q = require("q");
const lik_1 = require("lik");
class Smartsystem {
constructor() {
this.lazyModules = new lik_1.Objectmap();
}
/**
* add lazyModule to Smartsystem
*/
addLazyModule(lazyModuleArg) {
this.lazyModules.add(lazyModuleArg);
}
}
// create the internal smartsystem
let smartsystem = new Smartsystem();
class LazyModule {
constructor(nameArg, cwdArg = process.cwd()) {
2016-10-14 01:07:37 +00:00
this.loader = 'npm';
2016-10-11 23:41:30 +00:00
this.name = nameArg;
this.cwd = cwdArg;
2016-10-12 12:01:15 +00:00
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);
}
2016-10-12 12:01:15 +00:00
this.whenLoadedDeferred = q.defer();
this.whenLoaded = this.whenLoadedDeferred.promise;
2016-10-11 23:41:30 +00:00
}
2016-10-14 01:07:37 +00:00
setLoader(loaderArg) {
this.loader = loaderArg;
}
2016-10-11 23:41:30 +00:00
/**
* loads the module
*/
load() {
let done = q.defer();
let loadedModule;
2016-10-14 01:07:37 +00:00
if (this.loader === 'npm') {
2016-10-14 01:24:29 +00:00
loadedModule = require(this.name);
done.resolve(loadedModule);
}
else if (this.loader === 'systemjs') {
let systemjs = require('systemjs');
systemjs.import(this.name).then((m) => {
loadedModule = m;
this.whenLoadedDeferred.resolve(loadedModule);
done.resolve(loadedModule);
}).catch(err => { console.log(err); });
2016-10-14 01:07:37 +00:00
}
else {
2016-10-14 01:24:29 +00:00
throw Error('loader not supported');
2016-10-14 01:07:37 +00:00
}
2016-10-11 23:41:30 +00:00
return done.promise;
}
/**
* loads additional lazy modules specified as arguments and returns them in the promise for easy use
*/
loadAlso(...args) {
}
}
exports.LazyModule = LazyModule;
2016-10-14 01:24:29 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