goods-exporter 1.1.2 → 1.2.0

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.
@@ -4,16 +4,16 @@ import { JsonStreamStringify } from 'json-stream-stringify';
4
4
  import { XMLBuilder } from 'fast-xml-parser';
5
5
  import fs from 'fs';
6
6
 
7
- var __defProp$9 = Object.defineProperty;
8
- var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __publicField$9 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+ var __defProp$b = Object.defineProperty;
8
+ var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __publicField$b = (obj, key, value) => __defNormalProp$b(obj, typeof key !== "symbol" ? key + "" : key, value);
10
10
  class CSVStream {
11
11
  constructor({ delimiter, lineSeparator, emptyFieldValue }) {
12
- __publicField$9(this, "stream", new PassThrough());
13
- __publicField$9(this, "delimiter", ";");
14
- __publicField$9(this, "lineSeparator", "\n");
15
- __publicField$9(this, "emptyFieldValue", "");
16
- __publicField$9(this, "columns", /* @__PURE__ */ new Set());
12
+ __publicField$b(this, "stream", new PassThrough());
13
+ __publicField$b(this, "delimiter", ";");
14
+ __publicField$b(this, "lineSeparator", "\n");
15
+ __publicField$b(this, "emptyFieldValue", "");
16
+ __publicField$b(this, "columns", /* @__PURE__ */ new Set());
17
17
  if (delimiter !== void 0) this.delimiter = delimiter;
18
18
  if (lineSeparator !== void 0) this.lineSeparator = lineSeparator;
19
19
  if (emptyFieldValue !== void 0) this.emptyFieldValue = emptyFieldValue;
@@ -47,13 +47,13 @@ var Extension = /* @__PURE__ */ ((Extension2) => {
47
47
  return Extension2;
48
48
  })(Extension || {});
49
49
 
50
- var __defProp$8 = Object.defineProperty;
51
- var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
52
- var __publicField$8 = (obj, key, value) => __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
50
+ var __defProp$a = Object.defineProperty;
51
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
52
+ var __publicField$a = (obj, key, value) => __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
53
53
  class CSVFormatter {
54
54
  constructor() {
55
- __publicField$8(this, "formatterName", "CSV");
56
- __publicField$8(this, "fileExtension", Extension.CSV);
55
+ __publicField$a(this, "formatterName", "CSV");
56
+ __publicField$a(this, "fileExtension", Extension.CSV);
57
57
  }
58
58
  async format(writableStream, products, categories, _, __) {
59
59
  const mappedCategories = {};
@@ -116,14 +116,14 @@ class CSVFormatter {
116
116
  }
117
117
  }
118
118
 
119
- var __defProp$7 = Object.defineProperty;
120
- var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
121
- var __publicField$7 = (obj, key, value) => __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
119
+ var __defProp$9 = Object.defineProperty;
120
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
121
+ var __publicField$9 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
122
122
  const { stream: stream$2 } = pkg;
123
123
  class ExcelFormatter {
124
124
  constructor() {
125
- __publicField$7(this, "formatterName", "Excel");
126
- __publicField$7(this, "fileExtension", Extension.XLSX);
125
+ __publicField$9(this, "formatterName", "Excel");
126
+ __publicField$9(this, "fileExtension", Extension.XLSX);
127
127
  }
128
128
  async format(writableStream, products, categories, _, __) {
129
129
  const mappedCategories = {};
@@ -188,14 +188,14 @@ class ExcelFormatter {
188
188
  }
189
189
  }
190
190
 
191
- var __defProp$6 = Object.defineProperty;
192
- var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
193
- var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
191
+ var __defProp$8 = Object.defineProperty;
192
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
193
+ var __publicField$8 = (obj, key, value) => __defNormalProp$8(obj, typeof key !== "symbol" ? key + "" : key, value);
194
194
  const { stream: stream$1 } = pkg;
195
195
  class InsalesFormatter {
196
196
  constructor() {
197
- __publicField$6(this, "formatterName", "Insales");
198
- __publicField$6(this, "fileExtension", Extension.XLSX);
197
+ __publicField$8(this, "formatterName", "Insales");
198
+ __publicField$8(this, "fileExtension", Extension.XLSX);
199
199
  }
200
200
  async format(writableStream, products, categories, _, __) {
201
201
  const mappedCategories = {};
@@ -324,13 +324,13 @@ class InsalesFormatter {
324
324
  }
325
325
  }
326
326
 
327
- var __defProp$5 = Object.defineProperty;
328
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
329
- var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
327
+ var __defProp$7 = Object.defineProperty;
328
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
329
+ var __publicField$7 = (obj, key, value) => __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
330
330
  class JSONFormatter {
331
331
  constructor() {
332
- __publicField$5(this, "formatterName", "JSON");
333
- __publicField$5(this, "fileExtension", Extension.JSON);
332
+ __publicField$7(this, "formatterName", "JSON");
333
+ __publicField$7(this, "fileExtension", Extension.JSON);
334
334
  }
335
335
  async format(writableStream, products, categories, brands, _) {
336
336
  const stream = new JsonStreamStringify({
@@ -342,13 +342,13 @@ class JSONFormatter {
342
342
  }
343
343
  }
344
344
 
345
- var __defProp$4 = Object.defineProperty;
346
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
347
- var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
345
+ var __defProp$6 = Object.defineProperty;
346
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
347
+ var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
348
348
  class SimpleJSONFormatter {
349
349
  constructor() {
350
- __publicField$4(this, "formatterName", "JSON");
351
- __publicField$4(this, "fileExtension", Extension.JSON);
350
+ __publicField$6(this, "formatterName", "JSON");
351
+ __publicField$6(this, "fileExtension", Extension.JSON);
352
352
  }
353
353
  async format(writableStream, products, categories, brands, _) {
354
354
  const groupedProduct = /* @__PURE__ */ new Map();
@@ -374,14 +374,14 @@ class SimpleJSONFormatter {
374
374
  }
375
375
  }
376
376
 
377
- var __defProp$3 = Object.defineProperty;
378
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
379
- var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
377
+ var __defProp$5 = Object.defineProperty;
378
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
379
+ var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, typeof key !== "symbol" ? key + "" : key, value);
380
380
  const { stream } = pkg;
381
381
  class TgShopFormatter {
382
382
  constructor() {
383
- __publicField$3(this, "formatterName", "TgShop");
384
- __publicField$3(this, "fileExtension", Extension.XLSX);
383
+ __publicField$5(this, "formatterName", "TgShop");
384
+ __publicField$5(this, "fileExtension", Extension.XLSX);
385
385
  }
386
386
  async format(writableStream, products, categories, _, __) {
387
387
  const getParameter = (product, key) => product.params?.find((value) => value.key === key);
@@ -452,13 +452,13 @@ class TgShopFormatter {
452
452
  }
453
453
  }
454
454
 
455
- var __defProp$2 = Object.defineProperty;
456
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
457
- var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
455
+ var __defProp$4 = Object.defineProperty;
456
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
457
+ var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
458
458
  class TildaFormatter {
459
459
  constructor() {
460
- __publicField$2(this, "formatterName", "Tilda");
461
- __publicField$2(this, "fileExtension", Extension.CSV);
460
+ __publicField$4(this, "formatterName", "Tilda");
461
+ __publicField$4(this, "fileExtension", Extension.CSV);
462
462
  }
463
463
  async format(writableStream, products, categories, _, __) {
464
464
  const mappedCategories = {};
@@ -505,13 +505,233 @@ class TildaFormatter {
505
505
  }
506
506
  }
507
507
 
508
- var __defProp$1 = Object.defineProperty;
509
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
510
- var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
508
+ const buildCategoryPaths = (categories) => {
509
+ const idToCategory = /* @__PURE__ */ new Map();
510
+ categories.forEach((category) => {
511
+ idToCategory.set(category.id, category);
512
+ });
513
+ const categoryPaths = /* @__PURE__ */ new Map();
514
+ categories.forEach((category) => {
515
+ const path = [];
516
+ let currentCategory = category;
517
+ while (currentCategory) {
518
+ path.unshift(currentCategory);
519
+ if (currentCategory.parentId !== void 0) {
520
+ currentCategory = idToCategory.get(currentCategory.parentId);
521
+ } else {
522
+ currentCategory = void 0;
523
+ }
524
+ }
525
+ categoryPaths.set(category.id, path);
526
+ });
527
+ return categoryPaths;
528
+ };
529
+
530
+ var __defProp$3 = Object.defineProperty;
531
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
532
+ var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, typeof key !== "symbol" ? key + "" : key, value);
533
+ class WooCommerceFormatter {
534
+ constructor() {
535
+ __publicField$3(this, "formatterName", "WooCommerce");
536
+ __publicField$3(this, "fileExtension", Extension.CSV);
537
+ __publicField$3(this, "DEFAULT_COLUMNS", [
538
+ "ID",
539
+ "Type",
540
+ "SKU",
541
+ "Name",
542
+ "Parent",
543
+ "Short description",
544
+ "Description",
545
+ "Stock",
546
+ "Regular price",
547
+ "Position",
548
+ "Categories",
549
+ "Tags",
550
+ "Images"
551
+ ]);
552
+ }
553
+ formatKeyAttribute(key) {
554
+ const keyWithoutInvalidCharacters = key.replaceAll(",", "").replaceAll(";", "");
555
+ return keyWithoutInvalidCharacters.length >= 28 ? keyWithoutInvalidCharacters.slice(0, 24) + "..." : keyWithoutInvalidCharacters;
556
+ }
557
+ formatValueAttribute(value) {
558
+ return value.replaceAll(",", "").replaceAll(";", "");
559
+ }
560
+ formatProducts(products) {
561
+ const formatParams = (params) => {
562
+ return params?.map(({ key, value }) => {
563
+ const formatedKey = this.formatKeyAttribute(key);
564
+ const formatedValue = this.formatValueAttribute(value);
565
+ return { key: formatedKey, value: formatedValue };
566
+ });
567
+ };
568
+ return products.map((product) => {
569
+ const params = formatParams(product.params);
570
+ const properties = formatParams(product.properties);
571
+ return { ...product, params, properties };
572
+ });
573
+ }
574
+ createAttribute(data) {
575
+ if (!data?.name || data.id === void 0) return;
576
+ const attributeStartName = "Attribute";
577
+ const attribute = {};
578
+ attribute[`${attributeStartName} ${data.id} name`] = data.name;
579
+ if (data.values !== void 0)
580
+ attribute[`${attributeStartName} ${data.id} value(s)`] = data.values;
581
+ if (data.visible !== void 0)
582
+ attribute[`${attributeStartName} ${data.id} visible`] = data.visible;
583
+ if (data.global !== void 0)
584
+ attribute[`${attributeStartName} ${data.id} global`] = data.global;
585
+ return attribute;
586
+ }
587
+ extractAttributes(products) {
588
+ const formatedProducts = this.formatProducts(products);
589
+ const paramsMap = /* @__PURE__ */ new Map();
590
+ const propertiesMap = /* @__PURE__ */ new Map();
591
+ const uniqueAttributes = /* @__PURE__ */ new Map();
592
+ formatedProducts.forEach((product) => {
593
+ product.params?.forEach(({ key }) => {
594
+ if (!uniqueAttributes.has(key)) {
595
+ uniqueAttributes.set(key, uniqueAttributes.size);
596
+ }
597
+ });
598
+ product.properties?.forEach(({ key }) => {
599
+ if (!uniqueAttributes.has(key)) {
600
+ uniqueAttributes.set(key, uniqueAttributes.size);
601
+ }
602
+ });
603
+ });
604
+ formatedProducts.forEach((product) => {
605
+ const paramAttributes = paramsMap.get(product.variantId) ?? {};
606
+ const propertyAttributes = propertiesMap.get(product.variantId) ?? {};
607
+ product.params?.forEach(({ key, value }) => {
608
+ const index = uniqueAttributes.get(key);
609
+ if (index === void 0) {
610
+ 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}`);
611
+ return;
612
+ }
613
+ const attribute = this.createAttribute({
614
+ name: key,
615
+ id: index,
616
+ values: value,
617
+ visible: 0,
618
+ global: 0
619
+ });
620
+ if (!attribute) return;
621
+ Object.entries(attribute).forEach(
622
+ ([key2, value2]) => paramAttributes[key2] = value2
623
+ );
624
+ });
625
+ product.properties?.forEach(({ key, value }) => {
626
+ const index = uniqueAttributes.get(key);
627
+ if (index === void 0) {
628
+ 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}`);
629
+ return;
630
+ }
631
+ if (paramAttributes[`Attribute ${index} name`]) {
632
+ 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}`);
633
+ return;
634
+ }
635
+ const attribute = this.createAttribute({
636
+ name: key,
637
+ id: index,
638
+ values: value,
639
+ global: 0
640
+ });
641
+ if (!attribute) return;
642
+ Object.entries(attribute).forEach(
643
+ ([key2, value2]) => propertyAttributes[key2] = value2
644
+ );
645
+ });
646
+ paramsMap.set(product.variantId, paramAttributes);
647
+ propertiesMap.set(product.variantId, propertyAttributes);
648
+ });
649
+ return { params: paramsMap, properties: propertiesMap };
650
+ }
651
+ removeVisibleFromAttributes(params) {
652
+ Object.entries(params).forEach(([key]) => {
653
+ if (key.includes("visible")) params[key] = "";
654
+ });
655
+ }
656
+ async format(writableStream, products, categories, _, __) {
657
+ const categoriePaths = buildCategoryPaths(categories ?? []);
658
+ const csvStream = new CSVStream({
659
+ delimiter: ";",
660
+ emptyFieldValue: "",
661
+ lineSeparator: "\n"
662
+ });
663
+ csvStream.getWritableStream().pipe(writableStream);
664
+ const columns = new Set(this.DEFAULT_COLUMNS);
665
+ const attributes = this.extractAttributes(products);
666
+ const variations = products.map((product, index) => {
667
+ const pathsArray = categoriePaths.get(product.categoryId)?.map((category) => category.name);
668
+ let row = {
669
+ ID: product.variantId,
670
+ Type: "variation",
671
+ SKU: product.variantId,
672
+ Name: product.title,
673
+ Parent: product.parentId ?? 0,
674
+ "Short description": "",
675
+ Description: product.description,
676
+ Stock: product.count ?? 0,
677
+ "Regular price": product.price,
678
+ Position: index + 1,
679
+ Categories: pathsArray?.join(" > "),
680
+ Tags: product.keywords?.join(","),
681
+ Images: product.images?.join(",")
682
+ };
683
+ const productParams = attributes.params.get(product.variantId) ?? {};
684
+ this.removeVisibleFromAttributes(productParams);
685
+ row = { ...row, ...productParams };
686
+ return row;
687
+ });
688
+ const parentProducts = /* @__PURE__ */ new Map();
689
+ variations.forEach((product) => {
690
+ const currentParent = parentProducts.get(product.Parent);
691
+ let row = {
692
+ ...product,
693
+ Type: "variable",
694
+ ID: product.Parent,
695
+ SKU: product.Parent,
696
+ Position: 0,
697
+ Parent: "",
698
+ "Regular price": ""
699
+ };
700
+ row.Stock = (currentParent?.Stock || 0) + (product?.Stock || 0);
701
+ const productParams = attributes.params.get(product.SKU) ?? {};
702
+ const productProperties = attributes.properties.get(product.SKU) ?? {};
703
+ Object.entries(productParams).forEach(([key]) => {
704
+ if (key.includes("visible")) productParams[key] = 0;
705
+ });
706
+ if (currentParent) {
707
+ Object.entries(productParams).forEach(([key, value]) => {
708
+ if (key.includes("value(s)")) {
709
+ productParams[key] = currentParent[key] + `, ${value}`;
710
+ }
711
+ });
712
+ }
713
+ Object.keys({ ...row, ...productParams, ...productProperties }).forEach(
714
+ (item) => columns.add(item)
715
+ );
716
+ row = { ...row, ...productParams, ...productProperties };
717
+ parentProducts.set(product.Parent, row);
718
+ });
719
+ const variableProducts = Array.from(parentProducts.values());
720
+ csvStream.setColumns(columns);
721
+ [...variableProducts, ...variations].forEach((product) => {
722
+ csvStream.addRow(product);
723
+ });
724
+ csvStream.getWritableStream().end();
725
+ }
726
+ }
727
+
728
+ var __defProp$2 = Object.defineProperty;
729
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
730
+ var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
511
731
  class YMLFormatter {
512
732
  constructor() {
513
- __publicField$1(this, "formatterName", "YMl");
514
- __publicField$1(this, "fileExtension", Extension.YML);
733
+ __publicField$2(this, "formatterName", "YMl");
734
+ __publicField$2(this, "fileExtension", Extension.YML);
515
735
  }
516
736
  async format(writableStream, products, categories, brands, options) {
517
737
  const result = new PassThrough();
@@ -658,6 +878,17 @@ class YMLFormatter {
658
878
  }
659
879
  }
660
880
 
881
+ var __defProp$1 = Object.defineProperty;
882
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
883
+ var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
884
+ class XMLFormatter extends YMLFormatter {
885
+ constructor() {
886
+ super(...arguments);
887
+ __publicField$1(this, "formatterName", "XML");
888
+ __publicField$1(this, "fileExtension", Extension.XML);
889
+ }
890
+ }
891
+
661
892
  const Formatters = {
662
893
  TildaFormatter,
663
894
  CSVFormatter,
@@ -666,7 +897,9 @@ const Formatters = {
666
897
  TgShopFormatter,
667
898
  ExcelFormatter,
668
899
  JSONFormatter,
669
- SimpleJSONFormatter
900
+ SimpleJSONFormatter,
901
+ XMLFormatter,
902
+ WooCommerceFormatter
670
903
  };
671
904
 
672
905
  var __defProp = Object.defineProperty;
@@ -723,5 +956,5 @@ var Currency = /* @__PURE__ */ ((Currency2) => {
723
956
  return Currency2;
724
957
  })(Currency || {});
725
958
 
726
- export { Currency, Extension, FormatterAbstract, Formatters, GoodsExporter, Vat };
959
+ export { Currency, Extension, FormatterAbstract, Formatters, GoodsExporter, Vat, buildCategoryPaths };
727
960
  //# sourceMappingURL=index.mjs.map