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
98 lines (97 loc) • 4.76 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());
});
};
import os from 'os';
import path from 'path';
import fs from 'fs-extra';
import { AIElement } from './ai-element';
import { logger } from '../../lib';
import { AIElementError } from './ai-element-error';
export class AIElementCollection {
constructor(elements) {
this.elements = elements;
}
static collectAIElements(workspaceId, aiElementArgs) {
return __awaiter(this, void 0, void 0, function* () {
if (!workspaceId) {
throw new AIElementError("Value of 'workspaceId' must be defined.");
}
const workspaceAIElementFolder = path.join(AIElementCollection.AI_ELEMENT_FOLDER, workspaceId);
const aiElementsLocations = [
workspaceAIElementFolder,
...aiElementArgs.additionalLocations.map((userPath) => path.resolve(userPath)),
];
const aiElements = [];
yield Promise.all(aiElementsLocations.map((aiElementLocation) => __awaiter(this, void 0, void 0, function* () {
if (yield fs.pathExists(aiElementLocation)) {
aiElements.push(...AIElementCollection.CollectAiElementsFromLocation(aiElementLocation));
}
else {
const errorMessage = `AIElements location '${aiElementLocation}' does not exist.`;
if (aiElementArgs.onLocationNotExist === 'error') {
throw new AIElementError(errorMessage);
}
else if (aiElementArgs.onLocationNotExist === 'warn') {
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 AIElementError(errorMessage);
}
return new AIElementCollection(aiElements);
});
}
getByName(name) {
if (name === '') {
throw new AIElementError("Parameter 'name' must be non-empty. This might be due to corrupted metadata.");
}
logger.debug(`Getting all CustomElementJson with the name '${name}' ...`);
const elements = this.elements.filter((element) => element.hasName(name));
if (elements.length === 0) {
throw new 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.readdirSync(aiElementLocation);
if (files.length === 0) {
return [];
}
const aiElements = files
.filter((file) => path.extname(file) === '.json')
.map((file) => {
const jsonFile = path.join(aiElementLocation, file);
const baseName = path.basename(jsonFile, '.json');
const pngFile = path.join(aiElementLocation, `${baseName}.png`);
if (fs.existsSync(pngFile)) {
const metadata = JSON.parse(fs.readFileSync(jsonFile, 'utf-8'));
return AIElement.fromJson(metadata, pngFile);
}
return null;
});
const validAIElements = aiElements.filter((element) => element !== null);
if (validAIElements.length === 0) {
logger.debug(`No valid AIElements found in '${aiElementLocation}'.`);
}
return validAIElements;
}
}
AIElementCollection.AI_ELEMENT_FOLDER = path.join(os.homedir(), '.askui', 'SnippingTool', 'AIElement');