askui
Version:
Reliable, automated end-to-end-testing that depends on what is shown on your screen instead of the technology you are running on
105 lines (104 loc) • 5.41 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AIElementCollection = void 0;
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const ai_element_1 = require("./ai-element");
const lib_1 = require("../../lib");
const ai_element_error_1 = require("./ai-element-error");
class AIElementCollection {
constructor(elements) {
this.elements = elements;
}
static collectAIElements(workspaceId, aiElementArgs) {
return __awaiter(this, void 0, void 0, function* () {
if (!workspaceId) {
throw new ai_element_error_1.AIElementError("Value of 'workspaceId' must be defined.");
}
const workspaceAIElementFolder = path_1.default.join(AIElementCollection.AI_ELEMENT_FOLDER, workspaceId);
const aiElementsLocations = [
workspaceAIElementFolder,
...aiElementArgs.additionalLocations.map((userPath) => path_1.default.resolve(userPath)),
];
const aiElements = [];
yield Promise.all(aiElementsLocations.map((aiElementLocation) => __awaiter(this, void 0, void 0, function* () {
if (yield fs_extra_1.default.pathExists(aiElementLocation)) {
aiElements.push(...AIElementCollection.CollectAiElementsFromLocation(aiElementLocation));
}
else {
const errorMessage = `AIElements location '${aiElementLocation}' does not exist.`;
if (aiElementArgs.onLocationNotExist === 'error') {
throw new ai_element_error_1.AIElementError(errorMessage);
}
else if (aiElementArgs.onLocationNotExist === 'warn') {
lib_1.logger.warn(errorMessage);
}
}
})));
if (aiElements.length === 0) {
const formattedLocations = aiElementsLocations.map((aiElementsLocation) => `"${aiElementsLocation}"`).join(', ');
const errorMessage = `No AIElements found in the following location${aiElementsLocations.length > 1 ? 's' : ''}: [${formattedLocations}].`;
throw new ai_element_error_1.AIElementError(errorMessage);
}
return new AIElementCollection(aiElements);
});
}
getByName(name) {
if (name === '') {
throw new ai_element_error_1.AIElementError("Parameter 'name' must be non-empty. This might be due to corrupted metadata.");
}
lib_1.logger.debug(`Getting all CustomElementJson with the name '${name}' ...`);
const elements = this.elements.filter((element) => element.hasName(name));
if (elements.length === 0) {
throw new ai_element_error_1.AIElementError(`No AIElement with the name '${name}' was found.`);
}
return elements.map((element) => element.toCustomElement());
}
getByNames(names) {
if (names.length === 0) {
return [];
}
return names.flatMap((name) => this.getByName(name));
}
getNames() {
return [...new Set(this.elements.map((element) => element.name))];
}
static CollectAiElementsFromLocation(aiElementLocation) {
const files = fs_extra_1.default.readdirSync(aiElementLocation);
if (files.length === 0) {
return [];
}
const aiElements = files
.filter((file) => path_1.default.extname(file) === '.json')
.map((file) => {
const jsonFile = path_1.default.join(aiElementLocation, file);
const baseName = path_1.default.basename(jsonFile, '.json');
const pngFile = path_1.default.join(aiElementLocation, `${baseName}.png`);
if (fs_extra_1.default.existsSync(pngFile)) {
const metadata = JSON.parse(fs_extra_1.default.readFileSync(jsonFile, 'utf-8'));
return ai_element_1.AIElement.fromJson(metadata, pngFile);
}
return null;
});
const validAIElements = aiElements.filter((element) => element !== null);
if (validAIElements.length === 0) {
lib_1.logger.debug(`No valid AIElements found in '${aiElementLocation}'.`);
}
return validAIElements;
}
}
exports.AIElementCollection = AIElementCollection;
AIElementCollection.AI_ELEMENT_FOLDER = path_1.default.join(os_1.default.homedir(), '.askui', 'SnippingTool', 'AIElement');