UNPKG

@wbg-mde/r-factory

Version:

Metadata editor R integration module

65 lines (64 loc) 2.93 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 de_string_1 = require("../modules/import/de-string"); const import_test_1 = require("./import.test"); class DestringTest { constructor() { this.fileIndex = 0; this.destring = new de_string_1.DeString(); this.tstImport = new import_test_1.ImportAPITest(); } execute(files, variables) { try { this.fileIndex = 0; files.reduce((p, x) => p.then(_ => this.executeFile(x, variables)), Promise.resolve()); } catch (e) { console.log('execute error >> ' + e); } } executeFile(filepath, variables) { 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'); const outputCSVPath = path.join(test_config_1.TestConfig.outputPath.csv, 'F' + this.fileIndex + '.csv'); return Promise.all([ this.tstImport.readDataFIle(inputDatasetPath, this.fileIndex, inputJSONPath), this.tstImport.writeCSV(inputDatasetPath, this.fileIndex, inputCSVPath) ]).then(resp => this.destringVariables(resp, outputCSVPath, variables, outputJSONPath)) .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); }); } destringVariables(resp, outPath, listOfVariables, outJsonPath) { 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; const variables = resp[0].data.variables; return this.destring.destringVariables(variables, listOfVariables, csvFilepath, outPath).then((resp) => { if (resp.result === 'ok') { fs.writeFileSync(outJsonPath, JSON.stringify(resp.data, null, 4)); return resp; } else { return resp; } }); } } } exports.DestringTest = DestringTest;