UNPKG

2.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ndjsonMap = void 0;
4const js_lib_1 = require("@naturalcycles/js-lib");
5const fs_1 = require("fs");
6const path = require("path");
7const zlib_1 = require("zlib");
8const __1 = require("../..");
9/**
10 * Unzips input file automatically, if it ends with `.gz`.
11 * Zips output file automatically, if it ends with `.gz`.
12 */
13async 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 // This is to allow importing *.ts mappers
25 try {
26 require('ts-node/register/transpile-only');
27 require('tsconfig-paths/register');
28 }
29 catch { } // require if exists
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}
55exports.ndjsonMap = ndjsonMap;