@web-padawan/wc-icons-tool 0.1.1 → 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.1",
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,7 +1,13 @@
1
1
  import { execSync } from 'node:child_process';
2
- import path from 'node:path';
3
- import { readFileSync, unlinkSync, writeFileSync } from 'node:fs';
2
+ import { basename, normalize } from 'node:path';
3
+ import { readFileSync, unlinkSync, writeFileSync } from 'node:fs';
4
+ import { dirname } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+ import { globSync } from 'glob';
4
7
  import * as cheerio from 'cheerio';
8
+ import svgpath from 'svgpath';
9
+
10
+ const __dirname = dirname(fileURLToPath(import.meta.url));
5
11
 
6
12
  function createCopyright() {
7
13
  return `/**
@@ -11,21 +17,93 @@ function createCopyright() {
11
17
  */`;
12
18
  }
13
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
+
14
90
  export function generateLumoFont() {
15
91
  const FONT = `${process.cwd()}/packages/vaadin-lumo-styles/lumo-icons`;
16
92
 
17
93
  // Create SVG font
18
- const svgIcons2Font = path.normalize('./node_modules/.bin/svgicons2svgfont');
94
+ const svgIcons2Font = normalize(
95
+ `${__dirname}/../node_modules/.bin/svgicons2svgfont`
96
+ );
19
97
  execSync(
20
98
  `${svgIcons2Font} --fontname=lumo-icons --height=1000 --ascent=850 --descent=150 --normalize --fixedWidth --verbose -o ${FONT}.svg ${process.cwd()}/packages/vaadin-lumo-styles/icons/svg/*.svg`
21
99
  );
22
100
 
23
101
  // Convert SVG to TTF
24
- const svg2TTF = path.normalize('./node_modules/.bin/svg2ttf');
102
+ const svg2TTF = normalize(`${__dirname}/../node_modules/.bin/svg2ttf`);
25
103
  execSync(`${svg2TTF} --ts=1 ${FONT}.svg ${FONT}.ttf`);
26
104
 
27
105
  // Convert TTF to WOFF
28
- const ttf2WOFF = path.normalize('./node_modules/.bin/ttf2woff');
106
+ const ttf2WOFF = normalize(`${__dirname}/../node_modules/.bin/ttf2woff`);
29
107
  execSync(`${ttf2WOFF} ${FONT}.ttf ${FONT}.woff`);
30
108
 
31
109
  const content = readFileSync(`${FONT}.svg`, 'utf-8');
@@ -51,7 +129,9 @@ export function generateLumoFont() {
51
129
  const outputCSS = `
52
130
  @font-face {
53
131
  font-family: 'lumo-icons';
54
- src: url(data:application/font-woff;charset=utf-8;base64,${lumoIconsWoff.toString('base64')})
132
+ src: url(data:application/font-woff;charset=utf-8;base64,${lumoIconsWoff.toString(
133
+ 'base64'
134
+ )})
55
135
  format('woff');
56
136
  font-weight: normal;
57
137
  font-style: normal;
@@ -64,11 +144,17 @@ export function generateLumoFont() {
64
144
  `;
65
145
 
66
146
  // Write the output to src/props/icons.css
67
- writeFileSync(`${process.cwd()}/packages/vaadin-lumo-styles/src/props/icons.css`, [createCopyright(), outputCSS.trimStart()].join('\n'));
147
+ writeFileSync(
148
+ `${process.cwd()}/packages/vaadin-lumo-styles/src/props/icons.css`,
149
+ [createCopyright(), outputCSS.trimStart()].join('\n')
150
+ );
68
151
 
69
152
  // Write the list of glyphs for visual tests
70
153
  const list = glyphs.map((g) => g.name);
71
- writeFileSync(`${process.cwd()}/packages/vaadin-lumo-styles/test/glyphs.json`, JSON.stringify(list, null, 2));
154
+ writeFileSync(
155
+ `${process.cwd()}/packages/vaadin-lumo-styles/test/glyphs.json`,
156
+ JSON.stringify(list, null, 2)
157
+ );
72
158
 
73
159
  // Cleanup temporary font files
74
160
  unlinkSync(`${FONT}.svg`);