utils.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const fs = require('fs');
  2. const path = require('path');
  3. const base_path = '../_data/variables/';
  4. let utils = {
  5. getVariableType: (value) => {
  6. if (!value) {
  7. return 'string';
  8. }
  9. if (value.startsWith('hsl')) {
  10. return 'color';
  11. } else if (value.startsWith('$')) {
  12. return 'variable';
  13. } else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') {
  14. return 'font-family';
  15. } else if (value == 'true' || value == 'false') {
  16. return 'boolean';
  17. } else if (value.includes('+')) {
  18. return 'computed';
  19. } else if (value.endsWith('00')) {
  20. return 'font-weight';
  21. } else if (value.endsWith('px') || value.endsWith('em')) {
  22. return 'size';
  23. } else if (value.includes('$')) {
  24. return 'compound';
  25. } else if (value.startsWith('findColorInvert')) {
  26. return 'function';
  27. }
  28. return 'string';
  29. },
  30. parseLine: (line) => {
  31. if (line.startsWith('$') && line.endsWith('!default')) {
  32. const colon_index = line.indexOf(':');
  33. const variable_name = line.substring(0, colon_index).trim();
  34. const default_index = line.indexOf('!default');
  35. const variable_value = line.substring(colon_index + 1, default_index).trim();
  36. return variable = {
  37. name: variable_name,
  38. value: variable_value,
  39. type: utils.getVariableType(variable_value),
  40. };
  41. }
  42. return false;
  43. },
  44. getLines: (files, file_path) => {
  45. const file = files[file_path];
  46. const slash_index = file_path.lastIndexOf('/');
  47. const dot_index = file_path.lastIndexOf('.');
  48. const file_name = file_path.substring(slash_index + 1, dot_index);
  49. return {
  50. file_name,
  51. lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
  52. }
  53. },
  54. writeFile: (file_path, data) => {
  55. const json_data = JSON.stringify(data, null, ' ');
  56. const json_file_path = base_path + file_path.replace('.sass', '.json');
  57. utils.ensureDirectoryExistence(json_file_path);
  58. fs.writeFile(json_file_path, json_data, err => {
  59. if (err) {
  60. return console.log(err);
  61. }
  62. console.log(`The file ${json_file_path} was saved!`);
  63. });
  64. },
  65. getInitialValue: (value, type, initial_variables) => {
  66. if (value.startsWith('$') && value in initial_variables.by_name) {
  67. const second_value = initial_variables.by_name[value].value;
  68. if (second_value.startsWith('$') && second_value in initial_variables.by_name) {
  69. const third_value = initial_variables.by_name[second_value].value;
  70. console.log('third_value', third_value);
  71. return third_value;
  72. }
  73. return second_value;
  74. }
  75. return value;
  76. },
  77. getComputedData: (value, type, initial_variables, derived_variables) => {
  78. let computed_value = '';
  79. if (value.startsWith('$')) {
  80. let second_value;
  81. if (value in initial_variables.by_name) {
  82. second_value = initial_variables.by_name[value].value;
  83. } else if (value in derived_variables.by_name) {
  84. second_value = derived_variables.by_name[value].value;
  85. }
  86. if (second_value && second_value.startsWith('$')) {
  87. let third_value;
  88. if (second_value in initial_variables.by_name) {
  89. third_value = initial_variables.by_name[second_value].value;
  90. } else if (second_value in derived_variables.by_name) {
  91. third_value = derived_variables.by_name[second_value].value;
  92. }
  93. computed_value = third_value;
  94. } else {
  95. computed_value = second_value;
  96. }
  97. }
  98. if (computed_value != '') {
  99. const computed_type = utils.getVariableType(computed_value);
  100. return {
  101. computed_type,
  102. computed_value,
  103. }
  104. }
  105. return {};
  106. },
  107. ensureDirectoryExistence: (file_path) => {
  108. var dirname = path.dirname(file_path);
  109. if (fs.existsSync(dirname)) {
  110. return true;
  111. }
  112. utils.ensureDirectoryExistence(dirname);
  113. fs.mkdirSync(dirname);
  114. }
  115. }
  116. utils.files = {};
  117. utils.files.initial_variables = `${base_path}utilities/initial-variables.json`;
  118. utils.files.derived_variables = `${base_path}utilities/derived-variables.json`;
  119. module.exports = utils;