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