1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.ndjsonMap = void 0;
|
4 | const js_lib_1 = require("@naturalcycles/js-lib");
|
5 | const fs_1 = require("fs");
|
6 | const path = require("path");
|
7 | const zlib_1 = require("zlib");
|
8 | const __1 = require("../..");
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | async function ndjsonMap(opt) {
|
14 | const { inputFilePath, outputFilePath, mapperFilePath, logEveryInput = 1000, logEveryOutput = 100000, limitInput, limitOutput, } = opt;
|
15 | __1.requireFileToExist(inputFilePath);
|
16 | __1.requireFileToExist(mapperFilePath);
|
17 | const resolvedMapperPath = path.resolve(mapperFilePath);
|
18 | console.log({
|
19 | inputFilePath,
|
20 | outputFilePath,
|
21 | mapperFilePath,
|
22 | resolvedMapperPath,
|
23 | });
|
24 |
|
25 | try {
|
26 | require('ts-node/register/transpile-only');
|
27 | require('tsconfig-paths/register');
|
28 | }
|
29 | catch { }
|
30 | const { mapper } = require(resolvedMapperPath);
|
31 | if (!mapper) {
|
32 | throw new Error(`Mapper file should export "mapper" function`);
|
33 | }
|
34 | const transformUnzip = inputFilePath.endsWith('.gz') ? [zlib_1.createUnzip()] : [];
|
35 | const transformZip = outputFilePath.endsWith('.gz') ? [zlib_1.createGzip()] : [];
|
36 | await __1._pipeline([
|
37 | fs_1.createReadStream(inputFilePath),
|
38 | ...transformUnzip,
|
39 | __1.transformSplit(),
|
40 | __1.transformJsonParse(),
|
41 | __1.transformLimit(limitInput),
|
42 | __1.transformLogProgress({ metric: 'read', logEvery: logEveryInput }),
|
43 | __1.transformMap(mapper, {
|
44 | flattenArrayOutput: true,
|
45 | errorMode: js_lib_1.ErrorMode.SUPPRESS,
|
46 | ...opt,
|
47 | }),
|
48 | __1.transformLimit(limitOutput),
|
49 | __1.transformLogProgress({ metric: 'saved', logEvery: logEveryOutput }),
|
50 | __1.transformToNDJson(),
|
51 | ...transformZip,
|
52 | fs_1.createWriteStream(outputFilePath),
|
53 | ]);
|
54 | }
|
55 | exports.ndjsonMap = ndjsonMap;
|