scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
60 lines (50 loc) • 2.22 kB
text/typescript
import * as ScriptableAbstracts from 'scriptable-abstract';
import {Scriptable, ScriptableClass, ScriptableVariable} from 'scriptable-abstract';
import {Constructor} from '../types';
import * as MockImplementations from '.';
type ScriptableConstructor = typeof ScriptableClass<object> & {
identifier: string;
type: 'class' | 'variable';
states: unknown;
classInitializers: unknown;
};
/**
* Check if a value is a valid Scriptable implementation
* @param implementation - The value to check
* @returns True if the value is a valid Scriptable implementation
*/
const isScriptableImplementation = (implementation: unknown): implementation is ScriptableConstructor => {
return (
typeof implementation === 'function' &&
implementation.prototype instanceof Scriptable &&
'identifier' in implementation &&
'type' in implementation &&
'states' in implementation &&
'classInitializers' in implementation
);
};
// Default abstract implementations
const ABS_IMPLEMENTATIONS = Object.keys(ScriptableAbstracts)
.filter(key => key.startsWith('Abs'))
.map(key => ScriptableAbstracts[key as keyof typeof ScriptableAbstracts]) as Array<typeof Scriptable<any>>;
// Get all mock implementations that extend Scriptable
const MOCK_IMPLEMENTATIONS = Object.values(MockImplementations).filter(isScriptableImplementation) as Array<
typeof ScriptableClass<object> & Constructor
>;
export type ScriptableImplementation = (typeof ABS_IMPLEMENTATIONS)[number] | (typeof MOCK_IMPLEMENTATIONS)[number];
export const SCRIPTABLE_IMPLEMENTATIONS: Record<string, ScriptableImplementation> = [
...ABS_IMPLEMENTATIONS,
...MOCK_IMPLEMENTATIONS,
].reduce(
(acc, implementation) => {
acc[implementation.identifier] = implementation;
return acc;
},
{} as Record<string, ScriptableImplementation>,
);
export const GLOBAL_CLASSES = Object.values(SCRIPTABLE_IMPLEMENTATIONS).filter(
implementation => implementation.type === 'class',
) as Array<typeof ScriptableClass<object> & Constructor>;
export const GLOBAL_VARIABLES = Object.values(SCRIPTABLE_IMPLEMENTATIONS).filter(
implementation => implementation.type === 'variable',
) as Array<typeof ScriptableVariable<object> & Constructor>;