scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
144 lines • 4.33 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var document_picker_exports = {};
__export(document_picker_exports, {
MockDocumentPicker: () => MockDocumentPicker
});
module.exports = __toCommonJS(document_picker_exports);
var import_scriptable_abstract = require("scriptable-abstract");
var import_filemanager = require("./filemanager");
const _DEFAULT_STATE = {
selectedFiles: [],
exportedFiles: [],
lastExportedFile: null
};
class MockDocumentPicker extends import_scriptable_abstract.AbsDocumentPicker {
constructor() {
super({
selectedFiles: [],
exportedFiles: [],
lastExportedFile: null
});
__publicField(this, "fileManager", import_filemanager.MockFileManager.local());
}
static get instance() {
return super.instance;
}
/**
* @inheritdoc
*/
async open() {
return this.state.selectedFiles;
}
/**
* @inheritdoc
*/
async openFile() {
const files = await this.open();
if (files.length === 0) {
throw new Error("No file selected");
}
return files[0];
}
/**
* @inheritdoc
*/
async export(filePath) {
if (!this.fileManager.fileExists(filePath)) {
throw new Error("File does not exist");
}
this.setState((state) => ({
exportedFiles: [...state.exportedFiles, filePath],
lastExportedFile: filePath
}));
return [filePath];
}
/**
* @inheritdoc
*/
async exportString(content, name) {
const filename = name ?? "document.txt";
const filePath = this.fileManager.joinPath(this.fileManager.documentsDirectory(), filename);
const parentDir = filePath.split("/").slice(0, -1).join("/");
if (!this.fileManager.isDirectory(parentDir)) {
this.fileManager.createDirectory(parentDir, true);
}
this.fileManager.writeString(filePath, content);
this.setState((state) => ({
exportedFiles: [...state.exportedFiles, filePath],
lastExportedFile: filePath
}));
return [filePath];
}
/**
* @inheritdoc
*/
async exportData(data, name) {
const filename = name ?? "document.bin";
const filePath = this.fileManager.joinPath(this.fileManager.documentsDirectory(), filename);
const parentDir = filePath.split("/").slice(0, -1).join("/");
if (!this.fileManager.isDirectory(parentDir)) {
this.fileManager.createDirectory(parentDir, true);
}
this.fileManager.write(filePath, data);
this.setState((state) => ({
exportedFiles: [...state.exportedFiles, filePath],
lastExportedFile: filePath
}));
return [filePath];
}
/**
* @additional
* Mock file selection for testing
*/
mockSelectFiles(files) {
this.setState({ selectedFiles: [...files] });
}
/**
* @additional
* Get list of exported files
*/
getExportedFiles() {
return [...this.state.exportedFiles];
}
/**
* @additional
* Get last exported file
*/
getLastExportedFile() {
return this.state.lastExportedFile;
}
/**
* @additional
* Clear all selected and exported files
*/
clear() {
this.setState({
selectedFiles: [],
exportedFiles: [],
lastExportedFile: null
});
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MockDocumentPicker
});
//# sourceMappingURL=document-picker.js.map