dbmaster-cli
Version:
Tool for converting tables between Fifa Soccer Games
85 lines (84 loc) • 3.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamBuilder = void 0;
const byline_1 = require("byline");
const fs_1 = require("fs");
const iconv_lite_1 = require("iconv-lite");
const path_1 = require("path");
const interfaces_1 = require("../interfaces");
const transforms_1 = require("../transforms");
const interfaces_2 = require("./interfaces");
class StreamBuilder {
constructor(inputFolder, table, fields) {
this.inputFolder = inputFolder;
this.table = table;
this.fields = fields;
this.stream = this.init(this.inputFolder, this.table, this.fields);
}
init(inputFolder, table, fields) {
const inputFile = path_1.join(inputFolder, `${table}.txt`);
return fs_1.createReadStream(inputFile)
.pipe(iconv_lite_1.decodeStream('utf16le'))
.pipe(new byline_1.LineStream({ keepEmptyLines: false }))
.pipe(new transforms_1.SkipTransform({ skip: 1 }))
.pipe(new transforms_1.Csv2JsonTransform({ fields }));
}
actionValidate(fields) {
this.stream = this.stream.pipe(new transforms_1.ValidateTransform({ fields }));
return this;
}
actionFilter(filterFn) {
this.stream = this.stream.pipe(new transforms_1.FilterTransform({ filterFn }));
return this;
}
actionAppendDefault(fields) {
this.stream = this.stream.pipe(new transforms_1.AppendDefaultTransform({ fields }));
return this;
}
actionExtendContract(fields, refDate) {
this.stream = this.stream.pipe(new transforms_1.ExtendContractTransform({ fields, refDate }));
return this;
}
actionReindex(startingPos = 0) {
this.stream = this.stream.pipe(new transforms_1.ReindexTransform({ startingPos }));
return this;
}
actionApplyPlayernames(reindexMap, foreingKeyPrimaryColumn = interfaces_1.PLAYERNAMES_PRIMARY_COLUMN, foreignKeyColumns = interfaces_1.playersPlayernamesColumns) {
this.stream = this.stream.pipe(new transforms_1.ApplyPlayernamesTransform({ reindexMap, foreingKeyPrimaryColumn, foreignKeyColumns }));
return this;
}
actionReindexMap2RawData(primaryColumn) {
this.stream = this.stream.pipe(new transforms_1.ReindexMap2RawDataTransform({ primaryColumn }));
return this;
}
actionOnData(onDataFn) {
this.stream = this.stream.on('data', (buffer) => {
const cur = JSON.parse(buffer.toString());
onDataFn(cur);
});
return this;
}
actionWrite(outputFolder, fields, format = interfaces_2.OutputFormat.Csv) {
fs_1.mkdirSync(outputFolder, { recursive: true });
const outputFile = path_1.join(outputFolder, `${this.table}.txt`);
const ws = fs_1.createWriteStream(outputFile, { encoding: 'utf16le' });
if (format === interfaces_2.OutputFormat.Csv) {
this.stream = this.stream.pipe(new transforms_1.Json2CsvTransform({ fields }));
}
this.stream = this.stream.pipe(new transforms_1.NewLineTransform()).pipe(ws);
return this;
}
onData(fn) {
this.stream = this.stream.on('data', fn);
return this;
}
onFinish(fn) {
this.stream = this.stream.on('finish', fn);
return this;
}
onError(fn) {
this.stream = this.stream.on('error', fn);
return this;
}
}
exports.StreamBuilder = StreamBuilder;