goods-exporter 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1110 +0,0 @@
1
- 'use strict';
2
-
3
- var utils = require('src/utils');
4
- var stream$3 = require('stream');
5
- var pkg = require('exceljs');
6
- var jsonStreamStringify = require('json-stream-stringify');
7
- var events = require('events');
8
- var fastXmlParser = require('fast-xml-parser');
9
- var fs = require('fs');
10
-
11
- var __defProp$b = Object.defineProperty;
12
- var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
- var __publicField$b = (obj, key, value) => {
14
- __defNormalProp$b(obj, typeof key !== "symbol" ? key + "" : key, value);
15
- return value;
16
- };
17
- class CSVStream {
18
- constructor({ delimiter, lineSeparator, emptyFieldValue }) {
19
- __publicField$b(this, "stream", new stream$3.PassThrough());
20
- __publicField$b(this, "delimiter", ";");
21
- __publicField$b(this, "lineSeparator", "\n");
22
- __publicField$b(this, "emptyFieldValue", "");
23
- __publicField$b(this, "columns", /* @__PURE__ */ new Set());
24
- __publicField$b(this, "writer", utils.writeWithDrain(this.stream));
25
- if (delimiter !== void 0)
26
- this.delimiter = delimiter;
27
- if (lineSeparator !== void 0)
28
- this.lineSeparator = lineSeparator;
29
- if (emptyFieldValue !== void 0)
30
- this.emptyFieldValue = emptyFieldValue;
31
- }
32
- get writableStream() {
33
- return this.stream;
34
- }
35
- setColumns(columns) {
36
- this.columns = columns;
37
- this.stream.write(
38
- Array.from(this.columns).join(this.delimiter) + this.lineSeparator
39
- );
40
- }
41
- async addRow(items) {
42
- const data = Array.from(this.columns).map(
43
- (key) => items[key] === void 0 ? this.emptyFieldValue : items[key] + ""
44
- ).join(this.delimiter) + this.lineSeparator;
45
- await this.writer(data);
46
- }
47
- }
48
-
49
- class FormatterAbstract {
50
- }
51
- var Extension = /* @__PURE__ */ ((Extension2) => {
52
- Extension2["CSV"] = "csv";
53
- Extension2["YML"] = "yml";
54
- Extension2["XML"] = "xml";
55
- Extension2["XLSX"] = "xlsx";
56
- Extension2["JSON"] = "json";
57
- return Extension2;
58
- })(Extension || {});
59
-
60
- var __defProp$a = Object.defineProperty;
61
- var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
62
- var __publicField$a = (obj, key, value) => {
63
- __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
64
- return value;
65
- };
66
- class CSVFormatter {
67
- constructor() {
68
- __publicField$a(this, "formatterName", "CSV");
69
- __publicField$a(this, "fileExtension", Extension.CSV);
70
- }
71
- async format(writableStream, products, categories, _, __) {
72
- const mappedCategories = {};
73
- categories?.forEach(({ id, name }) => mappedCategories[id] = name);
74
- const csvStream = new CSVStream({
75
- delimiter: ";",
76
- emptyFieldValue: "",
77
- lineSeparator: "\n"
78
- });
79
- csvStream.writableStream.pipe(writableStream);
80
- const columns = /* @__PURE__ */ new Set([
81
- "url",
82
- "productId",
83
- "parentId",
84
- "variantId",
85
- "title",
86
- "description",
87
- "vendor",
88
- "vendorCode",
89
- "category",
90
- "images",
91
- "videos",
92
- "timeDeliveryMin",
93
- "timeDeliveryMax",
94
- "price",
95
- "oldPrice",
96
- "purchasePrice",
97
- "currency",
98
- "saleDate",
99
- "countryOfOrigin",
100
- "tags",
101
- "codesTN",
102
- "params",
103
- "properties",
104
- "sizes",
105
- "keywords",
106
- "relatedProducts"
107
- ]);
108
- products.forEach((product) => {
109
- Object.entries(product).forEach(([key, value]) => {
110
- if (value)
111
- columns.add(key);
112
- });
113
- });
114
- csvStream.setColumns(columns);
115
- for (const product of products) {
116
- const row = {
117
- ...product,
118
- category: mappedCategories[product.categoryId],
119
- images: product.images?.join(","),
120
- videos: product.videos?.join(","),
121
- tags: product.tags?.join(","),
122
- codesTN: product.codesTN?.join(", "),
123
- params: product.params?.map(({ key, value }) => `${key}=${value}`).join(", "),
124
- properties: product.properties?.map(({ key, value }) => `${key}=${value}`).join(", "),
125
- sizes: product.sizes?.map(({ name, value }) => `${name}=${value}`).join(", "),
126
- keywords: product.keywords?.join(","),
127
- relatedProducts: product.relatedProducts?.join(","),
128
- timeDeliveryMin: product.timeDelivery?.min,
129
- timeDeliveryMax: product.timeDelivery?.max
130
- };
131
- await csvStream.addRow(row);
132
- }
133
- csvStream.writableStream.end();
134
- }
135
- }
136
-
137
- var __defProp$9 = Object.defineProperty;
138
- var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
139
- var __publicField$9 = (obj, key, value) => {
140
- __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
141
- return value;
142
- };
143
- const { stream: stream$2 } = pkg;
144
- class ExcelFormatter {
145
- constructor() {
146
- __publicField$9(this, "formatterName", "Excel");
147
- __publicField$9(this, "fileExtension", Extension.XLSX);
148
- }
149
- async format(writableStream, products, categories, _, __) {
150
- const mappedCategories = {};
151
- categories?.forEach(({ id, name }) => mappedCategories[id] = name);
152
- const columns = /* @__PURE__ */ new Set([
153
- "url",
154
- "productId",
155
- "parentId",
156
- "variantId",
157
- "title",
158
- "description",
159
- "vendor",
160
- "vendorCode",
161
- "category",
162
- "images",
163
- "videos",
164
- "timeDeliveryMin",
165
- "timeDeliveryMax",
166
- "price",
167
- "oldPrice",
168
- "purchasePrice",
169
- "currency",
170
- "saleDate",
171
- "countryOfOrigin",
172
- "tags",
173
- "codesTN",
174
- "params",
175
- "properties",
176
- "sizes",
177
- "keywords",
178
- "relatedProducts"
179
- ]);
180
- products.forEach((product) => {
181
- Object.entries(product).forEach(([key, value]) => {
182
- if (value)
183
- columns.add(key);
184
- });
185
- });
186
- const workbook = new stream$2.xlsx.WorkbookWriter({
187
- stream: writableStream
188
- });
189
- const worksheet = workbook.addWorksheet("products");
190
- worksheet.columns = Array.from(columns).map((column) => ({
191
- key: column,
192
- header: column
193
- }));
194
- products.forEach((product) => {
195
- const row = {
196
- ...product,
197
- category: mappedCategories[product.categoryId],
198
- images: product.images?.join(","),
199
- videos: product.videos?.join(","),
200
- tags: product.tags?.join(","),
201
- keywords: product.keywords?.join(","),
202
- relatedProducts: product.relatedProducts?.join(","),
203
- codesTN: product.codesTN?.join(", "),
204
- params: product.params?.map(({ key, value }) => `${key}=${value}`).join(", "),
205
- properties: product.properties?.map(({ key, value }) => `${key}=${value}`).join(", "),
206
- sizes: product.sizes?.map(({ name, value }) => `${name}=${value}`).join(", "),
207
- timeDeliveryMin: product.timeDelivery?.min,
208
- timeDeliveryMax: product.timeDelivery?.max
209
- };
210
- worksheet.addRow(row).commit();
211
- });
212
- worksheet.commit();
213
- await workbook.commit();
214
- }
215
- }
216
-
217
- var __defProp$8 = Object.defineProperty;
218
- var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
219
- var __publicField$8 = (obj, key, value) => {
220
- __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
221
- return value;
222
- };
223
- const { stream: stream$1 } = pkg;
224
- class InsalesFormatter {
225
- constructor() {
226
- __publicField$8(this, "formatterName", "Insales");
227
- __publicField$8(this, "fileExtension", Extension.XLSX);
228
- }
229
- async format(writableStream, products, categories, _, __) {
230
- const mappedCategories = {};
231
- categories?.forEach(
232
- (category) => mappedCategories[category.id] = category
233
- );
234
- const getParams = (product) => {
235
- const properties = {};
236
- product.params?.forEach(
237
- (p) => properties[`\u0421\u0432\u043E\u0439\u0441\u0442\u0432\u043E: ${p.key}`] = p.value
238
- );
239
- return properties;
240
- };
241
- const getProperties = (product) => {
242
- const properties = {};
243
- product.properties?.forEach(
244
- (p) => properties[`\u041F\u0430\u0440\u0430\u043C\u0435\u0442\u0440: ${p.key}`] = p.value
245
- );
246
- return properties;
247
- };
248
- const getCategories = (product) => {
249
- const categories2 = {};
250
- const categoryList = new Array();
251
- function addCategory(categoryId) {
252
- if (categoryId === void 0)
253
- return;
254
- const category = mappedCategories[categoryId];
255
- if (category) {
256
- categoryList.push(category.name);
257
- addCategory(category.parentId);
258
- }
259
- }
260
- addCategory(product.categoryId);
261
- categoryList.forEach((name, i) => {
262
- const index = categoryList.length - 1 - i;
263
- const key = index === 0 ? "\u041A\u043E\u0440\u043D\u0435\u0432\u0430\u044F" : `\u041F\u043E\u0434\u043A\u0430\u0442\u0435\u0433\u043E\u0440\u0438\u044F ${index}`;
264
- categories2[key] = name;
265
- });
266
- return categories2;
267
- };
268
- const workbook = new stream$1.xlsx.WorkbookWriter({
269
- stream: writableStream
270
- });
271
- const worksheet = workbook.addWorksheet("products");
272
- const columns = /* @__PURE__ */ new Set([
273
- "\u0412\u043D\u0435\u0448\u043D\u0438\u0439 ID",
274
- "\u0421\u0441\u044B\u043B\u043A\u0430 \u043D\u0430 \u0442\u043E\u0432\u0430\u0440",
275
- "\u0410\u0440\u0442\u0438\u043A\u0443\u043B",
276
- "\u041A\u043E\u0440\u043D\u0435\u0432\u0430\u044F",
277
- "\u041F\u043E\u0434\u043A\u0430\u0442\u0435\u0433\u043E\u0440\u0438\u044F 1",
278
- "\u041F\u043E\u0434\u043A\u0430\u0442\u0435\u0433\u043E\u0440\u0438\u044F 2",
279
- "\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u0442\u043E\u0432\u0430\u0440\u0430 \u0438\u043B\u0438 \u0443\u0441\u043B\u0443\u0433\u0438",
280
- "\u0412\u0440\u0435\u043C\u044F \u0434\u043E\u0441\u0442\u0430\u0432\u043A\u0438: \u041C\u0438\u043D\u0438\u043C\u0430\u043B\u044C\u043D\u043E\u0435",
281
- "\u0412\u0440\u0435\u043C\u044F \u0434\u043E\u0441\u0442\u0430\u0432\u043A\u0438: \u041C\u0430\u043A\u0441\u0438\u043C\u0430\u043B\u044C\u043D\u043E\u0435",
282
- "\u0421\u0442\u0430\u0440\u0430\u044F \u0446\u0435\u043D\u0430",
283
- "\u0426\u0435\u043D\u0430 \u043F\u0440\u043E\u0434\u0430\u0436\u0438",
284
- "C\u0435\u0431\u0435\u0441\u0442\u043E\u0438\u043C\u043E\u0441\u0442\u044C",
285
- "\u041A\u0430\u0442\u0435\u0433\u043E\u0440\u0438\u0438",
286
- "\u041E\u0441\u0442\u0430\u0442\u043E\u043A",
287
- "\u0428\u0442\u0440\u0438\u0445-\u043A\u043E\u0434",
288
- "\u041A\u0440\u0430\u0442\u043A\u043E\u0435 \u043E\u043F\u0438\u0441\u0430\u043D\u0438\u0435",
289
- "\u041F\u043E\u043B\u043D\u043E\u0435 \u043E\u043F\u0438\u0441\u0430\u043D\u0438\u0435",
290
- "\u0413\u0430\u0431\u0430\u0440\u0438\u0442\u044B \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u0430",
291
- "\u0412\u0435\u0441",
292
- "\u0420\u0430\u0437\u043C\u0435\u0449\u0435\u043D\u0438\u0435 \u043D\u0430 \u0441\u0430\u0439\u0442\u0435",
293
- "\u041D\u0414\u0421",
294
- "\u0412\u0430\u043B\u044E\u0442\u0430 \u0441\u043A\u043B\u0430\u0434\u0430",
295
- "\u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u044F \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u0430",
296
- "\u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u044F",
297
- "\u0421\u0441\u044B\u043B\u043A\u0430 \u043D\u0430 \u0432\u0438\u0434\u0435\u043E",
298
- "\u041F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u044B",
299
- "\u0421\u0432\u043E\u0439\u0441\u0442\u0432\u0430",
300
- "\u0420\u0430\u0437\u043C\u0435\u0440\u043D\u0430\u044F \u0441\u0435\u0442\u043A\u0430",
301
- "\u0421\u0432\u044F\u0437\u0430\u043D\u043D\u044B\u0435 \u0442\u043E\u0432\u0430\u0440\u044B",
302
- "\u041A\u043B\u044E\u0447\u0435\u0432\u044B\u0435 \u0441\u043B\u043E\u0432\u0430"
303
- ]);
304
- products.forEach((product) => {
305
- Object.keys({
306
- ...getParams(product),
307
- ...getProperties(product)
308
- }).forEach((key) => {
309
- columns.add(key);
310
- });
311
- });
312
- worksheet.columns = Array.from(columns).map((column) => ({
313
- header: column,
314
- key: column
315
- }));
316
- for (const product of products) {
317
- const externalId = `${product.productId}-${product.variantId}`;
318
- const row = {
319
- "\u0412\u043D\u0435\u0448\u043D\u0438\u0439 ID": externalId,
320
- "\u0421\u0441\u044B\u043B\u043A\u0430 \u043D\u0430 \u0442\u043E\u0432\u0430\u0440": product.url,
321
- \u0410\u0440\u0442\u0438\u043A\u0443\u043B: product.vendorCode,
322
- "\u041D\u0430\u0437\u0432\u0430\u043D\u0438\u0435 \u0442\u043E\u0432\u0430\u0440\u0430 \u0438\u043B\u0438 \u0443\u0441\u043B\u0443\u0433\u0438": product.title,
323
- "\u0412\u0440\u0435\u043C\u044F \u0434\u043E\u0441\u0442\u0430\u0432\u043A\u0438: \u041C\u0438\u043D\u0438\u043C\u0430\u043B\u044C\u043D\u043E\u0435": product.timeDelivery?.min,
324
- "\u0412\u0440\u0435\u043C\u044F \u0434\u043E\u0441\u0442\u0430\u0432\u043A\u0438: \u041C\u0430\u043A\u0441\u0438\u043C\u0430\u043B\u044C\u043D\u043E\u0435": product.timeDelivery?.max,
325
- "\u0421\u0442\u0430\u0440\u0430\u044F \u0446\u0435\u043D\u0430": product.oldPrice,
326
- "\u0426\u0435\u043D\u0430 \u043F\u0440\u043E\u0434\u0430\u0436\u0438": product.price,
327
- C\u0435\u0431\u0435\u0441\u0442\u043E\u0438\u043C\u043E\u0441\u0442\u044C: product.purchasePrice,
328
- ...getCategories(product),
329
- \u041E\u0441\u0442\u0430\u0442\u043E\u043A: product.count,
330
- "\u0428\u0442\u0440\u0438\u0445-\u043A\u043E\u0434": product.barcode,
331
- "\u041A\u0440\u0430\u0442\u043A\u043E\u0435 \u043E\u043F\u0438\u0441\u0430\u043D\u0438\u0435": void 0,
332
- "\u041F\u043E\u043B\u043D\u043E\u0435 \u043E\u043F\u0438\u0441\u0430\u043D\u0438\u0435": product.description,
333
- "\u0413\u0430\u0431\u0430\u0440\u0438\u0442\u044B \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u0430": product.dimensions,
334
- \u0412\u0435\u0441: product.weight,
335
- "\u0420\u0430\u0437\u043C\u0435\u0449\u0435\u043D\u0438\u0435 \u043D\u0430 \u0441\u0430\u0439\u0442\u0435": product.available,
336
- \u041D\u0414\u0421: product.vat?.toString(),
337
- "\u0412\u0430\u043B\u044E\u0442\u0430 \u0441\u043A\u043B\u0430\u0434\u0430": product.currency.toString(),
338
- "\u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u044F \u0432\u0430\u0440\u0438\u0430\u043D\u0442\u0430": product.parentId === void 0 ? product.images?.join(" ") : void 0,
339
- \u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u044F: product.parentId === void 0 ? void 0 : product.images?.join(" "),
340
- "\u0421\u0441\u044B\u043B\u043A\u0430 \u043D\u0430 \u0432\u0438\u0434\u0435\u043E": product.videos ? product.videos[0] : void 0,
341
- ...getParams(product),
342
- ...getProperties(product),
343
- "\u0420\u0430\u0437\u043C\u0435\u0440\u043D\u0430\u044F \u0441\u0435\u0442\u043A\u0430": JSON.stringify(product.sizes),
344
- "\u0421\u0432\u044F\u0437\u0430\u043D\u043D\u044B\u0435 \u0442\u043E\u0432\u0430\u0440\u044B": product.relatedProducts?.join(","),
345
- "\u041A\u043B\u044E\u0447\u0435\u0432\u044B\u0435 \u0441\u043B\u043E\u0432\u0430": product.keywords?.join(",")
346
- };
347
- worksheet.addRow(row).commit();
348
- }
349
- worksheet.commit();
350
- await workbook.commit();
351
- }
352
- }
353
-
354
- var __defProp$7 = Object.defineProperty;
355
- var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
356
- var __publicField$7 = (obj, key, value) => {
357
- __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
358
- return value;
359
- };
360
- class JSONFormatter {
361
- constructor() {
362
- __publicField$7(this, "formatterName", "JSON");
363
- __publicField$7(this, "fileExtension", Extension.JSON);
364
- }
365
- async format(writableStream, products, categories, brands, _) {
366
- const stream = new jsonStreamStringify.JsonStreamStringify({
367
- categories,
368
- brands,
369
- products
370
- });
371
- stream.pipe(writableStream);
372
- }
373
- }
374
-
375
- var __defProp$6 = Object.defineProperty;
376
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
377
- var __publicField$6 = (obj, key, value) => {
378
- __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
379
- return value;
380
- };
381
- class SimpleJSONFormatter {
382
- constructor() {
383
- __publicField$6(this, "formatterName", "JSON");
384
- __publicField$6(this, "fileExtension", Extension.JSON);
385
- }
386
- async format(writableStream, products, categories, brands, _) {
387
- const groupedProduct = /* @__PURE__ */ new Map();
388
- products.forEach((product) => {
389
- if (product.parentId !== void 0)
390
- return;
391
- groupedProduct.set(product.variantId, {
392
- ...product,
393
- children: []
394
- });
395
- });
396
- products.forEach((product) => {
397
- if (product.parentId === void 0)
398
- return;
399
- const parent = groupedProduct.get(product.parentId);
400
- if (!parent)
401
- return;
402
- parent.children.push(product);
403
- });
404
- const stream = new jsonStreamStringify.JsonStreamStringify({
405
- categories,
406
- brands,
407
- products: Array.from(groupedProduct.values())
408
- });
409
- stream.pipe(writableStream);
410
- }
411
- }
412
-
413
- var __defProp$5 = Object.defineProperty;
414
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
415
- var __publicField$5 = (obj, key, value) => {
416
- __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
417
- return value;
418
- };
419
- const { stream } = pkg;
420
- class TgShopFormatter {
421
- constructor() {
422
- __publicField$5(this, "formatterName", "TgShop");
423
- __publicField$5(this, "fileExtension", Extension.XLSX);
424
- }
425
- async format(writableStream, products, categories, _, __) {
426
- const getParameter = (product, key) => product.params?.find((value) => value.key === key);
427
- const convertProduct = (product) => ({
428
- "category id": product.categoryId,
429
- "group id": product.parentId,
430
- "id product": product.variantId,
431
- "name product": product.title,
432
- price: product.price,
433
- picture: product.images?.join(", "),
434
- vendorCode: product.vendorCode,
435
- oldprice: product.oldPrice,
436
- description: product.description,
437
- shortDescription: "",
438
- quantityInStock: product.count,
439
- color: getParameter(product, "color")?.value,
440
- size: getParameter(product, "size")?.value,
441
- priority: void 0
442
- });
443
- const workbook = new stream.xlsx.WorkbookWriter({
444
- stream: writableStream
445
- });
446
- const categoryWorksheet = workbook.addWorksheet("categories");
447
- const productsWorksheet = workbook.addWorksheet("offers");
448
- categoryWorksheet.columns = [
449
- {
450
- header: "id",
451
- key: "id"
452
- },
453
- {
454
- header: "parentId",
455
- key: "parentId"
456
- },
457
- {
458
- header: "name",
459
- key: "name"
460
- }
461
- ];
462
- const columns = [
463
- "category id",
464
- "group id",
465
- "id product",
466
- "name product",
467
- "price",
468
- "picture",
469
- "vendorCode",
470
- "oldprice",
471
- "description",
472
- "shortDescription",
473
- "quantityInStock",
474
- "color",
475
- "size",
476
- "priority"
477
- ];
478
- productsWorksheet.columns = columns.map((column) => ({
479
- header: column,
480
- key: column
481
- }));
482
- categories?.forEach((category) => {
483
- categoryWorksheet.addRow(category).commit();
484
- });
485
- products.forEach((product) => {
486
- productsWorksheet.addRow(convertProduct(product)).commit();
487
- });
488
- categoryWorksheet.commit();
489
- productsWorksheet.commit();
490
- await workbook.commit();
491
- }
492
- }
493
-
494
- var __defProp$4 = Object.defineProperty;
495
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
496
- var __publicField$4 = (obj, key, value) => {
497
- __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
498
- return value;
499
- };
500
- class TildaFormatter {
501
- constructor() {
502
- __publicField$4(this, "formatterName", "Tilda");
503
- __publicField$4(this, "fileExtension", Extension.CSV);
504
- }
505
- async format(writableStream, products, categories, _, __) {
506
- const mappedCategories = {};
507
- categories?.forEach(({ id, name }) => mappedCategories[id] = name);
508
- const csvStream = new CSVStream({
509
- delimiter: ";",
510
- emptyFieldValue: "",
511
- lineSeparator: "\n"
512
- });
513
- csvStream.writableStream.pipe(writableStream);
514
- const columns = /* @__PURE__ */ new Set([
515
- "SKU",
516
- "Brand",
517
- "Category",
518
- "Title",
519
- "Text",
520
- "Photo",
521
- "Price",
522
- "Price Old",
523
- "Quantity",
524
- "Editions",
525
- "External ID",
526
- "Parent UID"
527
- ]);
528
- csvStream.setColumns(columns);
529
- for (const product of products) {
530
- const row = {
531
- SKU: product.vendorCode,
532
- Brand: product.vendor,
533
- Category: mappedCategories[product.categoryId],
534
- Title: product.title,
535
- Text: product.description,
536
- Photo: product.images?.join(";"),
537
- Price: product.price,
538
- "Price Old": product.oldPrice,
539
- Quantity: product.count,
540
- Editions: product.params?.map(({ key, value }) => `${key}:${value}`).join(";"),
541
- "External ID": product.variantId,
542
- "Parent UID": product.parentId
543
- };
544
- await csvStream.addRow(row);
545
- }
546
- csvStream.writableStream.end();
547
- }
548
- }
549
-
550
- const buildCategoryPaths = (categories) => {
551
- const idToCategory = /* @__PURE__ */ new Map();
552
- categories.forEach((category) => {
553
- idToCategory.set(category.id, category);
554
- });
555
- const categoryPaths = /* @__PURE__ */ new Map();
556
- categories.forEach((category) => {
557
- const path = [];
558
- let currentCategory = category;
559
- while (currentCategory) {
560
- path.unshift(currentCategory);
561
- if (currentCategory.parentId !== void 0) {
562
- currentCategory = idToCategory.get(currentCategory.parentId);
563
- } else {
564
- currentCategory = void 0;
565
- }
566
- }
567
- categoryPaths.set(category.id, path);
568
- });
569
- return categoryPaths;
570
- };
571
-
572
- const writeWithDrain = (stream) => {
573
- return async (chunk) => {
574
- const canWrite = stream.write(chunk);
575
- if (!canWrite) {
576
- await events.once(stream, "drain");
577
- }
578
- };
579
- };
580
-
581
- const delay = async (ms) => await new Promise((resolve) => setTimeout(resolve, ms));
582
-
583
- const urlQueryEncode = (inputUrl) => {
584
- try {
585
- const url = new URL(inputUrl);
586
- url.search = url.search.replace(/^\?/, "").replace(/,/g, "%2C");
587
- return url.toString();
588
- } catch (error) {
589
- console.error("Invalid URL:", error);
590
- return "";
591
- }
592
- };
593
-
594
- var __defProp$3 = Object.defineProperty;
595
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
596
- var __publicField$3 = (obj, key, value) => {
597
- __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
598
- return value;
599
- };
600
- class WooCommerceFormatter {
601
- constructor() {
602
- __publicField$3(this, "formatterName", "WooCommerce");
603
- __publicField$3(this, "fileExtension", Extension.CSV);
604
- __publicField$3(this, "DEFAULT_COLUMNS", [
605
- "ID",
606
- "Type",
607
- "SKU",
608
- "Name",
609
- "Parent",
610
- "Short description",
611
- "Description",
612
- "Stock",
613
- "Regular price",
614
- "Position",
615
- "Categories",
616
- "Tags",
617
- "Images",
618
- "SizeGrid"
619
- ]);
620
- }
621
- formatKeyAttribute(key) {
622
- const keyWithoutInvalidCharacters = key.replaceAll(",", "").replaceAll(";", "");
623
- return keyWithoutInvalidCharacters.length >= 28 ? keyWithoutInvalidCharacters.slice(0, 24) + "..." : keyWithoutInvalidCharacters;
624
- }
625
- formatValueAttribute(value) {
626
- return value.replaceAll(",", "").replaceAll(";", "");
627
- }
628
- formatProducts(products) {
629
- const formatParams = (params) => {
630
- return params?.map(({ key, value }) => {
631
- const formatedKey = this.formatKeyAttribute(key);
632
- const formatedValue = this.formatValueAttribute(value);
633
- return { key: formatedKey, value: formatedValue };
634
- });
635
- };
636
- return products.map((product) => {
637
- const params = formatParams(product.params);
638
- const properties = formatParams(product.properties);
639
- return { ...product, params, properties };
640
- });
641
- }
642
- createAttribute(data) {
643
- if (!data?.name || data.id === void 0)
644
- return;
645
- const attributeStartName = "Attribute";
646
- const attribute = {};
647
- attribute[`${attributeStartName} ${data.id} name`] = data.name;
648
- if (data.values !== void 0)
649
- attribute[`${attributeStartName} ${data.id} value(s)`] = data.values;
650
- if (data.visible !== void 0)
651
- attribute[`${attributeStartName} ${data.id} visible`] = data.visible;
652
- if (data.global !== void 0)
653
- attribute[`${attributeStartName} ${data.id} global`] = data.global;
654
- return attribute;
655
- }
656
- extractAttributes(products) {
657
- const formatedProducts = this.formatProducts(products);
658
- const paramsMap = /* @__PURE__ */ new Map();
659
- const propertiesMap = /* @__PURE__ */ new Map();
660
- const uniqueAttributes = /* @__PURE__ */ new Map();
661
- const genderTitle = "\u041F\u043E\u043B";
662
- formatedProducts.forEach((product) => {
663
- product.params?.forEach(({ key }) => {
664
- if (!uniqueAttributes.has(key)) {
665
- uniqueAttributes.set(key, uniqueAttributes.size);
666
- }
667
- });
668
- uniqueAttributes.set(genderTitle, uniqueAttributes.size);
669
- product.properties?.forEach(({ key }) => {
670
- if (!uniqueAttributes.has(key)) {
671
- uniqueAttributes.set(key, uniqueAttributes.size);
672
- }
673
- });
674
- });
675
- formatedProducts.forEach((product) => {
676
- const paramAttributes = paramsMap.get(product.variantId) ?? {};
677
- const propertyAttributes = propertiesMap.get(product.variantId) ?? {};
678
- product.params?.forEach(({ key, value }) => {
679
- const index = uniqueAttributes.get(key);
680
- if (index === void 0) {
681
- console.error(`\u041D\u0435 \u043D\u0430\u0448\u043B\u043E\u0441\u044C \u0443\u043D\u0438\u043A\u0430\u043B\u044C\u043D\u043E\u0433\u043E \u043A\u043B\u044E\u0447\u0430 \u0434\u043B\u044F \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u0430 - ${key}`);
682
- return;
683
- }
684
- const attribute = this.createAttribute({
685
- name: key,
686
- id: index,
687
- values: value,
688
- visible: 0,
689
- global: 0
690
- });
691
- if (!attribute)
692
- return;
693
- Object.entries(attribute).forEach(
694
- ([key2, value2]) => paramAttributes[key2] = value2
695
- );
696
- });
697
- const genderIndex = uniqueAttributes.get(genderTitle);
698
- const genderAttribute = this.createAttribute({
699
- name: genderTitle,
700
- id: genderIndex,
701
- values: product.gender,
702
- global: 0
703
- });
704
- if (genderAttribute) {
705
- Object.entries(genderAttribute).forEach(
706
- ([key, value]) => propertyAttributes[key] = value
707
- );
708
- }
709
- product.properties?.forEach(({ key, value }) => {
710
- const index = uniqueAttributes.get(key);
711
- if (index === void 0) {
712
- console.error(`\u041D\u0435 \u043D\u0430\u0448\u043B\u043E\u0441\u044C \u0443\u043D\u0438\u043A\u0430\u043B\u044C\u043D\u043E\u0433\u043E \u043A\u043B\u044E\u0447\u0430 \u0434\u043B\u044F \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u0430 - ${key}`);
713
- return;
714
- }
715
- if (paramAttributes[`Attribute ${index} name`]) {
716
- console.warn(`\u0414\u0430\u043D\u043D\u043E\u0435 \u0441\u0432\u043E\u0439\u0441\u0442\u0432\u043E \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u043F\u0430\u0440\u0430\u043C\u0435\u0442\u0440\u0430\u0445 - ${key}`);
717
- return;
718
- }
719
- const attribute = this.createAttribute({
720
- name: key,
721
- id: index,
722
- values: value,
723
- global: 0
724
- });
725
- if (!attribute)
726
- return;
727
- Object.entries(attribute).forEach(
728
- ([key2, value2]) => propertyAttributes[key2] = value2
729
- );
730
- });
731
- paramsMap.set(product.variantId, paramAttributes);
732
- propertiesMap.set(product.variantId, propertyAttributes);
733
- });
734
- return { params: paramsMap, properties: propertiesMap };
735
- }
736
- removeVisibleFromAttributes(params) {
737
- Object.entries(params).forEach(([key]) => {
738
- if (key.includes("visible"))
739
- params[key] = "";
740
- });
741
- }
742
- async format(writableStream, products, categories, _, __) {
743
- const categoryPaths = buildCategoryPaths(categories ?? []);
744
- const csvStream = new CSVStream({
745
- delimiter: ";",
746
- emptyFieldValue: "",
747
- lineSeparator: "\n"
748
- });
749
- csvStream.writableStream.pipe(writableStream);
750
- const columns = new Set(this.DEFAULT_COLUMNS);
751
- const attributes = this.extractAttributes(products);
752
- const variationsByParentId = /* @__PURE__ */ new Map();
753
- const imagesByParentId = /* @__PURE__ */ new Map();
754
- const sizesByParentId = /* @__PURE__ */ new Map();
755
- const variations = products.map((product, index) => {
756
- const pathsArray = categoryPaths.get(product.categoryId)?.map((category) => category.name);
757
- const price = product.price ? product.price : "";
758
- let row = {
759
- ID: product.variantId,
760
- Type: "variation",
761
- SKU: product.variantId,
762
- Name: product.title,
763
- Parent: product.parentId ?? 0,
764
- "Short description": "",
765
- Description: product.description,
766
- Stock: product.count ?? 0,
767
- "Regular price": price,
768
- Position: index + 1,
769
- Categories: pathsArray?.join(" > "),
770
- Tags: product.keywords?.join(","),
771
- Images: "",
772
- SizeGrid: ""
773
- };
774
- const productParams = attributes.params.get(product.variantId) ?? {};
775
- if (!imagesByParentId.has(row.Parent)) {
776
- const images = product.images?.map(urlQueryEncode).join(",");
777
- imagesByParentId.set(row.Parent, images);
778
- }
779
- if (!sizesByParentId.has(row.Parent)) {
780
- sizesByParentId.set(
781
- row.Parent,
782
- product.sizes ? JSON.stringify(product.sizes) : ""
783
- );
784
- }
785
- this.removeVisibleFromAttributes(productParams);
786
- row = { ...row, ...productParams };
787
- if (variationsByParentId.has(row.Parent)) {
788
- variationsByParentId.get(row.Parent)?.push(row);
789
- } else {
790
- variationsByParentId.set(row.Parent, [row]);
791
- }
792
- return row;
793
- });
794
- const parentProducts = /* @__PURE__ */ new Map();
795
- variations.forEach((product) => {
796
- const currentParent = parentProducts.get(product.Parent);
797
- let row = {
798
- ...product,
799
- Type: "variable",
800
- ID: product.Parent,
801
- SKU: product.Parent,
802
- Position: 0,
803
- Parent: "",
804
- "Regular price": "",
805
- Images: imagesByParentId.get(product.Parent) ?? "",
806
- SizeGrid: sizesByParentId.get(product.Parent) ?? ""
807
- };
808
- row.Stock = (currentParent?.Stock || 0) + (product?.Stock || 0);
809
- const productParams = attributes.params.get(product.SKU) ?? {};
810
- const productProperties = attributes.properties.get(product.SKU) ?? {};
811
- Object.entries(productParams).forEach(([key]) => {
812
- if (key.includes("visible"))
813
- productParams[key] = 0;
814
- });
815
- if (currentParent) {
816
- Object.entries(productParams).forEach(([key, value]) => {
817
- if (key.includes("value(s)")) {
818
- productParams[key] = currentParent[key] + `, ${value}`;
819
- }
820
- });
821
- }
822
- Object.keys({ ...row, ...productParams, ...productProperties }).forEach(
823
- (item) => columns.add(item)
824
- );
825
- row = { ...row, ...productParams, ...productProperties };
826
- parentProducts.set(product.Parent, row);
827
- });
828
- const variableProducts = Array.from(parentProducts.values());
829
- csvStream.setColumns(columns);
830
- for (const parentProduct of variableProducts) {
831
- await csvStream.addRow(parentProduct);
832
- for (const variationProduct of variationsByParentId.get(
833
- parentProduct.ID
834
- ) ?? []) {
835
- await csvStream.addRow(variationProduct);
836
- }
837
- }
838
- csvStream.writableStream.end();
839
- }
840
- }
841
-
842
- var __defProp$2 = Object.defineProperty;
843
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
844
- var __publicField$2 = (obj, key, value) => {
845
- __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
846
- return value;
847
- };
848
- class YMLFormatter {
849
- constructor() {
850
- __publicField$2(this, "formatterName", "YMl");
851
- __publicField$2(this, "fileExtension", Extension.YML);
852
- }
853
- async format(writableStream, products, categories, brands, options) {
854
- const result = new stream$3.PassThrough();
855
- result.pipe(writableStream);
856
- const builder = new fastXmlParser.XMLBuilder({
857
- ignoreAttributes: false,
858
- cdataPropName: "__cdata",
859
- format: true,
860
- indentBy: " "
861
- });
862
- const date = (/* @__PURE__ */ new Date()).toISOString().replace(/.\d+Z/, "");
863
- result.write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n');
864
- result.write('<yml_catalog date="' + date + '">\n');
865
- result.write("<shop>\n");
866
- const resultWriter = utils.writeWithDrain(result);
867
- if (options?.shopName) {
868
- await resultWriter(builder.build({ name: options.shopName }));
869
- await resultWriter("\n");
870
- }
871
- if (options?.companyName) {
872
- await resultWriter(builder.build({ company: options.companyName }));
873
- await resultWriter("\n");
874
- }
875
- if (categories) {
876
- await resultWriter(
877
- builder.build({
878
- // tagname: "categories",
879
- categories: { category: this.getCategories(categories) }
880
- })
881
- );
882
- await resultWriter("\n");
883
- }
884
- if (brands) {
885
- await resultWriter(
886
- builder.build({ brands: { brand: this.getBrands(brands) } })
887
- );
888
- await resultWriter("\n");
889
- }
890
- await resultWriter("<offers>\n");
891
- const offerStream = new stream$3.PassThrough();
892
- const offerWriter = utils.writeWithDrain(offerStream);
893
- offerStream.pipe(result, { end: false });
894
- for (const product of products) {
895
- const offer = builder.build({ offer: this.getOffer(product) });
896
- await offerWriter(offer + "\n");
897
- }
898
- offerStream.end();
899
- offerStream.on("end", () => {
900
- result.write("</offers>\n");
901
- result.write("</shop>\n");
902
- result.write("</yml_catalog>\n");
903
- result.end();
904
- });
905
- }
906
- getBrands(brands) {
907
- if (!brands)
908
- return [];
909
- return brands.map((brand) => ({
910
- "@_id": brand.id,
911
- "@_url": brand.coverURL ?? "",
912
- "#text": brand.name
913
- }));
914
- }
915
- getCategories(categories) {
916
- if (!categories)
917
- return [];
918
- return categories.map((cat) => ({
919
- "@_id": cat.id,
920
- "@_parentId": cat.parentId ?? "",
921
- "#text": cat.name
922
- }));
923
- }
924
- getOffer(product) {
925
- const result = {
926
- "@_id": product.variantId,
927
- name: product.title,
928
- price: product.price,
929
- oldprice: product.oldPrice,
930
- purchase_price: product.purchasePrice,
931
- additional_expenses: product.additionalExpenses,
932
- cofinance_price: product.cofinancePrice,
933
- currencyId: product.currency,
934
- categoryId: product.categoryId,
935
- vendorId: product.vendorId,
936
- vendor: product.vendor,
937
- vendorCode: product.vendorCode,
938
- picture: product.images,
939
- video: product.videos,
940
- available: product.available,
941
- "time-delivery": product.timeDelivery ? {
942
- "@_min": product.timeDelivery.min,
943
- "@_max": product.timeDelivery.max,
944
- "#text": `${product.timeDelivery.min}-${product.timeDelivery.max}`
945
- } : void 0,
946
- series: product.seriesName,
947
- "min-quantity": product.minQuantity,
948
- "step-quantity": product.stepQuantity,
949
- size: product.sizes?.map((size) => ({
950
- "#text": size.value,
951
- "@_name": size.name,
952
- "@_delimiter": size.delimiter
953
- })),
954
- keyword: product.keywords,
955
- saleDate: product.saleDate,
956
- property: product.properties?.map((property) => ({
957
- "#text": property.value,
958
- "@_name": property.key
959
- })),
960
- param: product.params?.map((param) => ({
961
- "#text": param.value,
962
- "@_name": param.key
963
- })),
964
- description: {
965
- __cdata: product.description
966
- },
967
- country_of_origin: product.countryOfOrigin,
968
- barcode: product.barcode,
969
- vat: product.vat,
970
- count: product.count,
971
- "set-ids": product.tags?.join(", "),
972
- adult: product.adult,
973
- downloadable: product.downloadable,
974
- "period-of-validity-days": product.validityPeriod,
975
- "comment-validity-days": product.validityComment,
976
- "service-life-days": product.serviceLifePeriod,
977
- "comment-life-days": product.serviceLifeComment,
978
- "warranty-days": product.warrantyPeriod,
979
- "comment-warranty": product.warrantyComment,
980
- manufacturer_warranty: product.manufacturerWarranty,
981
- certificate: product.certificate,
982
- url: product.url,
983
- weight: product.weight,
984
- dimensions: product.dimensions,
985
- boxCount: product.boxCount,
986
- disabled: product.disabled,
987
- age: product.age != null && {
988
- "@_unit": product.age.unit,
989
- "#text": product.age.value
990
- },
991
- "tn-ved-codes": product.codesTN?.length != null && {
992
- "tn-ved-code": product.codesTN
993
- },
994
- relatedProduct: product.relatedProducts,
995
- gender: product.gender
996
- };
997
- if (product.parentId !== void 0) {
998
- return {
999
- ...result,
1000
- "@_group_id": product.parentId
1001
- };
1002
- }
1003
- return result;
1004
- }
1005
- }
1006
-
1007
- var __defProp$1 = Object.defineProperty;
1008
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1009
- var __publicField$1 = (obj, key, value) => {
1010
- __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
1011
- return value;
1012
- };
1013
- class XMLFormatter extends YMLFormatter {
1014
- constructor() {
1015
- super(...arguments);
1016
- __publicField$1(this, "formatterName", "XML");
1017
- __publicField$1(this, "fileExtension", Extension.XML);
1018
- }
1019
- }
1020
-
1021
- const Formatters = {
1022
- TildaFormatter,
1023
- CSVFormatter,
1024
- InsalesFormatter,
1025
- YMLFormatter,
1026
- TgShopFormatter,
1027
- ExcelFormatter,
1028
- JSONFormatter,
1029
- SimpleJSONFormatter,
1030
- XMLFormatter,
1031
- WooCommerceFormatter
1032
- };
1033
-
1034
- var __defProp = Object.defineProperty;
1035
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1036
- var __publicField = (obj, key, value) => {
1037
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1038
- return value;
1039
- };
1040
- class GoodsExporter {
1041
- constructor(context) {
1042
- this.context = context;
1043
- __publicField(this, "_context");
1044
- __publicField(this, "formatter", new Formatters.YMLFormatter());
1045
- __publicField(this, "exporter", () => {
1046
- return fs.createWriteStream(
1047
- `${this.formatter.formatterName}.output.${this.formatter.fileExtension}`
1048
- );
1049
- });
1050
- __publicField(this, "transformers", new Array());
1051
- this._context = context;
1052
- }
1053
- setContext(context) {
1054
- this._context = context;
1055
- }
1056
- setTransformers(transformers) {
1057
- this.transformers = transformers;
1058
- }
1059
- setFormatter(formatter) {
1060
- this.formatter = formatter;
1061
- }
1062
- setExporter(exporter) {
1063
- this.exporter = exporter;
1064
- }
1065
- async export(products, categories, brands, option) {
1066
- let transformedProducts = products;
1067
- for (const transformer of this.transformers)
1068
- transformedProducts = await transformer(
1069
- transformedProducts,
1070
- this._context
1071
- );
1072
- const writableStream = this.exporter();
1073
- await this.formatter.format(
1074
- writableStream,
1075
- transformedProducts,
1076
- categories,
1077
- brands,
1078
- option
1079
- );
1080
- }
1081
- }
1082
-
1083
- var Vat = /* @__PURE__ */ ((Vat2) => {
1084
- Vat2["NO_VAT"] = "NO_VAT";
1085
- Vat2["VAT_0"] = "VAT_0";
1086
- Vat2["VAT_10"] = "VAT_10";
1087
- Vat2["VAT_20"] = "VAT_20";
1088
- return Vat2;
1089
- })(Vat || {});
1090
- var Currency = /* @__PURE__ */ ((Currency2) => {
1091
- Currency2["RUR"] = "RUR";
1092
- Currency2["BYN"] = "BYN";
1093
- Currency2["EUR"] = "EUR";
1094
- Currency2["USD"] = "USD";
1095
- Currency2["UAN"] = "UAN";
1096
- Currency2["KZT"] = "KZT";
1097
- return Currency2;
1098
- })(Currency || {});
1099
-
1100
- exports.Currency = Currency;
1101
- exports.Extension = Extension;
1102
- exports.FormatterAbstract = FormatterAbstract;
1103
- exports.Formatters = Formatters;
1104
- exports.GoodsExporter = GoodsExporter;
1105
- exports.Vat = Vat;
1106
- exports.buildCategoryPaths = buildCategoryPaths;
1107
- exports.delay = delay;
1108
- exports.urlQueryEncode = urlQueryEncode;
1109
- exports.writeWithDrain = writeWithDrain;
1110
- //# sourceMappingURL=index.cjs.map