@web-padawan/wc-icons-tool 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,5 +7,5 @@ CLI tool for the [Vaadin web components](https://github.com/vaadin/web-component
7
7
  Create iconset for `packages/icons` and `packages/vaadin-lumo-styles`:
8
8
 
9
9
  ```sh
10
- npx wc-icons-tool
10
+ npx @web-padawan/wc-icons-tool
11
11
  ```
package/bin/wc-icons-tool CHANGED
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import { generateIcons } from '../tasks/icons.js';
3
- import { generateLumoFont } from '../tasks/lumo.js';
3
+ import { generateLumoFont, generateLumoIconset } from '../tasks/lumo.js';
4
4
 
5
5
  async function main() {
6
6
  await generateIcons();
7
+ await generateLumoIconset();
7
8
  await generateLumoFont();
8
9
  }
9
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@web-padawan/wc-icons-tool",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "main": "./bin/wc-icons-tool",
5
5
  "bin": {
6
6
  "wc-icons-tool": "./bin/wc-icons-tool"
@@ -25,8 +25,9 @@
25
25
  "cheerio": "^1.0.0",
26
26
  "glob": "^13.0.0",
27
27
  "patch-package": "^8.0.1",
28
- "svgicons2svgfont": "^14.0.2",
29
28
  "svg2ttf": "6.0.3",
29
+ "svgicons2svgfont": "^14.0.2",
30
+ "svgpath": "^2.6.0",
30
31
  "ttf2woff": "3.0.0"
31
32
  }
32
33
  }
package/tasks/lumo.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { execSync } from 'node:child_process';
2
- import path from 'node:path';
2
+ import { basename, normalize } from 'node:path';
3
3
  import { readFileSync, unlinkSync, writeFileSync } from 'node:fs';
4
4
  import { dirname } from 'node:path';
5
5
  import { fileURLToPath } from 'node:url';
6
+ import { globSync } from 'glob';
6
7
  import * as cheerio from 'cheerio';
8
+ import svgpath from 'svgpath';
7
9
 
8
10
  const __dirname = dirname(fileURLToPath(import.meta.url));
9
11
 
@@ -15,11 +17,81 @@ function createCopyright() {
15
17
  */`;
16
18
  }
17
19
 
20
+ /**
21
+ * Normalize file sort order across platforms (OS X vs Linux, maybe others).
22
+ *
23
+ * Before: `[..., 'eye-disabled', 'eye', ...]`
24
+ * After: `[..., 'eye', 'eye-disabled', ...]`
25
+ *
26
+ * Order of appearance impacts assigned Unicode codepoints, and sometimes build diffs.
27
+ *
28
+ * @see https://github.com/nfroidure/svgicons2svgfont/pull/82
29
+ * @see https://github.com/nfroidure/svgicons2svgfont/blob/master/src/filesorter.js
30
+ * @see http://support.ecisolutions.com/doc-ddms/help/reportsmenu/ascii_sort_order_chart.htm
31
+ */
32
+ function sortIconFilesNormalized(file1, file2) {
33
+ return file1
34
+ .replace(/-/gu, '~')
35
+ .localeCompare(file2.replace(/-/gu, '~'), 'en-US');
36
+ }
37
+
38
+ function createIconset() {
39
+ const filenames = globSync(
40
+ `${process.cwd()}/packages/vaadin-lumo-styles/icons/svg/*.svg`
41
+ );
42
+
43
+ filenames.sort(sortIconFilesNormalized);
44
+
45
+ let output = `<svg xmlns="http://www.w3.org/2000/svg"><defs>\n`;
46
+ filenames.forEach((file) => {
47
+ const content = readFileSync(file, 'utf-8');
48
+ const path = content.match(
49
+ /<path( fill-rule="evenodd" clip-rule="evenodd")* d="([^"]*)"/u
50
+ );
51
+ const filename = basename(file);
52
+ if (path) {
53
+ const newPath = new svgpath(path[2])
54
+ .scale(1000 / 24, 1000 / 24)
55
+ .round(0)
56
+ .toString();
57
+ const name = filename
58
+ .replace('.svg', '')
59
+ .replace(/\s/gu, '-')
60
+ .toLowerCase();
61
+ const attrs = path[1] !== undefined ? path[1] : '';
62
+ output += `<g id="lumo:${name}"><path d="${newPath}"${attrs}></path></g>\n`;
63
+ } else {
64
+ throw new Error(`Unexpected SVG content: ${filename}`);
65
+ }
66
+ });
67
+
68
+ output += `</defs></svg>`;
69
+ return output;
70
+ }
71
+
72
+ export function generateLumoIconset() {
73
+ const iconset = `${createCopyright()}
74
+ import './version.js';
75
+ import { Iconset } from '@vaadin/icon/vaadin-iconset.js';
76
+
77
+ const template = document.createElement('template');
78
+
79
+ template.innerHTML = \`${createIconset()}\`;
80
+
81
+ Iconset.register('lumo', 1000, template);\n`;
82
+
83
+ writeFileSync(
84
+ `${process.cwd()}/packages/vaadin-lumo-styles/vaadin-iconset.js`,
85
+ iconset,
86
+ 'utf-8'
87
+ );
88
+ }
89
+
18
90
  export function generateLumoFont() {
19
91
  const FONT = `${process.cwd()}/packages/vaadin-lumo-styles/lumo-icons`;
20
92
 
21
93
  // Create SVG font
22
- const svgIcons2Font = path.normalize(
94
+ const svgIcons2Font = normalize(
23
95
  `${__dirname}/../node_modules/.bin/svgicons2svgfont`
24
96
  );
25
97
  execSync(
@@ -27,11 +99,11 @@ export function generateLumoFont() {
27
99
  );
28
100
 
29
101
  // Convert SVG to TTF
30
- const svg2TTF = path.normalize(`${__dirname}/../node_modules/.bin/svg2ttf`);
102
+ const svg2TTF = normalize(`${__dirname}/../node_modules/.bin/svg2ttf`);
31
103
  execSync(`${svg2TTF} --ts=1 ${FONT}.svg ${FONT}.ttf`);
32
104
 
33
105
  // Convert TTF to WOFF
34
- const ttf2WOFF = path.normalize(`${__dirname}/../node_modules/.bin/ttf2woff`);
106
+ const ttf2WOFF = normalize(`${__dirname}/../node_modules/.bin/ttf2woff`);
35
107
  execSync(`${ttf2WOFF} ${FONT}.ttf ${FONT}.woff`);
36
108
 
37
109
  const content = readFileSync(`${FONT}.svg`, 'utf-8');