convert.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* eslint-env node */
  2. /*
  3. The following script can be used to convert SVGs exported from illustrator into
  4. a format that's compatible with the shape system. It runs with nodejs. Some
  5. manual conversion may be necessary.
  6. FIXME this script is out-dated.
  7. */
  8. const fs = require('fs');
  9. const path = require('path');
  10. const palette = {
  11. '1': '#3AADAA',
  12. '2': '#7C6576',
  13. '3': '#F6F6F6',
  14. '4': '#FFFFFF',
  15. '5': '#383E45',
  16. };
  17. const positions = ['top', 'left', 'bottom', 'right', 'center', 'stretch'];
  18. const directories = fs.readdirSync(__dirname).filter(nodeName => {
  19. return nodeName[0] !== '.' && fs.lstatSync(path.join(__dirname, nodeName)).isDirectory();
  20. });
  21. const files = directories.flatMap(dirName => {
  22. return fs.readdirSync(path.join(__dirname, dirName))
  23. .filter(fileName => fileName.endsWith('.svg'))
  24. .map(fileName => path.join(__dirname, dirName, fileName));
  25. });
  26. const shapes = [];
  27. files.filter(f => f.endsWith('svg')).forEach(filePath => {
  28. const svg = String(fs.readFileSync(filePath));
  29. const fileName = filePath.match(/([^/]+)$/)[1];
  30. const colors = svg.match(/#[0-9A-F]{3,}/gi);
  31. const nonPaletteColors = colors && colors.filter(color => !Object.values(palette).includes(color.toUpperCase()));
  32. const shape = {
  33. svg,
  34. name: fileName.split(/[.-]/)[0],
  35. page: filePath.slice(__dirname.length + 1, -fileName.length - 1),
  36. colors: Object.keys(palette).filter(num => new RegExp(palette[num], 'i').test(svg)),
  37. position: positions.filter(pos => fileName.includes(pos)),
  38. nonIsometric: fileName.includes('+'),
  39. nonPaletteColors: nonPaletteColors && nonPaletteColors.length ? nonPaletteColors.join(' ') : null,
  40. containsImage: svg.includes('<image'),
  41. repeatX: fileName.includes('repeatx'),
  42. repeatY: fileName.includes('repeaty'),
  43. };
  44. shape.optionXML = `<we-button data-shape="web_editor/${encodeURIComponent(shape.page)}/${encodeURIComponent(shape.name)}" data-select-label="${shape.page} ${shape.name}"/>`;
  45. if (shape.position[0] === 'stretch') {
  46. shape.position = ['center'];
  47. shape.size = '100% 100%';
  48. } else {
  49. shape.size = '100% auto';
  50. }
  51. shape.scss = `'${encodeURIComponent(shape.page)}/${encodeURIComponent(shape.name)}': ('position': ${shape.position[0]}, 'size': ${shape.size}, 'colors': (${shape.colors.join(', ')})${shape.repeatX ? ", 'repeat-x': true" : ""}${shape.repeatY ? ", 'repeat-y': true" : ""})`;
  52. shapes.push(shape);
  53. });
  54. const xml = shapes.map(shape => shape.optionXML).join('\n');
  55. const scss = shapes.map(shape => shape.scss).join(',\n');
  56. const nonConformShapes = shapes.flatMap(shape => {
  57. const violations = {};
  58. let invalid = false;
  59. // Not sure if we want this check, edi still trying to see if she can do shadows without embedding PNGs
  60. // if (shape.containsImage) {
  61. // violations.containsImage = shape.containsImage;
  62. // invalid = true;
  63. // }
  64. if (shape.nonIsometric) {
  65. violations.nonIsometric = shape.nonIsometric;
  66. invalid = true;
  67. }
  68. if (shape.nonPaletteColors) {
  69. violations.nonPaletteColors = shape.nonPaletteColors;
  70. invalid = true;
  71. }
  72. if (shape.position.length > 1 || shape.position.length == 0) {
  73. violations.position = shape.position;
  74. invalid = true;
  75. }
  76. if (!invalid) {
  77. return []
  78. }
  79. return [[shape, violations]];
  80. });
  81. console.log('The following shapes are not conform:', nonConformShapes);
  82. const convertDir = './.converted';
  83. fs.mkdirSync(convertDir);
  84. const convertedPath = path.join(__dirname, convertDir);
  85. fs.writeFileSync(path.join(convertedPath, 'options.xml'), xml);
  86. fs.writeFileSync(path.join(convertedPath, 'variables.scss'), scss);
  87. shapes.forEach(shape => {
  88. const pageDir = path.join(convertedPath, shape.page);
  89. if (!fs.existsSync(pageDir)) {
  90. fs.mkdirSync(pageDir);
  91. }
  92. fs.writeFileSync(path.join(pageDir, shape.name + '.svg'), shape.svg);
  93. });