You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.8 KiB
82 lines
2.8 KiB
import LifecycleUnit from '../lifecycle/Unit.ts'
|
|
import {fs, path} from '../external/std.ts'
|
|
import {Canon} from './Canon.ts'
|
|
import {Logging} from '../service/logging/Logging.ts'
|
|
|
|
export interface CanonicalDefinition {
|
|
canonical_name: string,
|
|
original_name: string,
|
|
imported: any,
|
|
}
|
|
|
|
export interface CanonicalReference {
|
|
resource?: string,
|
|
item: string,
|
|
particular?: string,
|
|
}
|
|
|
|
export class Canonical<T> extends LifecycleUnit {
|
|
protected base_path: string = '.'
|
|
protected suffix: string = '.ts'
|
|
protected canonical_item: string = ''
|
|
protected _items: { [key: string]: T } = {}
|
|
|
|
public static resolve(reference: string): CanonicalReference {
|
|
const rsc_parts = reference.split('::')
|
|
const resource = rsc_parts.length > 1 ? rsc_parts[0] + 's' : undefined
|
|
const rsc_less = rsc_parts.length > 1 ? rsc_parts[1] : rsc_parts[0]
|
|
const prt_parts = rsc_less.split('.')
|
|
const item = prt_parts[0]
|
|
const particular = prt_parts.length > 1 ? prt_parts.slice(1).join('.') : undefined
|
|
|
|
return {
|
|
resource, item, particular
|
|
}
|
|
}
|
|
|
|
public all(): string[] {
|
|
return Object.keys(this._items)
|
|
}
|
|
|
|
public get path(): string {
|
|
return path.resolve(this.base_path)
|
|
}
|
|
|
|
public get canonical_items() {
|
|
return `${this.canonical_item}s`
|
|
}
|
|
|
|
public async up() {
|
|
const logger = this.make(Logging)
|
|
for await ( const entry of fs.walk(this.path) ) {
|
|
if ( !entry.isFile || !entry.path.endsWith(this.suffix) ) {
|
|
if ( entry.isFile ) logger.debug(`Skipping file in canonical path with invalid suffix: ${entry.path}`)
|
|
continue
|
|
}
|
|
const def = await this._get_canonical_definition(entry.path)
|
|
logger.verbose(`Registering canonical ${this.canonical_item} "${def.canonical_name}" from ${entry.path}.`)
|
|
this._items[def.canonical_name] = await this.init_canonical_item(def)
|
|
}
|
|
this.make(Canon).register_canonical(this)
|
|
}
|
|
|
|
public async init_canonical_item(definition: CanonicalDefinition): Promise<T> {
|
|
return definition.imported.default
|
|
}
|
|
|
|
private async _get_canonical_definition(file_path: string): Promise<CanonicalDefinition> {
|
|
const original_name = file_path.replace(this.path, '').substr(1)
|
|
const path_regex = new RegExp(path.SEP, 'g')
|
|
const canonical_name = original_name.replace(path_regex, ':')
|
|
.split('').reverse().join('')
|
|
.substr(this.suffix.length)
|
|
.split('').reverse().join('')
|
|
const imported = await import(file_path)
|
|
return { canonical_name, original_name, imported }
|
|
}
|
|
|
|
public get(key: string): T | undefined {
|
|
return this._items[key]
|
|
}
|
|
}
|