wn-ts-node
Version:
Wordnet interface library - TypeScript port
168 lines (167 loc) • 4.85 kB
JavaScript
import { dirname as w, join as a } from "path";
import { homedir as u } from "os";
import { existsSync as h, readFileSync as p, mkdirSync as g, statSync as y } from "fs";
import m from "smol-toml";
import { C as j, a as l, P as d } from "../legacy-CiH5BgHM.js";
import "kysely";
import { l as f } from "../logger-CpzBOsQ7.js";
import { fileURLToPath as _ } from "url";
const D = w(_(import.meta.url));
class $ extends j {
_dataDirectory;
_projects;
_allowMultithreading;
constructor() {
super();
const t = u();
this._dataDirectory = a(t, ".wn_ts_data"), this._projects = {}, this._allowMultithreading = !1, this.loadIndex(a(D, "index.toml"));
}
get dataDirectory() {
return this.ensureDirectory(this._dataDirectory), this._dataDirectory;
}
set dataDirectory(t) {
const r = t.startsWith("~") ? a(u(), t.slice(1)) : t;
if (f.config(`Setting dataDirectory to: ${r}`), f.config(`Path exists: ${h(r)}`), h(r) && f.config(`Is directory: ${this.isDirectory(r)}`), h(r) && !this.isDirectory(r))
throw new l(`Path exists and is not a directory: ${r}`);
this._dataDirectory = r, f.success(`dataDirectory set successfully to: ${this._dataDirectory}`);
}
get databasePath() {
return a(this.dataDirectory, "wn.db");
}
get downloadDirectory() {
const t = a(this.dataDirectory, "downloads");
return this.ensureDirectory(t), t;
}
get allowMultithreading() {
return this._allowMultithreading;
}
set allowMultithreading(t) {
this._allowMultithreading = t;
}
get index() {
return { ...this._projects };
}
getCachePath(t) {
const o = require("crypto").createHash("md5").update(t).digest("hex");
return a(this.downloadDirectory, o);
}
addProject(t, r = "wordnet", o, s, i, e) {
if (t in this._projects)
throw new Error(`Project already added: ${t}`);
this._projects[t] = {
type: r,
label: o,
language: s,
versions: {},
license: i
}, e && (this._projects[t].error = e);
}
addProjectVersion(t, r, o, s, i) {
const e = {};
if (o && !s)
e.resource_urls = o.split(/\s+/).filter(Boolean);
else if (s && !o)
e.error = s;
else if (o && s)
throw new l(`${t}:${r} specifies both url and error`);
i && (e.license = i);
const n = this._projects[t];
if (!n)
throw new Error(`Project not found: ${t}`);
n.versions[r] = e;
}
getProjectInfo(t) {
const [r, o] = this.splitLexiconSpecifier(t);
if (!(r in this._projects))
throw new d(`No such project id: ${r}`);
const s = this._projects[r];
if (s.error)
throw new d(s.error);
const i = s.versions;
let e = o;
if ((!e || e === "*") && (e = Object.keys(i)[0] || ""), e) {
if (!(e in i))
throw new d(`No such version: '${e}' (${r})`);
} else throw new d(`No versions available for ${r}`);
const n = i[e];
if (n.error)
throw new d(n.error);
const c = n.resource_urls || [];
return {
id: r,
version: e,
type: s.type || "wordnet",
label: s.label || "",
language: s.language || "",
license: n.license || s.license || "",
resource_urls: c,
cache: void 0
};
}
loadIndex(t) {
try {
const r = p(t, "utf-8"), o = m.parse(r);
this.update({ index: o });
} catch (r) {
throw r && (r.name === "TomlError" || r.constructor?.name === "TomlError") ? new l("malformed index file") : r;
}
}
ensureDirectory(t) {
if (!h(t))
try {
g(t, { recursive: !0 });
} catch (r) {
throw new l(
`Failed to create directory ${t}: ${r instanceof Error ? r.message : String(r)}`
);
}
}
isDirectory(t) {
try {
return y(t).isDirectory();
} catch {
return !1;
}
}
splitLexiconSpecifier(t) {
const r = t.split(":");
return r.length === 1 ? [r[0], ""] : [r[0], r.slice(1).join(":")];
}
update(t) {
const r = t.index || {};
for (const [o, s] of Object.entries(r)) {
const i = s;
if (o in this._projects) {
const e = this._projects[o];
for (const n of ["label", "language", "license"])
if (n in i && i[n] !== e[n])
throw new l(`${n} mismatch for ${o}`);
} else
this.addProject(
o,
i.type || "wordnet",
i.label,
i.language,
i.license,
i.error
);
for (const [e, n] of Object.entries(i.versions || {})) {
const c = n;
if ("url" in c && "error" in i)
throw new l(`${o}:${e} url specified with default error`);
this.addProjectVersion(
o,
e,
c.url,
c.error,
c.license
);
}
}
}
}
const L = new $();
export {
$ as ConfigManager,
L as config
};