utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. const fs = require('fs');
  2. const path = require('path');
  3. const base_path = '../_data/variables/';
  4. const css_keywords = ['null', 'ease-out', 'optimizeLegibility'];
  5. const regex_unitless = /^([0-9]+\.[0-9]+)$/;
  6. let utils = {
  7. getVariableType: (name, value) => {
  8. if (!value) {
  9. return 'string';
  10. }
  11. if (name == '$sizes') {
  12. return 'map';
  13. }
  14. if (value.startsWith('hsl') || value.startsWith('#') || value.startsWith('rgb')) {
  15. return 'color';
  16. } else if (css_keywords.includes(value)) {
  17. return 'keyword';
  18. } else if (
  19. value.startsWith('findColorInvert')
  20. || value.startsWith('mergeColorMaps')
  21. ) {
  22. return 'function';
  23. } else if (value.startsWith('$')) {
  24. return 'variable';
  25. } else if (
  26. value.startsWith('BlinkMacSystemFont')
  27. || value == 'monospace'
  28. ) {
  29. return 'font-family';
  30. } else if (value == 'true'
  31. || value == 'false'
  32. ) {
  33. return 'boolean';
  34. } else if (value.endsWith('ms')) {
  35. return 'duration';
  36. } else if (value.includes('+')) {
  37. return 'computed';
  38. } else if (
  39. value.endsWith('00')
  40. || value == 'normal'
  41. ) {
  42. return 'font-weight';
  43. } else if (
  44. value.endsWith('px')
  45. || value.endsWith('em')
  46. || value.includes('px ')
  47. || value.includes('em ')
  48. ) {
  49. return 'size';
  50. } else if (value.includes('$')) {
  51. return 'compound';
  52. } else if (value.match(regex_unitless)) {
  53. return 'unitless';
  54. }
  55. return 'string';
  56. },
  57. parseLine: (line) => {
  58. if (line.startsWith('$') && line.endsWith('!default')) {
  59. const colon_index = line.indexOf(':');
  60. const variable_name = line.substring(0, colon_index).trim();
  61. const default_index = line.indexOf('!default');
  62. const variable_value = line.substring(colon_index + 1, default_index).trim();
  63. return variable = {
  64. name: variable_name,
  65. value: variable_value,
  66. type: utils.getVariableType(variable_name, variable_value),
  67. };
  68. }
  69. return false;
  70. },
  71. getLines: (files, file_path) => {
  72. const file = files[file_path];
  73. const slash_index = file_path.lastIndexOf('/');
  74. const dot_index = file_path.lastIndexOf('.');
  75. const file_name = file_path.substring(slash_index + 1, dot_index);
  76. return {
  77. file_name,
  78. lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
  79. }
  80. },
  81. writeFile: (file_path, data) => {
  82. const json_data = JSON.stringify(data, null, ' ');
  83. const json_file_path = base_path + file_path.replace('.sass', '.json');
  84. utils.ensureDirectoryExistence(json_file_path);
  85. fs.writeFile(json_file_path, json_data, err => {
  86. if (err) {
  87. return console.log(err);
  88. }
  89. console.log(`The file ${json_file_path} was saved!`);
  90. });
  91. },
  92. getInitialValue: (value, type, initial_variables) => {
  93. if (value.startsWith('$') && value in initial_variables.by_name) {
  94. const second_value = initial_variables.by_name[value].value;
  95. if (second_value.startsWith('$') && second_value in initial_variables.by_name) {
  96. const third_value = initial_variables.by_name[second_value].value;
  97. return third_value;
  98. }
  99. return second_value;
  100. }
  101. return value;
  102. },
  103. getComputedData: (name, value, type, initial_variables, derived_variables = {}) => {
  104. let computed_value = '';
  105. if (value.startsWith('$')) {
  106. let second_value;
  107. if (value in initial_variables.by_name) {
  108. second_value = initial_variables.by_name[value].value;
  109. } else if (derived_variables.by_name && value in derived_variables.by_name) {
  110. second_value = derived_variables.by_name[value].value;
  111. }
  112. if (second_value && second_value.startsWith('$')) {
  113. let third_value;
  114. if (second_value in initial_variables.by_name) {
  115. third_value = initial_variables.by_name[second_value].value;
  116. } else if (derived_variables.by_name && second_value in derived_variables.by_name) {
  117. third_value = derived_variables.by_name[second_value].value;
  118. }
  119. computed_value = third_value;
  120. } else {
  121. computed_value = second_value;
  122. }
  123. } else if (value.startsWith('findColorInvert')) {
  124. // e.g. $turquoise-invert -> findColorInvert($turquoise)
  125. if (value.endsWith('($yellow)')) {
  126. computed_value = 'rgba(0, 0, 0, 0.7)';
  127. } else {
  128. computed_value = '#fff';
  129. }
  130. }
  131. if (computed_value && computed_value != '') {
  132. // e.g. $primary-invert -> $turquoise-invert -> findColorInvert($turquoise)
  133. if (computed_value.startsWith('findColorInvert')) {
  134. if (computed_value.endsWith('($yellow)')) {
  135. computed_value = 'rgba(0, 0, 0, 0.7)';
  136. } else {
  137. computed_value = '#fff';
  138. }
  139. }
  140. const computed_type = utils.getVariableType(name, computed_value);
  141. return {
  142. computed_type,
  143. computed_value,
  144. }
  145. }
  146. return {};
  147. },
  148. ensureDirectoryExistence: (file_path) => {
  149. var dirname = path.dirname(file_path);
  150. if (fs.existsSync(dirname)) {
  151. return true;
  152. }
  153. utils.ensureDirectoryExistence(dirname);
  154. fs.mkdirSync(dirname);
  155. }
  156. }
  157. utils.files = {};
  158. utils.files.initial_variables = `${base_path}utilities/initial-variables.json`;
  159. utils.files.derived_variables = `${base_path}utilities/derived-variables.json`;
  160. module.exports = utils;