@wbg-mde/r-factory
Version:
Metadata editor R integration module
93 lines (92 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const test_config_1 = require("./test-config");
const fs = require("fs");
const path = require("path");
const api_helper_1 = require("../modules/shared/api-helper");
const export_dataset_1 = require("../modules/export/export-dataset");
class ImportAPITest {
constructor() {
this.frequencyLimit = 50;
this.fileIndex = 0;
this.exportDataset = new export_dataset_1.ExportDataset();
}
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 outputDatasetPath = path.join(test_config_1.TestConfig.outputPath.dataset, 'F' + this.fileIndex + '.dta');
const outputCSVPath = path.join(test_config_1.TestConfig.outputPath.csv, 'F' + this.fileIndex + '.csv');
const outputJSONPath = path.join(test_config_1.TestConfig.outputPath.json, 'F' + this.fileIndex + '.json');
return Promise.all([
this.readDataFIle(inputDatasetPath, this.fileIndex, inputJSONPath),
this.writeCSV(inputDatasetPath, this.fileIndex, inputCSVPath)
]).then(resp => this.exportData(resp, outputDatasetPath))
.then((resp) => {
return Promise.all([
this.readDataFIle(outputDatasetPath, this.fileIndex, outputJSONPath),
this.writeCSV(outputDatasetPath, this.fileIndex, outputCSVPath)
]).then((resp) => {
if ((resp[0].data && resp[0].data.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);
});
}
readDataFIle(filepath, index, jsonpath) {
const fileId = 'F' + (index + 1);
const type = path.extname(filepath).slice(1);
return api_helper_1.APIHelper.call('import', 'post', {
freqLimit: this.frequencyLimit,
fileId: fileId,
type,
filepath
}).then((resp) => {
if (resp.data && resp.data.result === 'ok') {
fs.writeFileSync(jsonpath, JSON.stringify(resp.data.variables, null, 4));
return resp;
}
else {
return resp;
}
});
}
writeCSV(filepath, index, csvPath) {
const type = path.extname(filepath).slice(1);
return api_helper_1.APIHelper.call('writeCSV', 'post', {
csvPath,
type,
filepath
});
}
exportData(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.exportDataset.export(outPath, variables, 14, csvFilepath);
}
}
}
exports.ImportAPITest = ImportAPITest;