UNPKG

@wbg-mde/r-factory

Version:

Metadata editor R integration module

66 lines (65 loc) 2.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const test_config_1 = require("./test-config"); const fs = require("fs"); const path = require("path"); const variable_statistics_1 = require("../modules/convert/variable.statistics"); const import_test_1 = require("./import.test"); class UpdateVarStatsTest { constructor() { this.fileIndex = 0; this.varStats = new variable_statistics_1.VariableStatistics(); this.tstImport = new import_test_1.ImportAPITest(); } execute(files, listOfActions) { try { this.fileIndex = 0; files.reduce((p, x) => p.then(_ => this.executeFile(x, listOfActions)), Promise.resolve()); } catch (e) { console.log('execute error >> ' + e); } } executeFile(filepath, listOfActions) { this.fileIndex += 1; const inputDatasetPath = path.join(test_config_1.TestConfig.inputPath.dataset, filepath); const inputCSVPath = path.join(test_config_1.TestConfig.inputPath.csv, 'F' + this.fileIndex + '.csv'); const inputJSONPath = path.join(test_config_1.TestConfig.inputPath.json, 'F' + this.fileIndex + '.json'); const outputJSONPath = path.join(test_config_1.TestConfig.outputPath.json, 'F' + this.fileIndex + '.json'); return Promise.all([ this.tstImport.readDataFIle(inputDatasetPath, this.fileIndex, inputJSONPath), this.tstImport.writeCSV(inputDatasetPath, this.fileIndex, inputCSVPath) ]).then(resp => this.updateVarStatus(resp, outputJSONPath, listOfActions)) .then((resp) => { if ((resp && resp.result === 'ok')) { console.log(`file ${filepath} completed successfully!!`); } else { console.log(`file ${filepath} completed with errors!!`); } }).catch((e) => { console.log(`error while executing the file ${filepath}`); console.log(e); }); } updateVarStatus(resp, outPath, listOfActions) { if (resp.length === 2 && (resp[0].data && resp[0].data.result === 'ok') && (resp[1].data && resp[1].data.result === 'ok')) { const csvFilepath = resp[1].data.file; return this.varStats.updateVariableStatistics({ "variableInfo": listOfActions, "csvPath": csvFilepath }).then((resp) => { if (resp.result === 'ok') { fs.writeFileSync(outPath, JSON.stringify(resp.data, null, 4)); return resp; } else { return resp; } }); } } } exports.UpdateVarStatsTest = UpdateVarStatsTest;