scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
192 lines • 4.54 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { GlobalRegistry } from "./registry";
const DEFAULT_PREFERENCES = {
device: {
name: "iPhone Mock",
systemName: "iOS",
systemVersion: "16.0",
model: "iPhone",
screenSize: {
width: 390,
height: 844
},
screenResolution: {
width: 1170,
height: 2532
},
screenScale: 3,
screenBrightness: 1,
orientation: {
isInPortrait: true,
isInPortraitUpsideDown: false,
isInLandscapeLeft: false,
isInLandscapeRight: false,
isFaceUp: false,
isFaceDown: false
},
battery: {
level: 1,
isDischarging: false,
isCharging: false,
isFullyCharged: true
},
locale: {
preferredLanguages: ["en"],
locale: "en_US",
language: "en"
},
appearance: {
isUsingDarkAppearance: false
},
volume: 0.5
},
config: {
widgetFamily: "small",
runsInWidget: false,
runsInApp: true,
runsWithSiri: false,
runsInActionExtension: false,
runsInNotification: false,
runsFromHomeScreen: false,
runsInAccessoryWidget: false
},
script: {
name: "Test Script"
},
args: {
plainTexts: [],
fileURLs: [],
images: [],
queryParameters: {}
},
location: {
currentLocation: {
latitude: 0,
longitude: 0,
altitude: 0,
horizontalAccuracy: 5,
verticalAccuracy: 5
},
accuracy: "best",
simulatedDelay: 100
}
};
const _Preferences = class _Preferences {
constructor() {
__publicField(this, "current");
this.current = { ...DEFAULT_PREFERENCES };
}
static getInstance() {
if (!_Preferences.instance) {
_Preferences.instance = new _Preferences();
}
return _Preferences.instance;
}
/**
* Get current preferences
*/
get() {
return this.current;
}
/**
* Update preferences
* @param preferences Partial preferences to merge
*/
update(preferences) {
this.current = this.merge(this.current, preferences);
this.syncMocks();
}
/**
* Reset preferences to defaults
*/
reset() {
this.current = { ...DEFAULT_PREFERENCES };
this.syncMocks();
}
/**
* Load preferences from JSON string
*/
loadFromJSON(json) {
try {
const preferences = JSON.parse(json);
this.current = this.merge(DEFAULT_PREFERENCES, preferences);
this.syncMocks();
} catch (error) {
throw new Error(`Failed to parse preferences: ${error}`);
}
}
/**
* Save preferences to JSON string
*/
saveToJSON() {
return JSON.stringify(this.current, null, 2);
}
/**
* Get preferences for specific mock
*/
getMock(mockName) {
return this.current[mockName];
}
/**
* Update preferences for specific mock
*/
updateMock(mockName, preferences) {
this.current = {
...this.current,
[mockName]: {
...this.current[mockName],
...preferences
}
};
this.syncMocks();
}
/**
* Synchronize current preferences with all mocks
*/
syncMocks() {
if (GlobalRegistry.has("Device")) {
GlobalRegistry.get("Device").setState(this.current.device);
}
if (GlobalRegistry.has("config")) {
GlobalRegistry.get("config").setState(this.current.config);
}
if (GlobalRegistry.has("Script")) {
GlobalRegistry.get("Script").setState(this.current.script);
}
if (GlobalRegistry.has("args")) {
GlobalRegistry.get("args").setState(this.current.args);
}
if (GlobalRegistry.has("Location")) {
GlobalRegistry.get("Location").setState(this.current.location);
}
}
/**
* Deep merge preferences
*/
merge(target, source) {
const result = { ...target };
for (const key in source) {
if (source.hasOwnProperty(key)) {
const value = source[key];
if (value && typeof value === "object" && !Array.isArray(value)) {
result[key] = {
...result[key],
...value
};
} else {
result[key] = value;
}
}
}
return result;
}
};
__publicField(_Preferences, "instance");
let Preferences = _Preferences;
export {
DEFAULT_PREFERENCES,
Preferences
};
//# sourceMappingURL=preferences.js.map