@wbg-mde/r-factory
Version:
Metadata editor R integration module
95 lines (94 loc) • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const test_config_1 = require("./test-config");
const fs = require("fs");
const path = require("path");
const export_dictionary_1 = require("../modules/export/export-dictionary");
const import_test_1 = require("./import.test");
class DictionaryTest {
constructor() {
this.fileIndex = 0;
this.dictionary = new export_dictionary_1.ExportDataDictionary();
this.tstImport = new import_test_1.ImportAPITest();
}
execute(type) {
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, type)), Promise.resolve());
}
});
}
catch (e) {
console.log('execute error >> ' + e);
}
}
executeFile(filepath, type) {
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 dictionaryPath = path.join(test_config_1.TestConfig.dictionaryPath, 'F' + this.fileIndex + '.dat');
return Promise.all([
this.tstImport.readDataFIle(inputDatasetPath, this.fileIndex, inputJSONPath),
this.tstImport.writeCSV(inputDatasetPath, this.fileIndex, inputCSVPath)
]).then(resp => this.exportToDictionary(resp, dictionaryPath, type))
.then((resp) => {
if (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);
});
}
exportToDictionary(resp, outPath, type) {
return new Promise((resolve, reject) => {
try {
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;
switch (type.toUpperCase()) {
case 'STATA':
{
const dictPath = outPath.slice(0, -4) + '.dct';
this.dictionary.exportToDictionaryFormat(dictPath, variables, csvFilepath, '1', '', '', (resp) => {
resolve(resp);
});
}
break;
case 'SPSS':
{
const syntaxPath = outPath.slice(0, -4) + '.sps';
this.dictionary.exportToSPSSSyntaxFormat(syntaxPath, variables, csvFilepath, '1', '', '', (resp) => {
resolve(resp);
});
}
break;
default:
{
this.dictionary.exportFixedWidthText(outPath, variables, csvFilepath, '1', '', '', (resp) => {
resolve(resp);
});
}
break;
}
}
else {
resolve({ "result": "error" });
}
}
catch (e) {
reject(e);
}
});
}
}
exports.DictionaryTest = DictionaryTest;