2017-11-16 15:17:15 +03:00
|
|
|
import * as pathlib from 'path';
|
|
|
|
import * as resolve from 'resolve';
|
2017-11-09 12:16:48 +03:00
|
|
|
|
2017-11-18 12:38:34 +03:00
|
|
|
import type Scope from './scope';
|
2017-11-28 16:35:28 +03:00
|
|
|
import type {Type, TypeId} from './types';
|
2017-11-18 12:38:34 +03:00
|
|
|
import type {Query} from './query';
|
|
|
|
|
2017-11-16 15:17:15 +03:00
|
|
|
export default class Module {
|
2017-11-28 16:35:28 +03:00
|
|
|
+id: TypeId;
|
2017-11-18 12:38:34 +03:00
|
|
|
+path: string;
|
|
|
|
_scopeCount: number;
|
|
|
|
_exports: Map<?string, [Scope, string]>;
|
|
|
|
|
2017-11-28 16:35:28 +03:00
|
|
|
constructor(id: TypeId, path: string) {
|
|
|
|
this.id = id;
|
2017-11-09 12:16:48 +03:00
|
|
|
this.path = path;
|
2017-11-18 12:38:34 +03:00
|
|
|
this._scopeCount = 0;
|
2017-11-09 12:16:48 +03:00
|
|
|
this._exports = new Map;
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:35:28 +03:00
|
|
|
generateScopeId(): TypeId {
|
|
|
|
if (this._scopeCount === 0) {
|
|
|
|
++this._scopeCount;
|
|
|
|
return this.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.id.concat(String(this._scopeCount++));
|
2017-11-09 12:16:48 +03:00
|
|
|
}
|
|
|
|
|
2017-11-18 12:38:34 +03:00
|
|
|
addExport(name: ?string, scope: Scope, reference: string) {
|
2017-11-09 12:16:48 +03:00
|
|
|
this._exports.set(name, [scope, reference]);
|
|
|
|
}
|
|
|
|
|
2017-11-21 15:28:59 +03:00
|
|
|
query(name: ?string, params: (?Type)[]): Query {
|
2017-11-09 12:16:48 +03:00
|
|
|
const result = this._exports.get(name);
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return {
|
2017-11-28 16:35:28 +03:00
|
|
|
kind: 'unknown',
|
2017-11-09 12:16:48 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const [scope, reference] = result;
|
|
|
|
|
2017-11-13 21:11:18 +03:00
|
|
|
return scope.query(reference, params);
|
2017-11-09 12:16:48 +03:00
|
|
|
}
|
|
|
|
|
2017-11-18 12:38:34 +03:00
|
|
|
resolve(path: string): string {
|
2017-11-09 12:16:48 +03:00
|
|
|
const basedir = pathlib.dirname(this.path);
|
|
|
|
|
|
|
|
// TODO: follow symlinks.
|
|
|
|
return resolve.sync(path, {basedir});
|
|
|
|
}
|
|
|
|
|
2017-11-18 12:38:34 +03:00
|
|
|
exports(): Iterator<[Scope, string]> {
|
2017-11-09 12:16:48 +03:00
|
|
|
return this._exports.values();
|
|
|
|
}
|
|
|
|
}
|