utils.js 3.6 KB

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