UNPKG

@wbg-mde/r-factory

Version:

Metadata editor R integration module

69 lines (68 loc) 2.91 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 re_sequence_1 = require("../modules/import/re-sequence"); const import_test_1 = require("./import.test"); class ResequenceTest { constructor() { this.frequencyLimit = 50; this.fileIndex = 0; this.resequence = new re_sequence_1.ReSequence(); this.tstImport = new import_test_1.ImportAPITest(); } execute() { try { fs.readdir(test_config_1.TestConfig.inputPath.dataset, (err, files) => { if (files) { this.fileIndex = 0; files.reduce((p, x) => p.then(_ => this.executeFile(x)), Promise.resolve()); } }); } catch (e) { console.log('execute error >> ' + e); } } executeFile(filepath) { 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.resequenceDataset(resp, 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); }); } resequenceDataset(resp, outPath) { 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.resequence.resequenceDataset(variables, csvFilepath).then((resp) => { if (resp.result === 'ok') { fs.writeFileSync(outPath, JSON.stringify(resp.data, null, 4)); return resp; } else { return resp; } }); } } } exports.ResequenceTest = ResequenceTest;