aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ScriptingGlobals.ts
blob: 444e8fc0a13f2d85e2e9a9e54ba1442e78fe83f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import ts from 'typescript';

export { ts };

const _scriptingGlobals: { [name: string]: unknown } = {};
const _scriptingDescriptions: { [name: string]: string } = {};
const _scriptingParams: { [name: string]: string } = {};
export let scriptingGlobals: { [name: string]: unknown } = _scriptingGlobals;

export namespace ScriptingGlobals {
    export function getGlobals()      { return Object.keys(_scriptingGlobals); } // prettier-ignore
    export function getGlobalObj()    { return _scriptingGlobals; } // prettier-ignore
    export function getDescriptions() { return _scriptingDescriptions; } // prettier-ignore
    export function getParameters()   { return _scriptingParams; } // prettier-ignore

    export function add(name: string, namespace_func_or_object: unknown): void;
    export function add(func: { name: string }, description?: string, params?: string): void;
    export function add(first: string | { name: string }, second?: unknown, params?: string): void {
        let n: string = '';
        let obj: unknown;

        if (second !== undefined) {
            if (typeof first === 'string') {
                n = first;
                obj = second;
            } else {
                obj = first;
                n = first.name;
                _scriptingDescriptions[n] = second as string;
                if (params !== undefined) {
                    _scriptingParams[n] = params;
                }
            }
        } else if (first instanceof Object && 'name' in first && typeof first.name === 'string') {
            n = first.name;
            obj = first;
        } else {
            throw new Error('Must either register an object with a name, or give a name and an object');
        }
        if (n === undefined || n === 'undefined') {
            return; // false;
        }
        // eslint-disable-next-line no-prototype-builtins
        if (_scriptingGlobals.hasOwnProperty(n)) {
            throw new Error(`Global with name ${n} is already registered, choose another name`);
        }
        _scriptingGlobals[n] = obj;
        return; // true;
    }
    export function makeMutableGlobalsCopy(globals?: { [name: string]: unknown }) {
        return { ..._scriptingGlobals, ...(globals || {}) };
    }

    export function setScriptingGlobals(globals: { [key: string]: unknown }) {
        scriptingGlobals = globals;
    }

    export function removeGlobal(name: string) {
        if (getGlobals().includes(name)) {
            delete _scriptingGlobals[name];
            if (_scriptingDescriptions[name]) {
                delete _scriptingDescriptions[name];
            }
            if (_scriptingParams[name]) {
                delete _scriptingParams[name];
            }
            return true;
        }
        return false;
    }

    export function resetScriptingGlobals() {
        scriptingGlobals = _scriptingGlobals;
    }

    // const types = Object.keys(ts.SyntaxKind).map(kind => ts.SyntaxKind[kind]);
    export function printNodeType(node: ts.Node, indentation = '') {
        console.log(indentation + ts.SyntaxKind[node.kind]);
    }
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function scriptingGlobal(constructor: { new (...args: any[]): unknown }) {
    ScriptingGlobals.add(constructor);
}