utils.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. const fs = require('fs');
  2. function getVariableType(value) {
  3. if (value.startsWith('hsl')) {
  4. return 'color';
  5. } else if (value.startsWith('$')) {
  6. return 'variable';
  7. } else if (value.startsWith('BlinkMacSystemFont') || value == 'monospace') {
  8. return 'font-family';
  9. } else if (value == 'true' || value == 'false') {
  10. return 'boolean';
  11. } else if (value.includes('+')) {
  12. return 'computed';
  13. } else if (value.endsWith('px') || value.endsWith('rem')) {
  14. return 'size';
  15. }
  16. return 'string';
  17. }
  18. function parseLine(line) {
  19. if (line.startsWith('$') && line.endsWith('!default')) {
  20. const colon_index = line.indexOf(':');
  21. const variable_name = line.substring(0, colon_index).trim();
  22. const default_index = line.indexOf('!default');
  23. const variable_value = line.substring(colon_index + 1, default_index).trim();
  24. return variable = {
  25. name: variable_name,
  26. value: variable_value,
  27. type: getVariableType(variable_value),
  28. };
  29. }
  30. return false;
  31. }
  32. function getLines(files, file_path) {
  33. const file = files[file_path];
  34. const slash_index = file_path.lastIndexOf('/');
  35. const dot_index = file_path.lastIndexOf('.');
  36. const file_name = file_path.substring(slash_index + 1, dot_index);
  37. return {
  38. file_name,
  39. lines: file.contents.toString().split(/(?:\r\n|\r|\n)/g),
  40. }
  41. }
  42. function writeFile(file_path, data) {
  43. const json_data = JSON.stringify(data, null, ' ');
  44. fs.writeFile(file_path, json_data, err => {
  45. if (err) {
  46. return console.log(err);
  47. }
  48. console.log('The file was saved!');
  49. });
  50. }
  51. let plugin = {};
  52. plugin.getVariableType = getVariableType;
  53. plugin.getLines = getLines;
  54. plugin.parseLine = parseLine;
  55. plugin.writeFile = writeFile;
  56. module.exports = plugin;