datainout
Version:
Import and reports data
1,110 lines (1,085 loc) • 39.5 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
// src/importers/Importer.ts
import * as fs2 from "fs";
// src/helpers/path-file.ts
import * as path2 from "path";
// src/helpers/datainout-config.ts
import * as path from "path";
import * as fs from "fs";
var globalConfig = {};
function getConfig() {
if (Object.keys(globalConfig).length === 0) {
const jsPath = path.join(process.cwd(), "datainout.config.js");
const tsPath = path.join(process.cwd(), "datainout.config.ts");
if (fs.existsSync(jsPath)) globalConfig = __require(jsPath);
else if (fs.existsSync(tsPath)) globalConfig = __require(tsPath).default;
else globalConfig = { templateExtension: ".js", dateFormat: "DD-MM-YYYY" };
}
return globalConfig;
}
// src/helpers/path-file.ts
var config = getConfig();
function pathImport(_path, fieldName) {
var _a;
if (!(config == null ? void 0 : config.import) || !fieldName || !((_a = config == null ? void 0 : config.import) == null ? void 0 : _a[fieldName])) return path2.join(process.cwd(), _path);
return path2.join(process.cwd(), config.import[fieldName], _path);
}
// src/helpers/parse-type.ts
var TypeParser = class {
constructor(opts) {
var _a;
this.dateFormat = (_a = opts == null ? void 0 : opts.dateFormat) != null ? _a : "DD-MM-YYYY";
}
boolean(val) {
return val === "true";
}
date(val) {
return new Date(val);
}
number(val) {
return +val;
}
object(val) {
return JSON.parse(val);
}
string(val) {
return val + "";
}
};
// src/helpers/excel.helper.ts
import * as exceljs from "exceljs";
var SYNTAX = {
VARIABLE_TABLE_SYNTAX: "$$",
VARIABLE_SYNTAX: "$",
INDEX_COLUMN_TABLE_SYNTAX: "$$**",
END_TABLE_SYNYAX: "$$br"
};
var DEFAULT_BEGIN_TABLE = -1;
var DEFAULT_END_TABLE = -1;
var DEFAULT_COLUMN_INDEX = 1;
var ReaderExceljsHelper = class _ReaderExceljsHelper {
constructor(opts) {
this.isSampleExcel = true;
this.isStop = false;
var _a;
this.onCell = opts == null ? void 0 : opts.onCell;
this.onRow = opts == null ? void 0 : opts.onRow;
this.onSheet = opts == null ? void 0 : opts.onSheet;
this.isSampleExcel = (_a = opts == null ? void 0 : opts.isSampleExcel) != null ? _a : true;
this.templateManager = opts.templateManager;
}
load(arg) {
return __async(this, null, function* () {
const workBook = new exceljs.Workbook();
if (arg instanceof Buffer) yield workBook.xlsx.load(arg);
else yield workBook.xlsx.readFile(arg);
for (let i = 0; !this.isStop && i < workBook.worksheets.length; i++) {
const workSheet = workBook.getWorksheet(i + 1);
if (workSheet) yield this.eachSheet(workSheet, i + 1);
}
});
}
eachSheet(sheet, sheetIndex) {
return __async(this, null, function* () {
var _a, _b, _c, _d;
this.templateManager.SheetIndex = sheetIndex - 1;
const sheetDesc = this.templateManager.SheetTemplate;
const trackingRows = [];
let columnIndex = DEFAULT_COLUMN_INDEX;
let beginTable = DEFAULT_BEGIN_TABLE;
let endTable = DEFAULT_END_TABLE;
for (let i = 1; !this.isStop && i <= sheet.rowCount; i++) {
const row = sheet.getRow(i);
this.templateManager.defineActualTableStartRow(_ReaderExceljsHelper.beginTableAt(row, sheetDesc, this.isSampleExcel));
this.templateManager.defineActualTableStartRow(_ReaderExceljsHelper.endTableAt(row, sheetDesc, this.isSampleExcel));
columnIndex = (_a = _ReaderExceljsHelper.columnTableIndex(row, sheetDesc, this.isSampleExcel)) != null ? _a : columnIndex;
endTable = (_b = this.templateManager.ActualTableEndRow) != null ? _b : DEFAULT_END_TABLE;
beginTable = (_c = this.templateManager.ActualTableStartRow) != null ? _c : DEFAULT_BEGIN_TABLE;
const section = _ReaderExceljsHelper.getSection(row, beginTable, endTable);
const cells = [];
for (let j = 0; j < row.cellCount; j++) {
if (!((_d = row.getCell(j + 1)) == null ? void 0 : _d.value)) continue;
const cell = this.convertCell(row.getCell(j + 1), section, beginTable, endTable);
cells.push(cell);
if (this.onCell) yield this.onCell(cell);
}
if (this.onRow) {
const rowDataHelper = this.convertRow(row, section, cells);
if (i === 1) trackingRows.push(rowDataHelper);
if (i === sheet.rowCount) trackingRows.push(rowDataHelper);
yield this.onRow(rowDataHelper);
}
}
if (this.onSheet)
yield this.onSheet({
beginTableAt: beginTable != null ? beginTable : 1,
columnIndex,
endTableAt: endTable,
sheetIndex,
name: sheet.name,
detail: sheet,
rowCount: sheet.rowCount,
lastestRow: trackingRows[1],
firstRow: trackingRows[0]
});
});
}
convertCell(cell, section, beginTableAt, endTableAt) {
var _a;
const isVariable = _ReaderExceljsHelper.isVariable(cell.value);
return {
address: _ReaderExceljsHelper.getAddress(cell.address, section, isVariable),
detail: cell,
isVariable,
rowIndex: +cell.fullAddress.row,
section,
label: _ReaderExceljsHelper.getLabel(cell.value + "", isVariable),
variableValue: isVariable ? _ReaderExceljsHelper.getVariableValue(cell) : void 0,
beginTableAt,
endTableAt,
formula: (_a = cell.formula) != null ? _a : void 0
};
}
convertRow(row, section, cells) {
var _a, _b, _c, _d;
return {
detail: row,
rowIndex: row.number,
section,
cells,
beginTableAt: (_b = (_a = cells[0]) == null ? void 0 : _a.beginTableAt) != null ? _b : DEFAULT_BEGIN_TABLE,
endTableAt: (_d = (_c = cells[0]) == null ? void 0 : _c.endTableAt) != null ? _d : DEFAULT_END_TABLE
};
}
static getSection(row, beginTable, endTable) {
const rowIndex = typeof row === "number" ? row : row.number;
let section = "header";
if (beginTable === DEFAULT_BEGIN_TABLE) section = "header";
else if (beginTable !== DEFAULT_BEGIN_TABLE && rowIndex < beginTable) section = "header";
else if (rowIndex > endTable && endTable !== DEFAULT_END_TABLE) section = "footer";
else if (rowIndex > beginTable) section = "table";
return section;
}
/** Get begin table at by row */
static beginTableAt(row, sheetOpts, isSampleExcel = true) {
if (isSampleExcel)
for (let i = 0; i < row.cellCount; i++) {
const cellValue = row.getCell(i + 1).value;
if (cellValue === void 0 || typeof cellValue !== "string") continue;
if (cellValue.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return row.number;
if (cellValue.includes(SYNTAX.VARIABLE_TABLE_SYNTAX)) return row.number - 1;
}
else if (sheetOpts) return sheetOpts.beginTableAt;
return void 0;
}
/** Get end table at by row */
static endTableAt(row, sheetOpts, isSampleExcel = true) {
var _a;
if (isSampleExcel)
for (let i = 0; i < row.cellCount; i++) {
const cellValue = row.getCell(i + 1).value;
if (cellValue === void 0 || typeof cellValue !== "string") continue;
if (cellValue.includes(SYNTAX.END_TABLE_SYNYAX)) return row.number - 1;
if (!cellValue.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX) && cellValue.includes(SYNTAX.VARIABLE_TABLE_SYNTAX)) return row.number;
}
else if (sheetOpts) {
const cellvalues = (_a = row == null ? void 0 : row.values) != null ? _a : [];
if (cellvalues && !cellvalues[sheetOpts.keyTableAt] && row.number > sheetOpts.beginTableAt) return row.number - 1;
}
return void 0;
}
static isNullableRow(row) {
var _a, _b;
return (_b = (_a = row == null ? void 0 : row.values) == null ? void 0 : _a.reduce((acc, val) => acc && !!!val, true)) != null ? _b : false;
}
/** Get key of table by row */
static columnTableIndex(row, sheetOpts, isSampleExcel = true) {
if (isSampleExcel)
for (let i = 0; i < row.cellCount; i++) {
const cellValue = row.getCell(i + 1).value;
if (cellValue === void 0 || typeof cellValue !== "string") continue;
if ((cellValue + "").includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return row.number + 1;
}
else if (sheetOpts) return sheetOpts.keyTableAt;
return void 0;
}
static isVariable(cellValue) {
cellValue = cellValue + "";
if (cellValue.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return false;
return cellValue.includes(SYNTAX.VARIABLE_SYNTAX);
}
static getLabel(label, isVariable) {
if (isVariable) return void 0;
if (label.includes(SYNTAX.INDEX_COLUMN_TABLE_SYNTAX)) return label.split("->").pop();
return label;
}
static getAddress(address, section, isVariable) {
return !isVariable ? address : section !== "header" ? address.split(/\d+/)[0] : address;
}
static getVariableValue(cell) {
const cellValue = (cell.value + "").replace("$$", "$");
let fieldName = "";
let type = "string";
fieldName = cellValue.split("$")[1];
if (fieldName.includes("->")) {
const args = fieldName.split("->");
fieldName = args[0];
type = args[1].toLowerCase();
}
return { fieldName, type };
}
static splitAddress(address) {
const col = address.split(/\d+/)[0];
const row = address.split(/[a-zA-Z]/)[1];
return { col, row };
}
};
// src/common/core/error-message-default.ts
var ERROR_MESSAGE = {
MESSAGE_REQUIRED_CELL: "must be required",
MESSAGE_INVALID_CELL: "validate failed"
};
// src/common/error/ValidateError.ts
var ValidateImportError = class extends Error {
constructor(opts) {
super(opts.message);
this.address = opts == null ? void 0 : opts.address;
this.keyField = opts.keyField;
this.value = opts.value;
}
};
// src/helpers/validate-cell-importer.ts
function validateCellImport(value, cellDes, address, keyField) {
const isNullValue = value === void 0 || value === null;
const ValidateImportErrorOpts = {
keyField,
address,
value
};
if (isNullValue && cellDes.required === true) {
const message = `${ERROR_MESSAGE.MESSAGE_REQUIRED_CELL}`;
throw new ValidateImportError(__spreadProps(__spreadValues({}, ValidateImportErrorOpts), {
message
}));
}
if (cellDes.validate) {
const result = cellDes.validate(value);
if (result instanceof Error) throw result;
const { isValid, message } = result;
if (isValid) {
const defaultMessage = `${ERROR_MESSAGE.MESSAGE_INVALID_CELL}`;
throw new ValidateImportError(__spreadProps(__spreadValues({}, ValidateImportErrorOpts), {
message: message != null ? message : defaultMessage
}));
}
}
}
// src/helpers/convert-row-to-table-data-v2.ts
var ConvertorRows2TableData = class {
constructor(opts) {
this.tableCols = [];
this.container = { header: {}, footer: {}, table: [] };
this.section = "header";
this.addresses = { header: {}, footer: {}, table: [] };
this.onTrigger = () => __async(this, null, function* () {
});
this.onErrors = () => __async(this, null, function* () {
});
var _a, _b, _c, _d;
this.chunkSize = (_a = opts.chunkSize) != null ? _a : 10;
this.typeParser = (_b = opts.typeParser) != null ? _b : new TypeParser();
this.templateManager = opts.templateManager;
this.onErrors = (_c = opts == null ? void 0 : opts.onErrors) != null ? _c : this.onErrors;
this.onTrigger = (_d = opts == null ? void 0 : opts.onTrigger) != null ? _d : this.onTrigger;
this.tableCols = this.templateManager.GroupCells.table.map((e) => e.keyName);
}
push(row) {
return __async(this, null, function* () {
if (row === null) return yield this.complete();
const section = this.getSection(row.number);
let values = {};
let address = {};
for (let i = 0; i < row.cellCount; i++) {
const cell = row.getCell(i + 1);
const keyName = this.getKeyField(cell, section);
if (!keyName) continue;
values[keyName] = cell.value;
address[keyName] = cell.address;
}
if (Object.keys(values).length > 0) this.addContainer(section, values, address);
const isTrigger = this.isTrigger(section, row.number);
if (isTrigger) {
const data = yield this.pop(section);
if (data) yield this.onTrigger(section, data);
}
this.section = section;
});
}
pop(section) {
return __async(this, null, function* () {
const data = Array.isArray(this.container[section]) ? this.container[section] : [this.container[section]];
const address = Array.isArray(this.addresses[section]) ? this.addresses[section] : [this.addresses[section]];
const error = [];
if (section === "table") {
this.container.table = [];
this.addresses.table = [];
} else {
this.container[section] = {};
this.addresses[section] = {};
}
for (let i = 0; i < data.length; i++) {
const { errors, row } = this.formatValue(data[i], address[i], section);
if (row) data[i] = row;
if (errors && errors.length > 0) error.push(...errors);
}
if (error.length !== 0) yield this.onErrors(error);
return !data[0] || Object.keys(data[0]).length === 0 ? null : section === "table" ? data : data[0];
});
}
getSection(rowIndex) {
var _a, _b;
const beginTable = (_a = this.templateManager.ActualTableStartRow) != null ? _a : DEFAULT_BEGIN_TABLE;
const endTable = (_b = this.templateManager.ActualTableEndRow) != null ? _b : DEFAULT_END_TABLE;
const section = ReaderExceljsHelper.getSection(rowIndex, beginTable, endTable);
return section;
}
isTrigger(section, rowIndex) {
var _a, _b, _c;
const beginTable = (_a = this.templateManager.ActualTableStartRow) != null ? _a : DEFAULT_BEGIN_TABLE;
const chunkSize = (_c = (_b = this.container) == null ? void 0 : _b.table) == null ? void 0 : _c.length;
if (beginTable === rowIndex) return true;
else if (section === "footer" && this.section === "table") return true;
else if (this.chunkSize <= chunkSize && section === "table") return true;
return false;
}
addContainer(section, data, address) {
if (!this.addresses[section] && section === "table") this.addresses.table = [];
if (!this.addresses[section] && section !== "table") this.addresses[section] = {};
if (section === "table") {
this.container.table.push(data);
this.addresses.table.push(address);
} else {
this.container[section] = __spreadValues(__spreadValues({}, this.container[section]), data);
this.addresses[section] = __spreadValues(__spreadValues({}, this.addresses[section]), address);
}
}
formatValue(data, address, section) {
var _a;
const row = __spreadValues({}, data);
const errors = [];
if (!this.templateManager.GroupCells[section]) return { row: null, errors: null };
(_a = this.templateManager.GroupCells[section]) == null ? void 0 : _a.forEach((cell) => {
let value = row[cell.keyName];
if (cell.setValue) value = cell.setValue(value, row);
if (cell.type && cell.type !== "virtual") value = this.typeParser[cell.type](value);
try {
validateCellImport(value, cell, address[cell.keyName], cell.keyName);
row[cell.keyName] = value;
} catch (error) {
errors.push(error);
}
});
return { row, errors };
}
getKeyField(cell, section) {
if (section === "table") {
return this.tableCols[cell.fullAddress.col - 1];
}
if (section === "header" && this.templateManager.GroupCells.header) {
const headerCells = this.templateManager.GroupCells.header;
const index = headerCells.findIndex((e) => e.address === cell.address);
if (index >= 0) return headerCells[index].keyName;
else return "";
}
if (this.templateManager.GroupCells.footer) {
const footerCells = this.templateManager.GroupCells.footer;
const index = footerCells.findIndex((e) => {
var _a, _b, _c, _d;
const rownum = ((_a = this.templateManager.ActualTableEndRow) != null ? _a : DEFAULT_END_TABLE) + ((_c = (_b = e.fullAddress) == null ? void 0 : _b.row) != null ? _c : 0);
return cell.address === `${(_d = e.fullAddress) == null ? void 0 : _d.col}${rownum}`;
});
if (index >= 0) return footerCells[index].keyName;
else return "";
}
return null;
}
complete() {
return __async(this, null, function* () {
let data = yield this.pop(this.section);
if (data) yield this.onTrigger(this.section, data);
data = yield this.pop("footer");
if (data) yield this.onTrigger("footer", data);
yield this.onTrigger(this.section, null);
});
}
};
// src/importers/ImporterHandler.ts
var ImporterHandler = class {
constructor(eachRow = false) {
this.eachRow = false;
this.eachRow = eachRow;
}
run(tabledata, filterImporter, setGlobalError = (err) => {
}) {
return __async(this, null, function* () {
try {
if (tabledata instanceof Error) yield this.catch(tabledata);
else if (tabledata.header) yield this.handleHeader(tabledata.header, filterImporter);
else if (tabledata.footer) yield this.handleHeader(tabledata.footer, filterImporter);
else if (tabledata.table && !this.eachRow) yield this.handleChunk(tabledata.table, filterImporter);
else if (tabledata.table && this.eachRow)
for (let i = 0; i < tabledata.table.length; i++)
try {
yield this.handleRow(tabledata.table[i], filterImporter);
} catch (error) {
yield this.catch(error);
setGlobalError(error);
}
} catch (e) {
yield this.catch(e);
setGlobalError(e);
}
});
}
catch(error) {
return __async(this, null, function* () {
});
}
handleChunk(chunk, filter) {
return __async(this, null, function* () {
});
}
handleRow(data, filter) {
return __async(this, null, function* () {
});
}
handleHeader(header, filter) {
return __async(this, null, function* () {
});
}
handleFooter(footer, filter) {
return __async(this, null, function* () {
});
}
};
// src/common/core/RingPromise.ts
import { Writable } from "stream";
var RingPromise = class {
constructor(ringSize, task) {
this.ring = [];
this.index = 0;
this.ring = new Array(ringSize).fill(Promise.resolve());
this.task = task;
}
run(...arg) {
return __async(this, null, function* () {
this.index = (this.index + 1) % this.ring.length;
this.ring[this.index] = this.task(...arg);
if (this.index === this.ring.length - 1) yield Promise.all(this.ring);
});
}
stream() {
const that = this;
return new Writable({
objectMode: true,
write(arg, _encoding, callback) {
return __async(this, null, function* () {
try {
yield that.run(...arg);
} catch (err) {
return callback(err);
}
callback();
});
},
final(callback) {
Promise.all(that.ring).then(() => callback()).catch((err) => callback(err));
}
});
}
};
// src/common/core/QueueData.ts
var QueueData = class {
constructor(size = 50) {
this.cells = [];
this.resolveFunc = () => {
};
this.size = size;
this.waitingFunc = this.createWaiter();
}
createWaiter() {
return new Promise((resolve) => {
this.resolveFunc = resolve;
});
}
waiting() {
return __async(this, null, function* () {
if (this.cells.length < this.size) return;
yield this.waitingFunc;
});
}
shift() {
const data = this.cells.shift();
if (this.cells.length === this.size - 1) {
this.resolveFunc();
}
return data;
}
add(data) {
this.cells.push(data);
if (this.cells.length === this.size) {
this.waitingFunc = this.createWaiter();
}
}
};
// src/importers/readers/BaseReader.ts
var BaseReader = class {
constructor(opts) {
this.handler = {};
this.queueData = new QueueData(100);
this.ringPromise = {};
var _a;
this.type = opts.type;
this.typeParser = (_a = opts.typeParser) != null ? _a : new TypeParser();
this.templateManager = opts.templateManager;
this.convertorRows2TableData = new ConvertorRows2TableData({
onTrigger: (sect, data) => __async(this, null, function* () {
return yield this.onTrigger(sect, data);
}),
onErrors: (err) => __async(this, null, function* () {
return yield this.onErrors(err);
}),
chunkSize: opts == null ? void 0 : opts.chunkSize,
templateManager: this.templateManager
});
}
run(templateManager, arg, handler, opts) {
return __async(this, null, function* () {
var _a;
this.convertorRows2TableData = new ConvertorRows2TableData({
onTrigger: (sect, data) => __async(this, null, function* () {
return yield this.onTrigger(sect, data);
}),
onErrors: (err) => __async(this, null, function* () {
return yield this.onErrors(err);
}),
chunkSize: opts == null ? void 0 : opts.chunkSize,
templateManager: this.templateManager
});
this.ringPromise = new RingPromise((_a = opts == null ? void 0 : opts.jobCount) != null ? _a : 1, this.createTask());
this.options = opts;
this.handler = handler;
this.templateManager = templateManager;
yield this.load(arg);
this.consumeData();
});
}
consumeData() {
return __async(this, null, function* () {
while (this.globalError === void 0) {
const queueData = this.queueData.shift();
if (queueData && queueData.data) yield this.ringPromise.run(queueData == null ? void 0 : queueData.data, queueData == null ? void 0 : queueData.filter);
else if (!(queueData == null ? void 0 : queueData.data)) break;
else if (!queueData) yield new Promise((r) => setTimeout(r, 2));
if (this.globalError) {
console.error("Error occurred, stopping the reader.");
break;
}
}
});
}
getType() {
return this.type;
}
createTask() {
return (data, filter) => __async(this, null, function* () {
if (this.handler instanceof ImporterHandler) yield this.handler.run(data, filter, this.setGlobalError);
else
for (let i = 0; i < this.handler.length; i++) {
try {
data = yield this.handler[i](data, filter);
} catch (error) {
this.setGlobalError(error);
}
}
});
}
onTrigger(section, data) {
return __async(this, null, function* () {
var _a;
const filter = {
section,
sheetIndex: (_a = this.templateManager.SheetInformation.sheetIndex) != null ? _a : 0,
sheetName: this.templateManager.SheetInformation.sheetName,
isHasNext: data !== null
};
yield this.queueData.waiting();
this.queueData.add({ data, filter });
});
}
onErrors(errors) {
return __async(this, null, function* () {
var _a;
errors = Array.isArray(errors) ? errors : [errors];
const task = this.createTask();
if ((_a = this.options) == null ? void 0 : _a.ignoreErrors) this.setGlobalError(errors[0]);
else
for (let i = 0; i < errors.length; i++) {
yield task(errors[i], null);
}
});
}
setGlobalError(err) {
var _a;
if (!((_a = this.options) == null ? void 0 : _a.ignoreErrors) && this.globalError) this.globalError = err;
}
};
// src/importers/readers/exceljs/ExcelJsReader.ts
var ExcelJsReader = class extends BaseReader {
constructor(templateManager) {
super({ type: "excel", typeParser: new TypeParser(), templateManager });
this.excelReaderHelper = null;
}
load(arg) {
return __async(this, null, function* () {
var _a;
this.excelReaderHelper = new ReaderExceljsHelper({
onSheet: () => __async(this, null, function* () {
yield this.convertorRows2TableData.push(null);
}),
onRow: (data) => __async(this, null, function* () {
yield this.convertorRows2TableData.push(data.detail);
}),
isSampleExcel: false,
templateManager: this.templateManager
});
this.convertorRows2TableData = new ConvertorRows2TableData({
onTrigger: (sect, data) => __async(this, null, function* () {
return yield this.callHandler(sect, data);
}),
onErrors: (err) => __async(this, null, function* () {
return yield this.onErrors(err);
}),
chunkSize: (_a = this.options) == null ? void 0 : _a.chunkSize,
templateManager: this.templateManager
});
const buffer = Buffer.isBuffer(arg) ? arg : Buffer.from(arg);
yield this.excelReaderHelper.load(buffer);
});
}
createTask() {
return (section, data) => __async(this, null, function* () {
var _a;
const filter = {
section,
sheetIndex: (_a = this.templateManager.SheetInformation.sheetIndex) != null ? _a : 0,
sheetName: this.templateManager.SheetInformation.sheetName,
isHasNext: data !== null
};
let err;
data = data instanceof Error ? data : { [section]: data };
const setGlobalError = (e) => err = e;
if (this.handler instanceof ImporterHandler) {
yield this.handler.run(data, filter, setGlobalError);
if (err) throw err;
} else for (let i = 0; i < this.handler.length; i++) data = yield this.handler[i](data, filter);
});
}
callHandler(section, data) {
return __async(this, null, function* () {
yield this.ringPromise.run(section, data);
});
}
onErrors(errors) {
return __async(this, null, function* () {
var _a, _b;
errors = Array.isArray(errors) ? errors : [errors];
if (!((_a = this.options) == null ? void 0 : _a.ignoreErrors)) throw errors[0];
if ((_b = this.options) == null ? void 0 : _b.ignoreErrors) yield this.callHandler(null, errors[0]);
else for (let i = 0; i < errors.length; i++) yield this.callHandler(null, errors[i]);
});
}
};
// src/importers/readers/csv/ExceljsCsvReader.ts
import * as exceljs2 from "exceljs";
import { Readable } from "stream";
var ExcelJsCsvReader = class extends BaseReader {
constructor(templateManager) {
super({ type: "csv", templateManager });
}
load(arg) {
return __async(this, null, function* () {
let workSheet;
const workBook = new exceljs2.Workbook();
if (arg instanceof Buffer) {
const stream2 = new Readable();
workSheet = yield workBook.csv.read(stream2);
stream2.push(arg);
stream2.push(null);
} else workSheet = yield workBook.csv.readFile(arg);
for (let i = 0; i < workSheet.actualRowCount; i++) {
const row = workSheet.getRow(i + 1);
this.templateManager.defineActualTableStartRow(ReaderExceljsHelper.beginTableAt(row, this.templateManager.SheetTemplate, false));
this.templateManager.defineActualTableEndRow(ReaderExceljsHelper.endTableAt(row, this.templateManager.SheetTemplate, false));
yield this.convertorRows2TableData.push(row);
}
yield this.convertorRows2TableData.push(null);
});
}
};
// src/importers/readers/exceljs/ExcelJsStreamReaderV2.ts
import * as exceljs3 from "exceljs";
// src/common/core/ListEvents.ts
var EventRegister = class {
constructor() {
this.listEvents = {};
}
on(key, func) {
this.listEvents[key] = func;
return this;
}
onStart(func) {
this.listEvents.start = func;
return this;
}
onFinish(func) {
this.listEvents.finish = func;
return this;
}
onFile(func) {
this.listEvents.onFile = func;
return this;
}
onBegin(func) {
this.listEvents.begin = func;
return this;
}
onData(func) {
this.listEvents.data = func;
return this;
}
endData(func) {
this.listEvents.enddata = func;
return this;
}
onEnd(func) {
this.listEvents.end = func;
return this;
}
onError(func) {
this.listEvents.error = func;
return this;
}
onHeader(func) {
this.listEvents.header = func;
return this;
}
onFooter(func) {
this.listEvents.footer = func;
return this;
}
emitEvent(key, data) {
if (this.listEvents[key]) this.listEvents[key](data);
return this;
}
};
// src/importers/readers/BaserReaderStream.ts
var BaseReaderStream = class extends BaseReader {
constructor(templateManager, readable, handler) {
super({ type: "excel-stream", typeParser: new TypeParser(), templateManager });
this.listEvents = new EventRegister();
this.templateManager = templateManager;
this.handler = handler;
this.readable = readable;
}
onStart(func) {
throw new Error("Method not implemented.");
}
endData(func) {
throw new Error("Method not implemented.");
}
onHeader(func) {
throw new Error("Method not implemented.");
}
onFooter(func) {
throw new Error("Method not implemented.");
}
start() {
(() => __async(this, null, function* () {
return yield this.run(this.templateManager, this.readable, this.handler);
}))();
}
on(key, func) {
this.listEvents.on(key, func);
return this;
}
onFinish(func) {
this.listEvents.onFinish(func);
return this;
}
onBegin(func) {
this.listEvents.onBegin(func);
return this;
}
onData(func) {
this.listEvents.onData(func);
return this;
}
onEnd(func) {
this.listEvents.onEnd(func);
return this;
}
onError(func) {
this.listEvents.onError(func);
return this;
}
};
// src/importers/readers/exceljs/ExcelJsStreamReaderV2.ts
var ExcelJsStreamReader = class extends BaseReaderStream {
constructor(templateManager, readable, handlers) {
super(templateManager, readable, handlers);
}
load(arg) {
return __async(this, null, function* () {
const workBookReader = new exceljs3.stream.xlsx.WorkbookReader(arg, {
worksheets: "emit"
});
try {
for (var iter2 = __forAwait(workBookReader), more2, temp2, error2; more2 = !(temp2 = yield iter2.next()).done; more2 = false) {
const worksheet = temp2.value;
const sheet = worksheet;
this.listEvents.emitEvent("begin", sheet.name);
try {
for (var iter = __forAwait(worksheet), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
const row = temp.value;
if (this.globalError) throw this.globalError;
this.templateManager.defineActualTableStartRow(ReaderExceljsHelper.beginTableAt(row, this.templateManager.SheetTemplate, false));
this.templateManager.defineActualTableEndRow(ReaderExceljsHelper.endTableAt(row, this.templateManager.SheetTemplate, false));
yield this.convertorRows2TableData.push(row);
this.listEvents.emitEvent("data");
}
} catch (temp) {
error = [temp];
} finally {
try {
more && (temp = iter.return) && (yield temp.call(iter));
} finally {
if (error)
throw error[0];
}
}
this.listEvents.emitEvent("end", sheet.name);
}
} catch (temp2) {
error2 = [temp2];
} finally {
try {
more2 && (temp2 = iter2.return) && (yield temp2.call(iter2));
} finally {
if (error2)
throw error2[0];
}
}
yield this.convertorRows2TableData.push(null);
this.listEvents.emitEvent("finish");
return this;
});
}
};
// src/helpers/get-file-extension.ts
function getFileExtension(path3) {
const fileName = path3.replace("\\", "/").split("/").pop();
if (!fileName) throw new Error(`Path [${path3}] invalid`);
const extension = fileName.split(".").pop();
if (!extension) throw new Error(`Path [${path3}] invalid`);
if (extension === "js") return "js";
else if (extension === "ts") return "ts";
else throw new Error(`Path [${path3}] invalid`);
}
// src/helpers/sort-by-address.ts
function reverseString(str) {
return str.split("").reverse().join("");
}
function sortByAddress(arr, mode = "ASC") {
return arr.sort((a, b) => {
if (!a.address) return 1;
if (!b.address) return -1;
if (a.address === b.address) return 0;
const result = reverseString(a.address).localeCompare(reverseString(b.address));
return result * (mode === "DESC" ? -1 : 1);
});
}
// src/common/core/Template.ts
var ExcelTemplateManager = class {
constructor(templatePath) {
this.sheets = [];
this.currentSheetIndex = 0;
this.groupCells = {};
this.isNullTemplate = false;
this.isNullTemplate = !templatePath;
this.sheets = templatePath ? this.getTemplate(templatePath) : [];
}
get ActualTableStartRow() {
return this.actualTableStartRow;
}
get ActualTableEndRow() {
return this.actualTableEndRow;
}
defineActualTableStartRow(actualTableStartRow) {
if (!actualTableStartRow || actualTableStartRow <= 0) return;
if (this.ActualTableStartRow) return;
this.actualTableStartRow = actualTableStartRow;
}
defineActualTableEndRow(actualTableEndRow) {
if (!actualTableEndRow || actualTableEndRow <= 0) return;
if (this.ActualTableEndRow) return;
this.actualTableEndRow = actualTableEndRow;
}
set SheetIndex(sheetIndex) {
if (sheetIndex < 0) return;
this.currentSheetIndex = sheetIndex;
this.groupCells = this.formatSheet();
}
get SheetInformation() {
return this.SheetTemplate;
}
get SheetTemplate() {
return this.sheets[this.currentSheetIndex];
}
get Sheets() {
return this.sheets;
}
get GroupCells() {
return this.groupCells;
}
get(arg) {
if (this.isNullTemplate) throw new Error("Template is null. Please check template path");
let index = 0;
if (typeof arg === "string") index = this.findIndexByKeyName(arg);
else if (typeof arg === "number") index = arg;
return this.SheetTemplate.cells[index];
}
add(cell) {
if (this.isNullTemplate) throw new Error("Template is null. Please check template path");
if (!Array.isArray(cell)) cell = [cell];
this.SheetTemplate.cells.push(...cell);
}
update(key, cell) {
if (this.isNullTemplate) throw new Error("Template is null. Please check template path");
let index = 0;
if (typeof key === "string") index = this.findIndexByKeyName(key);
else if (typeof key === "number") index = key;
this.SheetTemplate.cells[index] = cell;
}
remove(key) {
if (this.isNullTemplate) throw new Error("Template is null. Please check template path");
let index = 0;
if (typeof key === "string") index = this.findIndexByKeyName(key);
else if (typeof key === "number") index = key;
this.SheetTemplate.cells.splice(index, 1);
}
findIndexByKeyName(key) {
const index = this.SheetTemplate.cells.findIndex((e) => e.keyName === key);
if (index < 0) throw new Error("Not found template with key: " + key);
return index;
}
getTemplate(templatePath) {
const template = getFileExtension(templatePath) === "js" ? __require(templatePath) : __require(templatePath).default;
return template.sheets;
}
formatSheet() {
var _a;
const defaultGroup = {};
if (this.isNullTemplate) return defaultGroup;
const excel = (_a = this.SheetTemplate) == null ? void 0 : _a.cells.reduce((acc, cell) => {
var _a2;
if (!acc[cell.section]) acc[cell.section] = [cell];
else (_a2 = acc[cell.section]) == null ? void 0 : _a2.push(cell);
return acc;
}, {});
const keys = Object.keys(excel);
for (let i = 0; i < keys.length; i++) excel[keys[i]] = sortByAddress(excel[keys[i]]);
return excel;
}
};
// src/importers/Importer.ts
var Importer = class {
constructor(templatePath) {
var _a;
this.templatePath = pathImport(templatePath, "templateDir");
this.templatePath = `${this.templatePath}${(_a = getConfig().templateExtension) != null ? _a : ".js"}`;
this.excelsTemplate = new ExcelTemplateManager(this.templatePath);
this.excelsTemplate.SheetIndex = 0;
}
get ExcelTemplate() {
return this.excelsTemplate;
}
load(arg, handler, opts) {
return __async(this, null, function* () {
var _a;
const type = (_a = opts == null ? void 0 : opts.type) != null ? _a : "excel";
const reader = this.createBaseReader(type);
yield reader.run(this.excelsTemplate, arg, handler, opts);
});
}
createStream(arg, handler, type) {
if (!type) type = "excel-stream";
const fsStream = typeof arg === "string" ? fs2.createReadStream(pathImport(arg, "layoutDir")) : arg;
const readerStream = new ExcelJsStreamReader(this.excelsTemplate, fsStream, handler);
return readerStream;
}
createBaseReader(type) {
if (type === "excel") return new ExcelJsReader(this.excelsTemplate);
if (type === "csv") return new ExcelJsCsvReader(this.excelsTemplate);
throw new Error(`Type ${type} not supports`);
}
};
export {
Importer
};