@zohodesk/react-cli 1.0.3-exp.3 → 1.0.3-exp.5

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.
@@ -25,7 +25,38 @@ const {
25
25
  errHandler
26
26
  } = require('../postcss-plugins/variableModificationPlugin/index');
27
27
 
28
- const supportedProps = ['font-size', 'margin', 'margin-top', 'margin-bottom', 'margin-left', 'margin-right', 'padding', 'padding-top', 'padding-bottom', 'padding-left', 'padding-right', '^top', '^right', '^bottom', '^left', '^width', 'min-width', 'max-width', '^height', 'min-height', 'max-height', 'text-indent', 'clip', 'flex-basis', 'row-gap', 'gap', 'column-gap', 'flex']; // const avoidProps = [];
28
+ const supportedProps = ['font-size', 'margin', 'margin-top', 'margin-bottom', 'margin-left', 'margin-right', 'padding', 'padding-top', 'padding-bottom', 'padding-left', 'padding-right', '^top', '^right', '^bottom', '^left', '^width', 'min-width', 'max-width', '^height', 'min-height', 'max-height', 'text-indent', 'clip', 'flex-basis', 'row-gap', 'gap', 'column-gap', 'flex'];
29
+
30
+ function convertCalcValue(pxReplacement, parsedValue) {
31
+ Object.keys(parsedValue).forEach(key => {
32
+ if (parsedValue[key].includes('px')) {
33
+ parsedValue[key] = pxReplacement.replace('$$', parsedValue[key].replace('px', ''));
34
+ }
35
+ });
36
+ return parsedValue;
37
+ }
38
+
39
+ function parseCalcValue(calcValue) {
40
+ // Remove "calc(" and ")" from the string
41
+ const value = calcValue.replace(/calc\(/gi, '').replace(/\)/gi, '').trim(); // Split the string by "*" or "/"
42
+
43
+ const parts = value.split(/[\\/*]/); // Parse the first part as a number
44
+
45
+ const num1 = parts[0].trim(); // Parse the second part as a number
46
+
47
+ const num2 = parts[1].trim();
48
+ return {
49
+ valOne: num1,
50
+ valTwo: num2
51
+ };
52
+ }
53
+
54
+ function convertToCalc(pxReplacement, value) {
55
+ const parsedValue = parseFloat(value, 10);
56
+ const sign = parsedValue < 0 ? '-' : '';
57
+ const varName = `${parsedValue < 0 ? parsedValue * -1 : parsedValue}`;
58
+ return `calc( ${pxReplacement.replace('$$', varName)} * ${sign}1 )`;
59
+ } // const avoidProps = [];
29
60
  // -- is issue IO --
30
61
 
31
62
  /*
@@ -39,6 +70,7 @@ comment :
39
70
  do not execute when --zd_size comes as prop
40
71
  */
41
72
 
73
+
42
74
  function isIgnoreValuePresent(ignoreVals, prop) {
43
75
  let present = false;
44
76
  ignoreVals.forEach(issue => {
@@ -89,6 +121,25 @@ function variableConvertor(rootOriginal, variables, settingsObject) {
89
121
  const valArr = decl.value.split(' '); // single values are considered in the above array and converted below
90
122
 
91
123
  valArr.forEach((value, index) => {
124
+ if (value.includes('px')) {
125
+ if (value.includes('calc')) {
126
+ const res = convertCalcValue(pxReplacement, parseCalcValue(value));
127
+ valArr[index] = `calc( ${res.valOne.trim()} * ${res.valTwo.trim()} )`;
128
+ return;
129
+ }
130
+
131
+ if (/-(\d+)/gi.test(value)) {
132
+ valArr[index] = convertToCalc(pxReplacement, value);
133
+ return;
134
+ }
135
+
136
+ const num = value.replace('px', '');
137
+
138
+ if (value) {
139
+ valArr[index] = pxReplacement.replace('$$', num);
140
+ }
141
+ }
142
+
92
143
  if (value.includes('px')) {
93
144
  const num = value.replace('px', '');
94
145
  valArr[index] = pxReplacement.replace('$$', num);
@@ -89,6 +89,7 @@ function pxToCalc(value) {
89
89
  }
90
90
  });
91
91
  });
92
+ console.log(arr.join(' '));
92
93
  return arr.join(' ');
93
94
  }
94
95
 
@@ -38,6 +38,37 @@ function isIgnoreValuePresent(ignoreVals, prop) {
38
38
  return present;
39
39
  }
40
40
 
41
+ function convertCalcValue(pxReplacement, parsedValue) {
42
+ Object.keys(parsedValue).forEach(key => {
43
+ if (parsedValue[key].includes('px')) {
44
+ parsedValue[key] = pxReplacement.replace('$$', parsedValue[key].replace('px', ''));
45
+ }
46
+ });
47
+ return parsedValue;
48
+ }
49
+
50
+ function parseCalcValue(calcValue) {
51
+ // Remove "calc(" and ")" from the string
52
+ const value = calcValue.replace(/calc\(/gi, '').replace(/\)/gi, '').trim(); // Split the string by "*" or "/"
53
+
54
+ const parts = value.split(/[\\/*]/); // Parse the first part as a number
55
+
56
+ const num1 = parts[0].trim(); // Parse the second part as a number
57
+
58
+ const num2 = parts[1].trim();
59
+ return {
60
+ valOne: num1,
61
+ valTwo: num2
62
+ };
63
+ }
64
+
65
+ function convertToCalc(pxReplacement, value) {
66
+ const parsedValue = parseFloat(value, 10);
67
+ const sign = parsedValue < 0 ? '-' : '';
68
+ const varName = `${parsedValue < 0 ? parsedValue * -1 : parsedValue}`;
69
+ return `calc( ${pxReplacement.replace('$$', varName)} * ${sign}1 )`;
70
+ }
71
+
41
72
  function variableConverter(rootOriginal, variables, settingsObject) {
42
73
  rootOriginal.walkRules(rule => {
43
74
  rule.nodes.forEach((decl, index) => {
@@ -63,6 +94,25 @@ function variableConverter(rootOriginal, variables, settingsObject) {
63
94
  const valArr = decl.value.split(' '); // single values are considered in the above array and converted below
64
95
 
65
96
  valArr.forEach((value, index) => {
97
+ if (value.includes('px')) {
98
+ if (value.includes('calc')) {
99
+ const res = convertCalcValue(pxReplacement, parseCalcValue(value));
100
+ valArr[index] = `calc( ${res.valOne.trim()} * ${res.valTwo.trim()} )`;
101
+ return;
102
+ }
103
+
104
+ if (/-(\d+)/gi.test(value)) {
105
+ valArr[index] = convertToCalc(pxReplacement, value);
106
+ return;
107
+ }
108
+
109
+ const num = value.replace('px', '');
110
+
111
+ if (value) {
112
+ valArr[index] = pxReplacement.replace('$$', num);
113
+ }
114
+ }
115
+
66
116
  if (value.includes('px')) {
67
117
  const num = value.replace('px', '');
68
118
  valArr[index] = pxReplacement.replace('$$', num);
@@ -106,7 +156,6 @@ function watchHandler(fromPath, toPath) {
106
156
  // strictMode
107
157
 
108
158
  } = data;
109
- const oldCode = rootOriginal.toString();
110
159
  rootOriginal.walkRules(rule => {
111
160
  rule.walkDecls(decl => {
112
161
  decl.value.split(' ').forEach(val => {
@@ -131,24 +180,15 @@ function watchHandler(fromPath, toPath) {
131
180
  return;
132
181
  }
133
182
 
134
- const convertedCode = variableConverter(rootOriginal, variables, settingsObject).toString();
135
-
136
- if (convertedCode.trim() !== '') {
137
- fs.writeFileSync(fromPath, convertedCode);
138
- } // console.log(convertedCode);
139
-
183
+ const convertedCode = variableConverter(rootOriginal, variables, settingsObject).toString(); // if (convertedCode.trim() !== '') {
184
+ // fs.writeFileSync(fromPath, convertedCode);
185
+ // }
186
+ // console.log(convertedCode);
140
187
 
141
- postcss([postcssVariableConvertor(cssVariableOptions)]).process(css, {
142
- from: fromPath,
143
- to: toPath
188
+ postcss([postcssVariableConvertor(cssVariableOptions)]).process(convertedCode, {
189
+ from: undefined
144
190
  }).then(result => {
145
191
  fs.writeFileSync(toPath, result.css);
146
-
147
- if (result.map) {
148
- fs.writeFileSync(`${toPath}.map`, result.map);
149
- }
150
- }).then(() => {
151
- fs.writeFileSync(fromPath, oldCode);
152
192
  }); // console.log(variableOptions);
153
193
  }
154
194
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zohodesk/react-cli",
3
- "version": "1.0.3-exp.3",
3
+ "version": "1.0.3-exp.5",
4
4
  "description": "A CLI tool for build modern web application and libraries",
5
5
  "scripts": {
6
6
  "init": "node ./lib/utils/init.js",