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