UNPKG

@hv-kit/hexpress

Version:

facilitates typescript backend development with express

232 lines 8.29 kB
"use strict"; 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.ImportJSON = exports.ImportExcel = exports.ImportCSV = exports.cleanImportedDatas = void 0; const hexpress_config_json_1 = __importDefault(require("../../../../hexpress.config.json")); const string_1 = require("./string"); const utils_1 = require("./utils"); const exceljs_1 = __importDefault(require("exceljs")); const csv = require('fast-csv'); const fs = require('fs'); function cleanImportedDatas(datas, config = { columns: [ { target: 'code', label: '{{0}}', type: 'str' }, { target: 'title', label: '{{1}}', type: 'str' }, { target: 'date_added', label: '{{2}}', type: 'str' }, { target: 'date_updated', label: '{{3}}', type: 'str' } ], row: { first: 1, end: undefined } }) { let res = datas.filter((value, index) => (((typeof config.row.first === 'number' && index >= config.row.first) || [undefined, NaN].includes(config.row.first)) && ((typeof config.row.end === 'number' && index <= config.row.end) || [undefined, NaN].includes(config.row.end)))); res = res.map((value, index) => { let valueFinal = {}; config.columns.forEach((column, index) => { valueFinal[column.target] = (0, utils_1.ConvertToType)((0, string_1.StringReplace)(column.label, value), column.type); if (valueFinal[column.target] === column.label) { valueFinal[column.target] = undefined; } }); return valueFinal; }); return res; } exports.cleanImportedDatas = cleanImportedDatas; function ImportCSV(file, config = { columns: [ { target: 'code', label: '{{0}}', type: 'str' }, { target: 'title', label: '{{1}}', type: 'str' }, { target: 'date_added', label: '{{2}}', type: 'str' }, { target: 'date_updated', label: '{{3}}', type: 'str' } ], row: { first: 1, end: undefined } }) { return __awaiter(this, void 0, void 0, function* () { let result = []; try { result = yield (new Promise((resolve, reject) => { const res = []; csv.parseFile(file.path).on("data", (data) => { res.push(data); }).on("end", function () { resolve(res); fs.unlinkSync(file.path); }); })); if (hexpress_config_json_1.default.debug) { console.log('----> import_functs > ImportCSV - file:: ', file); console.log('----> import_functs > ImportCSV - file.path:: ', file.path); console.log('----> import_functs > ImportCSV - result:: ', result); } } catch (error) { if (hexpress_config_json_1.default.debug) { console.log('----> import_functs > ImportCSV - error:: ', error); } result = []; fs.unlinkSync(file.path); } return cleanImportedDatas(result, config); }); } exports.ImportCSV = ImportCSV; function ImportExcel(file, config = { columns: [ { target: 'code', label: '{{0}}', type: 'str' }, { target: 'title', label: '{{1}}', type: 'str' }, { target: 'date_added', label: '{{2}}', type: 'str' }, { target: 'date_updated', label: '{{3}}', type: 'str' } ], row: { first: 1, end: undefined } }) { return __awaiter(this, void 0, void 0, function* () { let result = []; if (hexpress_config_json_1.default.debug) { console.log('----> import_functs > ImportExcel - file:: ', file); } try { const workbook = new exceljs_1.default.Workbook(); let datas = []; yield workbook.xlsx.readFile(file.path).then((workbook) => { workbook.eachSheet((worksheet) => { worksheet.eachRow({ includeEmpty: true }, function (row, rowNumber) { datas.push(JSON.parse(JSON.stringify(row.values))); }); }); }); result = datas.map((data, index) => data.filter((value, index) => !(index == 0 && !value))); fs.unlinkSync(file.path); if (hexpress_config_json_1.default.debug) { // console.log('----> import_functs > ImportExcel - datas:: ', datas); console.log('----> import_functs > ImportExcel - file:: ', file); console.log('----> import_functs > ImportExcel - file.path:: ', file.path); console.log('----> import_functs > ImportExcel - result:: ', result); } } catch (error) { if (hexpress_config_json_1.default.debug) { console.log('----> import_functs > ImportExcel - error:: ', error); } result = []; fs.unlinkSync(file.path); } return cleanImportedDatas(result, config); }); } exports.ImportExcel = ImportExcel; function ImportJSON(file, config = { columns: [ { target: 'code', label: '{{0}}', type: 'str' }, { target: 'title', label: '{{1}}', type: 'str' }, { target: 'date_added', label: '{{2}}', type: 'str' }, { target: 'date_updated', label: '{{3}}', type: 'str' } ], row: { first: 1, end: undefined } }) { return __awaiter(this, void 0, void 0, function* () { let result = []; try { let datas = JSON.parse(fs.readFileSync(file.path, hexpress_config_json_1.default.encoder)); result = datas; fs.unlinkSync(file.path); if (hexpress_config_json_1.default.debug) { // console.log('----> import_functs > ImportExcel - datas:: ', datas); console.log('----> import_functs > ImportExcel - file:: ', file); console.log('----> import_functs > ImportExcel - file.path:: ', file.path); console.log('----> import_functs > ImportExcel - typeof(result):: ', typeof (result), ' && result:: ', result); } } catch (error) { if (hexpress_config_json_1.default.debug) { console.log('----> import_functs > ImportExcel - error:: ', error); } result = []; fs.unlinkSync(file.path); } return result; }); } exports.ImportJSON = ImportJSON; exports.default = { cleanImportedDatas, ImportCSV, ImportExcel, ImportJSON, }; //# sourceMappingURL=import_functs.js.map