57 lines
1.0 KiB
JavaScript
57 lines
1.0 KiB
JavaScript
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
import {Resource} from './resource';
|
|
|
|
export class ResourceRegistry {
|
|
|
|
constructor() {
|
|
this.idToUriMap = {};
|
|
this.resources = {};
|
|
this.uriToOtherMap = {};
|
|
this.id = 1;
|
|
}
|
|
|
|
load(uri) {
|
|
return Resource.read(uri).then((json) => {
|
|
for (const uri in json) {
|
|
this.register(uri, json.uuid, json.id);
|
|
}
|
|
});
|
|
}
|
|
|
|
flush() {
|
|
const RESOURCE_PATH = path.resolve(process.cwd(), 'resource');
|
|
fs.writeFileSync(
|
|
path.join(RESOURCE_PATH, 'registry.json'),
|
|
JSON.stringify(this.uriToOtherMap)
|
|
);
|
|
}
|
|
|
|
idToUri(id) {
|
|
return this.idToUriMap[id];
|
|
}
|
|
|
|
register(uri, uuid, id) {
|
|
id = id || this.id++;
|
|
this.idToUriMap[id] = uri;
|
|
this.uriToOtherMap[uri] = {
|
|
id,
|
|
uuid,
|
|
};
|
|
}
|
|
|
|
uriToId(uri) {
|
|
const entry = this.uriToOtherMap[uri];
|
|
return entry && entry.id;
|
|
}
|
|
|
|
uriToUuid(uri) {
|
|
const entry = this.uriToOtherMap[uri];
|
|
return entry && entry.uuid;
|
|
}
|
|
|
|
}
|
|
|
|
export const globalRegistry = new ResourceRegistry();
|