extensions refactor

This commit is contained in:
Jan Prochazka
2020-11-21 20:12:33 +01:00
parent 3009724a82
commit 5ec39054a3
18 changed files with 200 additions and 227 deletions

View File

@@ -1,6 +1,82 @@
import { usePlugins } from '../plugins/PluginsProvider';
import axios from './axios';
import { FormSchemaSelect } from './forms';
export function useFileFormats() {
const plugins = usePlugins();
return [];
const excelFormat = {
storageType: 'excel',
extension: 'xlsx',
name: 'MS Excel',
readerFunc: 'excelSheetReader',
writerFunc: 'excelSheetWriter',
addFilesToSourceList: async (file, newSources, newValues) => {
const resp = await axios.get(`files/analyse-excel?filePath=${encodeURIComponent(file.full)}`);
const sheetNames = resp.data;
for (const sheetName of sheetNames) {
newSources.push(sheetName);
newValues[`sourceFile_${sheetName}`] = {
fileName: file.full,
sheetName,
};
}
},
args: [
{
type: 'checkbox',
name: 'singleFile',
label: 'Create single file',
direction: 'target',
},
],
getDefaultOutputName: (sourceName, values) => {
if (values.target_excel_singleFile) {
return sourceName;
}
return null;
},
getOutputParams: (sourceName, values) => {
if (values.target_excel_singleFile) {
return {
sheetName: values[`targetName_${sourceName}`] || sourceName,
fileName: 'data.xlsx',
};
}
return null;
},
};
const jsonlFormat = {
storageType: 'jsonl',
extension: 'jsonl',
name: 'JSON lines',
readerFunc: 'jsonLinesReader',
writerFunc: 'jsonLinesWriter',
};
export function buildFileFormats(plugins) {
const res = [excelFormat, jsonlFormat];
for (const { content } of plugins) {
const { fileFormats } = content;
if (fileFormats) res.push(...fileFormats);
}
return res;
}
export function findFileFormat(extensions, storageType) {
return extensions.fileFormats.find((x) => x.storageType == storageType);
}
export function getFileFormatDirections(format) {
if (!format) return [];
const res = [];
if (format.readerFunc) res.push('source');
if (format.writerFunc) res.push('target');
return res;
}
export function getDefaultFileFormat(extensions) {
return extensions.fileFormats.find((x) => x.storageType == 'csv') || jsonlFormat;
}