Anki/ts/lib/register-package.ts
Henrik Giesel 6e0f7c3d53 Implement register-package and export some packages
Exported packages are:
* anki/packages
* anki/shortcuts
* anki/bridgecommand
2021-08-30 14:41:40 +02:00

52 lines
1.2 KiB
TypeScript

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { runtimeLibraries } from "./runtime-require";
export function registerPackage(
name: string,
entries: Record<string, unknown>,
deprecation?: Record<string, string>
): void {
const pack = deprecation
? new Proxy(entries, {
get: (target, name: string) => {
if (name in deprecation) {
console.log(`anki: ${name} is deprecated: ${deprecation[name]}`);
}
return target[name];
},
})
: entries;
runtimeLibraries[name] = pack;
}
function listPackages(): string[] {
return Object.keys(runtimeLibraries);
}
function hasPackages(...names: string[]): boolean {
const libraries = listPackages();
return names.reduce(
(accu: boolean, name: string) => accu && libraries.includes(name),
true
);
}
function immediatelyDeprecated() {
return false;
}
registerPackage(
"anki/packages",
{
listPackages,
hasPackages,
immediatelyDeprecated,
},
{
[immediatelyDeprecated.name]: "Do not use this function",
}
);