mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
/*
|
|
* Copyright (C) 2019 StApps
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation, version 3.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
* more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import {SCThings} from '@openstapps/core';
|
|
import {readdir, readFile} from 'fs';
|
|
import {join} from 'path';
|
|
import {promisify} from 'util';
|
|
import {Bulk} from './bulk';
|
|
import {ConnectorClient} from './connectorClient';
|
|
import {HttpClientInterface} from './httpClientInterface';
|
|
|
|
/**
|
|
* Options to set up indexing core test files to backend
|
|
*/
|
|
export interface E2EOptions {
|
|
/**
|
|
* File path of the directory containing core test files
|
|
*/
|
|
samples: string;
|
|
|
|
/**
|
|
* URL of the backend to index to
|
|
*/
|
|
to: string;
|
|
}
|
|
|
|
/**
|
|
* Function to add all the SCThings that getItemsFromSamples() returns to the backend
|
|
*
|
|
* @param client HTTP client
|
|
* @param options Map of options
|
|
*/
|
|
export async function indexSamples(client: HttpClientInterface, options: E2EOptions): Promise<void> {
|
|
const api = new ConnectorClient(client, options.to);
|
|
|
|
const items = await getItemsFromSamples(options.samples);
|
|
|
|
if (items.length === 0) {
|
|
throw new Error('Could not index samples. None were retrived from the file system.');
|
|
}
|
|
|
|
items.sort((a, b) => (a.type.localeCompare(b.type)));
|
|
|
|
let currentBulkType;
|
|
let currentBulk: Bulk<SCThings> | undefined;
|
|
|
|
try {
|
|
// Add items depending on their type property with one type per bulk
|
|
for (const item of items) {
|
|
if (currentBulkType !== item.type) {
|
|
if (typeof currentBulk !== 'undefined') {
|
|
await currentBulk.done();
|
|
}
|
|
currentBulk = await api.bulk(item.type, 'stapps-core-sample-data');
|
|
currentBulkType = item.type;
|
|
}
|
|
await currentBulk!.add(item);
|
|
}
|
|
// close the last open bulk
|
|
if (typeof currentBulk !== 'undefined') {
|
|
await currentBulk.done();
|
|
}
|
|
} catch (err) {
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get all SCThings from the predefined core test json files
|
|
*
|
|
* @param samplesDirectory Filepath to the directory containing to the core test json files
|
|
* @returns an Array of all the SCThings specified for test usage
|
|
*/
|
|
export async function getItemsFromSamples<T extends SCThings>(samplesDirectory: string): Promise<T[]> {
|
|
const readDirPromised = promisify(readdir);
|
|
const readFilePromised = promisify(readFile);
|
|
|
|
const things: T[] = [];
|
|
try {
|
|
const fileNames = await readDirPromised(samplesDirectory);
|
|
for (const fileName of fileNames) {
|
|
const filePath = join(samplesDirectory, fileName);
|
|
const fileContent = await readFilePromised(filePath, {encoding: 'utf8'});
|
|
const schemaObject = JSON.parse(fileContent);
|
|
if (schemaObject.errorNames.length === 0 && typeof schemaObject.instance.type === 'string') {
|
|
things.push(schemaObject.instance);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
return error;
|
|
}
|
|
return things;
|
|
}
|