@atlaskit/codemod-cli 0.27.2 → 0.27.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/cjs/main.js +85 -33
  3. package/dist/cjs/presets/index.js +2 -1
  4. package/dist/cjs/presets/remove-token-fallbacks/remove-token-fallbacks.js +225 -0
  5. package/dist/cjs/presets/remove-token-fallbacks/types.js +5 -0
  6. package/dist/cjs/presets/remove-token-fallbacks/utils/all-tokens.js +44 -0
  7. package/dist/cjs/presets/remove-token-fallbacks/utils/color-utils.js +93 -0
  8. package/dist/cjs/presets/remove-token-fallbacks/utils/get-team-info.js +51 -0
  9. package/dist/cjs/presets/remove-token-fallbacks/utils/normalize-values.js +113 -0
  10. package/dist/cjs/presets/remove-token-fallbacks/utils/remove-unused-imports.js +61 -0
  11. package/dist/cjs/presets/remove-token-fallbacks/utils/remove-unused-variables.js +37 -0
  12. package/dist/cjs/presets/remove-token-fallbacks/utils/reporter.js +310 -0
  13. package/dist/cjs/presets/remove-token-fallbacks/utils/token-processor.js +632 -0
  14. package/dist/cjs/presets/remove-token-fallbacks/utils/update-comments.js +58 -0
  15. package/dist/es2019/main.js +24 -0
  16. package/dist/es2019/presets/index.js +2 -1
  17. package/dist/es2019/presets/remove-token-fallbacks/remove-token-fallbacks.js +130 -0
  18. package/dist/es2019/presets/remove-token-fallbacks/types.js +1 -0
  19. package/dist/es2019/presets/remove-token-fallbacks/utils/all-tokens.js +30 -0
  20. package/dist/es2019/presets/remove-token-fallbacks/utils/color-utils.js +84 -0
  21. package/dist/es2019/presets/remove-token-fallbacks/utils/get-team-info.js +22 -0
  22. package/dist/es2019/presets/remove-token-fallbacks/utils/normalize-values.js +104 -0
  23. package/dist/es2019/presets/remove-token-fallbacks/utils/remove-unused-imports.js +51 -0
  24. package/dist/es2019/presets/remove-token-fallbacks/utils/remove-unused-variables.js +31 -0
  25. package/dist/es2019/presets/remove-token-fallbacks/utils/reporter.js +118 -0
  26. package/dist/es2019/presets/remove-token-fallbacks/utils/token-processor.js +377 -0
  27. package/dist/es2019/presets/remove-token-fallbacks/utils/update-comments.js +46 -0
  28. package/dist/esm/main.js +85 -33
  29. package/dist/esm/presets/index.js +2 -1
  30. package/dist/esm/presets/remove-token-fallbacks/remove-token-fallbacks.js +215 -0
  31. package/dist/esm/presets/remove-token-fallbacks/types.js +1 -0
  32. package/dist/esm/presets/remove-token-fallbacks/utils/all-tokens.js +38 -0
  33. package/dist/esm/presets/remove-token-fallbacks/utils/color-utils.js +86 -0
  34. package/dist/esm/presets/remove-token-fallbacks/utils/get-team-info.js +44 -0
  35. package/dist/esm/presets/remove-token-fallbacks/utils/normalize-values.js +107 -0
  36. package/dist/esm/presets/remove-token-fallbacks/utils/remove-unused-imports.js +55 -0
  37. package/dist/esm/presets/remove-token-fallbacks/utils/remove-unused-variables.js +31 -0
  38. package/dist/esm/presets/remove-token-fallbacks/utils/reporter.js +302 -0
  39. package/dist/esm/presets/remove-token-fallbacks/utils/token-processor.js +625 -0
  40. package/dist/esm/presets/remove-token-fallbacks/utils/update-comments.js +51 -0
  41. package/dist/types/presets/index.d.ts +1 -0
  42. package/dist/types/presets/remove-token-fallbacks/remove-token-fallbacks.d.ts +29 -0
  43. package/dist/types/presets/remove-token-fallbacks/types.d.ts +39 -0
  44. package/dist/types/presets/remove-token-fallbacks/utils/all-tokens.d.ts +1 -0
  45. package/dist/types/presets/remove-token-fallbacks/utils/color-utils.d.ts +3 -0
  46. package/dist/types/presets/remove-token-fallbacks/utils/get-team-info.d.ts +8 -0
  47. package/dist/types/presets/remove-token-fallbacks/utils/normalize-values.d.ts +8 -0
  48. package/dist/types/presets/remove-token-fallbacks/utils/remove-unused-imports.d.ts +2 -0
  49. package/dist/types/presets/remove-token-fallbacks/utils/remove-unused-variables.d.ts +2 -0
  50. package/dist/types/presets/remove-token-fallbacks/utils/reporter.d.ts +4 -0
  51. package/dist/types/presets/remove-token-fallbacks/utils/token-processor.d.ts +30 -0
  52. package/dist/types/presets/remove-token-fallbacks/utils/update-comments.d.ts +2 -0
  53. package/dist/types-ts4.5/presets/index.d.ts +1 -0
  54. package/dist/types-ts4.5/presets/remove-token-fallbacks/remove-token-fallbacks.d.ts +29 -0
  55. package/dist/types-ts4.5/presets/remove-token-fallbacks/types.d.ts +39 -0
  56. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/all-tokens.d.ts +1 -0
  57. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/color-utils.d.ts +3 -0
  58. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/get-team-info.d.ts +8 -0
  59. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/normalize-values.d.ts +8 -0
  60. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/remove-unused-imports.d.ts +2 -0
  61. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/remove-unused-variables.d.ts +2 -0
  62. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/reporter.d.ts +4 -0
  63. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/token-processor.d.ts +30 -0
  64. package/dist/types-ts4.5/presets/remove-token-fallbacks/utils/update-comments.d.ts +2 -0
  65. package/package.json +8 -3
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.normalizeValues = normalizeValues;
8
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
+ var _chalk = _interopRequireDefault(require("chalk"));
10
+ var _colorUtils = require("./color-utils");
11
+ // so far allowing to remove only exact matches in values. Can be increased to auto-remove fallbacks with similar values
12
+ var ACCEPTABLE_COLOR_DIFFERENCE = 0;
13
+ var ACCEPTABLE_SPACE_DIFFERENCE = 0;
14
+ var ACCEPTABLE_NUMERIC_DIFFERENCE = 0;
15
+ function normalizeValues(tokenKey, tokenValue, fallbackValue) {
16
+ var tokenLogValue;
17
+ var fallbackLogValue;
18
+ var normalizedTokenValue = tokenValue;
19
+ var normalizedFallbackValue = fallbackValue;
20
+ var lowerCaseTokenKey = tokenKey === null || tokenKey === void 0 ? void 0 : tokenKey.toLowerCase();
21
+ var difference;
22
+ var isAcceptableDifference;
23
+ if (lowerCaseTokenKey.startsWith('color') || lowerCaseTokenKey.startsWith('elevation')) {
24
+ if (tokenValue && (0, _colorUtils.isValidColor)(tokenValue)) {
25
+ var normalizedHex = (0, _colorUtils.colorToHex)(tokenValue);
26
+ tokenLogValue = _chalk.default.bgHex(normalizedHex)(tokenValue);
27
+ normalizedTokenValue = normalizedHex;
28
+ }
29
+ if (fallbackValue && (0, _colorUtils.isValidColor)(fallbackValue)) {
30
+ var _normalizedHex = (0, _colorUtils.colorToHex)(fallbackValue);
31
+ fallbackLogValue = _chalk.default.bgHex(_normalizedHex)(fallbackValue);
32
+ normalizedFallbackValue = _normalizedHex;
33
+ }
34
+ if (normalizedTokenValue && normalizedFallbackValue) {
35
+ difference = (0, _colorUtils.compareHex)(normalizedTokenValue, normalizedFallbackValue);
36
+ isAcceptableDifference = difference <= ACCEPTABLE_COLOR_DIFFERENCE;
37
+ }
38
+ } else if (lowerCaseTokenKey.startsWith('space')) {
39
+ var tokenValueInPx = tokenValue ? convertToPx(tokenValue) : undefined;
40
+ var fallbackValueInPx = fallbackValue ? convertToPx(fallbackValue) : undefined;
41
+ if (tokenValueInPx !== undefined && fallbackValueInPx !== undefined) {
42
+ var maxVal = Math.max(tokenValueInPx, fallbackValueInPx);
43
+ difference = Math.abs(tokenValueInPx - fallbackValueInPx) / maxVal * 100;
44
+ isAcceptableDifference = difference <= ACCEPTABLE_SPACE_DIFFERENCE;
45
+ }
46
+ // Log the normalized values
47
+ normalizedTokenValue = tokenValue;
48
+ normalizedFallbackValue = fallbackValue;
49
+ tokenLogValue = tokenValue;
50
+ fallbackLogValue = fallbackValue;
51
+ } else {
52
+ // Handle other numeric comparisons
53
+ var tokenValueNumber = parseFloat(tokenValue !== null && tokenValue !== void 0 ? tokenValue : '');
54
+ var fallbackValueNumber = parseFloat(fallbackValue !== null && fallbackValue !== void 0 ? fallbackValue : '');
55
+ if (!isNaN(tokenValueNumber) && !isNaN(fallbackValueNumber)) {
56
+ var _maxVal = Math.max(tokenValueNumber, fallbackValueNumber);
57
+ difference = Math.abs(tokenValueNumber - fallbackValueNumber) / _maxVal * 100;
58
+ isAcceptableDifference = difference <= ACCEPTABLE_NUMERIC_DIFFERENCE;
59
+ }
60
+ // Log the normalized values
61
+ normalizedTokenValue = tokenValue;
62
+ normalizedFallbackValue = fallbackValue;
63
+ tokenLogValue = tokenValue;
64
+ fallbackLogValue = fallbackValue;
65
+ }
66
+ if (tokenLogValue === undefined) {
67
+ tokenLogValue = _chalk.default.magenta(tokenValue || '');
68
+ }
69
+ if (fallbackLogValue === undefined) {
70
+ fallbackLogValue = _chalk.default.yellow(fallbackValue || '');
71
+ }
72
+ return {
73
+ difference: difference,
74
+ isAcceptableDifference: isAcceptableDifference,
75
+ tokenLogValue: tokenLogValue,
76
+ fallbackLogValue: fallbackLogValue,
77
+ normalizedTokenValue: normalizedTokenValue,
78
+ normalizedFallbackValue: normalizedFallbackValue
79
+ };
80
+ }
81
+ function convertToPx(value) {
82
+ // If the value is a number, return it directly
83
+ if (typeof value === 'number') {
84
+ return value;
85
+ }
86
+ // Check if the string is a plain number (without units)
87
+ var plainNumberRegex = /^-?\d+(\.\d+)?$/;
88
+ if (plainNumberRegex.test(value)) {
89
+ return parseFloat(value);
90
+ }
91
+ // Regular expression to match CSS units
92
+ var unitRegex = /^(-?\d+(\.\d+)?)(px|rem|em|%)$/;
93
+ var match = value.match(unitRegex);
94
+ if (!match) {
95
+ return undefined;
96
+ }
97
+ var _match = (0, _slicedToArray2.default)(match, 4),
98
+ num = _match[1],
99
+ unit = _match[3];
100
+ var numericValue = parseFloat(num);
101
+ switch (unit) {
102
+ case 'px':
103
+ return numericValue;
104
+ case 'rem':
105
+ return numericValue * 16;
106
+ // Assuming 1rem = 16px
107
+ case 'em':
108
+ return numericValue * 16;
109
+ // Assuming 1em = 16px
110
+ default:
111
+ return undefined;
112
+ }
113
+ }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeUnusedImports = removeUnusedImports;
7
+ function removeUnusedImports(importDeclarations, j) {
8
+ var removeIfUnused = function removeIfUnused(importSpecifier, importDeclaration) {
9
+ var _importSpecifier$valu;
10
+ var varName = (_importSpecifier$valu = importSpecifier.value.local) === null || _importSpecifier$valu === void 0 ? void 0 : _importSpecifier$valu.name;
11
+ if (varName === 'React' || varName === 'jsx') {
12
+ return false;
13
+ }
14
+ var isUsedInScopes = function isUsedInScopes() {
15
+ return j(importDeclaration).closestScope().find(j.Identifier, {
16
+ name: varName
17
+ }).filter(function (p) {
18
+ var _importSpecifier$valu2;
19
+ if (p.value.start === ((_importSpecifier$valu2 = importSpecifier.value.local) === null || _importSpecifier$valu2 === void 0 ? void 0 : _importSpecifier$valu2.start)) {
20
+ return false;
21
+ }
22
+ if (p.parentPath.value.type === 'Property' && p.name === 'key') {
23
+ return false;
24
+ }
25
+ if (p.name === 'property') {
26
+ return false;
27
+ }
28
+ return true;
29
+ }).size() > 0;
30
+ };
31
+ if (!isUsedInScopes()) {
32
+ j(importSpecifier).remove();
33
+ return true;
34
+ }
35
+ return false;
36
+ };
37
+ var removeUnusedDefaultImport = function removeUnusedDefaultImport(importDeclaration) {
38
+ return j(importDeclaration).find(j.ImportDefaultSpecifier).filter(function (s) {
39
+ return removeIfUnused(s, importDeclaration);
40
+ }).size() > 0;
41
+ };
42
+ var removeUnusedNonDefaultImports = function removeUnusedNonDefaultImports(importDeclaration) {
43
+ return j(importDeclaration).find(j.ImportSpecifier).filter(function (s) {
44
+ return removeIfUnused(s, importDeclaration);
45
+ }).size() > 0;
46
+ };
47
+ var processImportDeclaration = function processImportDeclaration(importDeclaration) {
48
+ var _importDeclaration$va, _importDeclaration$va2;
49
+ if (((_importDeclaration$va = importDeclaration.value.specifiers) === null || _importDeclaration$va === void 0 ? void 0 : _importDeclaration$va.length) === 0) {
50
+ return false;
51
+ }
52
+ var hadUnusedDefaultImport = removeUnusedDefaultImport(importDeclaration);
53
+ var hadUnusedNonDefaultImports = removeUnusedNonDefaultImports(importDeclaration);
54
+ if (((_importDeclaration$va2 = importDeclaration.value.specifiers) === null || _importDeclaration$va2 === void 0 ? void 0 : _importDeclaration$va2.length) === 0) {
55
+ j(importDeclaration).remove();
56
+ return true;
57
+ }
58
+ return hadUnusedDefaultImport || hadUnusedNonDefaultImports;
59
+ };
60
+ importDeclarations.forEach(processImportDeclaration);
61
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeUnusedVariables = removeUnusedVariables;
7
+ function removeUnusedVariables(variableDeclarations, j) {
8
+ var removeIfUnused = function removeIfUnused(varDeclarator) {
9
+ var _varDeclarator$value;
10
+ if (((_varDeclarator$value = varDeclarator.value) === null || _varDeclarator$value === void 0 ? void 0 : _varDeclarator$value.id.type) !== 'Identifier') {
11
+ return false;
12
+ }
13
+ var varName = varDeclarator.value.id.name;
14
+ var isUsedInScopes = function isUsedInScopes() {
15
+ return j(varDeclarator).closestScope().find(j.Identifier, {
16
+ name: varName
17
+ }).filter(function (p) {
18
+ if (p.value.start === varDeclarator.value.id.start) {
19
+ return false;
20
+ }
21
+ if (p.parentPath.value.type === 'Property' && p.name === 'key') {
22
+ return false;
23
+ }
24
+ if (p.name === 'property') {
25
+ return false;
26
+ }
27
+ return true;
28
+ }).size() > 0;
29
+ };
30
+ if (!isUsedInScopes()) {
31
+ j(varDeclarator).remove();
32
+ return true;
33
+ }
34
+ return false;
35
+ };
36
+ variableDeclarations.forEach(removeIfUnused);
37
+ }
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.clearFolder = clearFolder;
8
+ exports.combineReports = combineReports;
9
+ exports.writeReports = writeReports;
10
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
12
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13
+ var _promises = _interopRequireDefault(require("fs/promises"));
14
+ var _path = _interopRequireDefault(require("path"));
15
+ var _uuid = require("uuid");
16
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
17
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
18
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /* eslint-disable no-console */
19
+ function writeToCsv(_x, _x2) {
20
+ return _writeToCsv.apply(this, arguments);
21
+ }
22
+ function _writeToCsv() {
23
+ _writeToCsv = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(filePath, data) {
24
+ return _regenerator.default.wrap(function _callee$(_context) {
25
+ while (1) switch (_context.prev = _context.next) {
26
+ case 0:
27
+ _context.next = 2;
28
+ return _promises.default.writeFile(filePath, data.join('\n'), 'utf-8');
29
+ case 2:
30
+ case "end":
31
+ return _context.stop();
32
+ }
33
+ }, _callee);
34
+ }));
35
+ return _writeToCsv.apply(this, arguments);
36
+ }
37
+ function readCsv(_x3) {
38
+ return _readCsv.apply(this, arguments);
39
+ }
40
+ function _readCsv() {
41
+ _readCsv = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(filePath) {
42
+ var data;
43
+ return _regenerator.default.wrap(function _callee2$(_context2) {
44
+ while (1) switch (_context2.prev = _context2.next) {
45
+ case 0:
46
+ _context2.next = 2;
47
+ return _promises.default.readFile(filePath, 'utf-8');
48
+ case 2:
49
+ data = _context2.sent;
50
+ return _context2.abrupt("return", data.split('\n').filter(function (line) {
51
+ return line.trim() !== '';
52
+ }));
53
+ case 4:
54
+ case "end":
55
+ return _context2.stop();
56
+ }
57
+ }, _callee2);
58
+ }));
59
+ return _readCsv.apply(this, arguments);
60
+ }
61
+ function escapeCsvValue(value) {
62
+ if (!value) {
63
+ return value;
64
+ }
65
+ if (value.includes('"')) {
66
+ // Escape double quotes by doubling them
67
+ value = value.replace(/"/g, '""');
68
+ }
69
+ if (value.includes(',') || value.includes('"') || value.includes('\n')) {
70
+ // Surround the value with double quotes if it contains a comma, double quotes, or newlines
71
+ value = "\"".concat(value, "\"");
72
+ }
73
+ return value;
74
+ }
75
+ function clearFolder(_x4) {
76
+ return _clearFolder.apply(this, arguments);
77
+ }
78
+ function _clearFolder() {
79
+ _clearFolder = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(reportFolder) {
80
+ var filesToDelete, _iterator, _step, file, filePath;
81
+ return _regenerator.default.wrap(function _callee3$(_context3) {
82
+ while (1) switch (_context3.prev = _context3.next) {
83
+ case 0:
84
+ console.log('Clearing report folder:', reportFolder);
85
+ _context3.next = 3;
86
+ return _promises.default.readdir(reportFolder);
87
+ case 3:
88
+ filesToDelete = _context3.sent;
89
+ _iterator = _createForOfIteratorHelper(filesToDelete);
90
+ _context3.prev = 5;
91
+ _iterator.s();
92
+ case 7:
93
+ if ((_step = _iterator.n()).done) {
94
+ _context3.next = 14;
95
+ break;
96
+ }
97
+ file = _step.value;
98
+ filePath = _path.default.join(reportFolder, file);
99
+ _context3.next = 12;
100
+ return _promises.default.unlink(filePath);
101
+ case 12:
102
+ _context3.next = 7;
103
+ break;
104
+ case 14:
105
+ _context3.next = 19;
106
+ break;
107
+ case 16:
108
+ _context3.prev = 16;
109
+ _context3.t0 = _context3["catch"](5);
110
+ _iterator.e(_context3.t0);
111
+ case 19:
112
+ _context3.prev = 19;
113
+ _iterator.f();
114
+ return _context3.finish(19);
115
+ case 22:
116
+ case "end":
117
+ return _context3.stop();
118
+ }
119
+ }, _callee3, null, [[5, 16, 19, 22]]);
120
+ }));
121
+ return _clearFolder.apply(this, arguments);
122
+ }
123
+ function saveFilePaths(_x5, _x6) {
124
+ return _saveFilePaths.apply(this, arguments);
125
+ }
126
+ function _saveFilePaths() {
127
+ _saveFilePaths = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(reportFolder, files) {
128
+ var filesTxtPath;
129
+ return _regenerator.default.wrap(function _callee4$(_context4) {
130
+ while (1) switch (_context4.prev = _context4.next) {
131
+ case 0:
132
+ filesTxtPath = _path.default.join(reportFolder, 'files.txt');
133
+ _context4.next = 3;
134
+ return _promises.default.writeFile(filesTxtPath, Array.from(files).map(function (filePath) {
135
+ return "\"".concat(filePath, "\"");
136
+ }).join(' '), 'utf-8');
137
+ case 3:
138
+ case "end":
139
+ return _context4.stop();
140
+ }
141
+ }, _callee4);
142
+ }));
143
+ return _saveFilePaths.apply(this, arguments);
144
+ }
145
+ function combineReports(_x7) {
146
+ return _combineReports.apply(this, arguments);
147
+ }
148
+ function _combineReports() {
149
+ _combineReports = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5(reportFolder) {
150
+ var files, totalReplaced, totalNotReplaced, combinedReplacements, combinedNonReplacements, affectedPaths, _iterator2, _step2, file, filePath, replacements, codeFilePaths, _iterator3, _step3, codeFilePath, nonReplacements, totalTokens, percentageReplaced, combinedSummaryPath, summaryData, sortCsvLines, sortedReplacements, sortedNonReplacements, combinedReplacementsPath, header, combinedNonReplacementsPath;
151
+ return _regenerator.default.wrap(function _callee5$(_context5) {
152
+ while (1) switch (_context5.prev = _context5.next) {
153
+ case 0:
154
+ console.log('Combining reports in folder:', reportFolder);
155
+ _context5.next = 3;
156
+ return _promises.default.readdir(reportFolder);
157
+ case 3:
158
+ files = _context5.sent;
159
+ totalReplaced = 0;
160
+ totalNotReplaced = 0;
161
+ combinedReplacements = [];
162
+ combinedNonReplacements = [];
163
+ affectedPaths = new Set();
164
+ _iterator2 = _createForOfIteratorHelper(files);
165
+ _context5.prev = 10;
166
+ _iterator2.s();
167
+ case 12:
168
+ if ((_step2 = _iterator2.n()).done) {
169
+ _context5.next = 34;
170
+ break;
171
+ }
172
+ file = _step2.value;
173
+ filePath = _path.default.join(reportFolder, file);
174
+ if (!file.endsWith('_success.csv')) {
175
+ _context5.next = 26;
176
+ break;
177
+ }
178
+ _context5.next = 18;
179
+ return readCsv(filePath);
180
+ case 18:
181
+ replacements = _context5.sent;
182
+ codeFilePaths = replacements.map(function (x) {
183
+ return x.split(',')[2];
184
+ });
185
+ _iterator3 = _createForOfIteratorHelper(codeFilePaths);
186
+ try {
187
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
188
+ codeFilePath = _step3.value;
189
+ affectedPaths.add(codeFilePath);
190
+ }
191
+ } catch (err) {
192
+ _iterator3.e(err);
193
+ } finally {
194
+ _iterator3.f();
195
+ }
196
+ totalReplaced += replacements.length;
197
+ combinedReplacements.push.apply(combinedReplacements, (0, _toConsumableArray2.default)(replacements));
198
+ _context5.next = 32;
199
+ break;
200
+ case 26:
201
+ if (!file.endsWith('_failed.csv')) {
202
+ _context5.next = 32;
203
+ break;
204
+ }
205
+ _context5.next = 29;
206
+ return readCsv(filePath);
207
+ case 29:
208
+ nonReplacements = _context5.sent;
209
+ totalNotReplaced += nonReplacements.length;
210
+ combinedNonReplacements.push.apply(combinedNonReplacements, (0, _toConsumableArray2.default)(nonReplacements));
211
+ case 32:
212
+ _context5.next = 12;
213
+ break;
214
+ case 34:
215
+ _context5.next = 39;
216
+ break;
217
+ case 36:
218
+ _context5.prev = 36;
219
+ _context5.t0 = _context5["catch"](10);
220
+ _iterator2.e(_context5.t0);
221
+ case 39:
222
+ _context5.prev = 39;
223
+ _iterator2.f();
224
+ return _context5.finish(39);
225
+ case 42:
226
+ totalTokens = totalReplaced + totalNotReplaced;
227
+ percentageReplaced = totalTokens > 0 ? totalReplaced / totalTokens * 100 : 0;
228
+ _context5.next = 46;
229
+ return clearFolder(reportFolder);
230
+ case 46:
231
+ // Write combined summary as JSON
232
+ combinedSummaryPath = _path.default.join(reportFolder, 'summary.json');
233
+ summaryData = {
234
+ totalReplaced: totalReplaced,
235
+ totalNotReplaced: totalNotReplaced,
236
+ percentageReplaced: percentageReplaced
237
+ };
238
+ _context5.next = 50;
239
+ return _promises.default.writeFile(combinedSummaryPath, JSON.stringify(summaryData, null, 2), 'utf-8');
240
+ case 50:
241
+ // Sort the combined arrays
242
+ sortCsvLines = function sortCsvLines(lines) {
243
+ return lines.sort(function (a, b) {
244
+ var aCols = a.split(',');
245
+ var bCols = b.split(',');
246
+ for (var i = 0; i < 4; i++) {
247
+ var comparison = aCols[i].localeCompare(bCols[i]);
248
+ if (comparison !== 0) {
249
+ return comparison;
250
+ }
251
+ }
252
+ return 0;
253
+ });
254
+ };
255
+ sortedReplacements = sortCsvLines(combinedReplacements);
256
+ sortedNonReplacements = sortCsvLines(combinedNonReplacements); // Write combined replacements
257
+ combinedReplacementsPath = _path.default.join(reportFolder, 'success.csv');
258
+ header = 'Team,Package,File,Line Number,Raw Token Key,Raw Fallback Value,Resolved Token Value,Resolved Fallback Value,Difference %';
259
+ _context5.next = 57;
260
+ return _promises.default.writeFile(combinedReplacementsPath, [header].concat((0, _toConsumableArray2.default)(sortedReplacements)).join('\n'), 'utf-8');
261
+ case 57:
262
+ // Write combined non-replacements
263
+ combinedNonReplacementsPath = _path.default.join(reportFolder, 'failed.csv');
264
+ _context5.next = 60;
265
+ return _promises.default.writeFile(combinedNonReplacementsPath, [header].concat((0, _toConsumableArray2.default)(sortedNonReplacements)).join('\n'), 'utf-8');
266
+ case 60:
267
+ _context5.next = 62;
268
+ return saveFilePaths(reportFolder, affectedPaths);
269
+ case 62:
270
+ case "end":
271
+ return _context5.stop();
272
+ }
273
+ }, _callee5, null, [[10, 36, 39, 42]]);
274
+ }));
275
+ return _combineReports.apply(this, arguments);
276
+ }
277
+ function prepareCsvData(items) {
278
+ return items.map(function (item) {
279
+ var _item$difference$toFi, _item$difference;
280
+ return [escapeCsvValue(item.teamInfo.teamName), escapeCsvValue(item.teamInfo.packageName), escapeCsvValue(item.filePath), escapeCsvValue(String(item.lineNumber)), escapeCsvValue(item.tokenKey), escapeCsvValue(item.rawFallbackValue), escapeCsvValue(item.resolvedTokenValue), escapeCsvValue(item.resolvedFallbackValue), escapeCsvValue((_item$difference$toFi = (_item$difference = item.difference) === null || _item$difference === void 0 ? void 0 : _item$difference.toFixed(1)) !== null && _item$difference$toFi !== void 0 ? _item$difference$toFi : '')].join(',');
281
+ });
282
+ }
283
+ function writeReports(_x8, _x9) {
284
+ return _writeReports.apply(this, arguments);
285
+ }
286
+ function _writeReports() {
287
+ _writeReports = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee6(details, reportFolder) {
288
+ var replacementsFilePath, nonReplacementsFilePath, replacementData, nonReplacementData;
289
+ return _regenerator.default.wrap(function _callee6$(_context6) {
290
+ while (1) switch (_context6.prev = _context6.next) {
291
+ case 0:
292
+ replacementsFilePath = _path.default.join(reportFolder, "".concat((0, _uuid.v4)(), "_success.csv"));
293
+ nonReplacementsFilePath = _path.default.join(reportFolder, "".concat((0, _uuid.v4)(), "_failed.csv"));
294
+ _context6.next = 4;
295
+ return _promises.default.mkdir(reportFolder, {
296
+ recursive: true
297
+ });
298
+ case 4:
299
+ replacementData = prepareCsvData(details.replaced);
300
+ nonReplacementData = prepareCsvData(details.notReplaced);
301
+ _context6.next = 8;
302
+ return Promise.all([writeToCsv(replacementsFilePath, replacementData), writeToCsv(nonReplacementsFilePath, nonReplacementData)]);
303
+ case 8:
304
+ case "end":
305
+ return _context6.stop();
306
+ }
307
+ }, _callee6);
308
+ }));
309
+ return _writeReports.apply(this, arguments);
310
+ }