Skip to content

Commit 09e78c9

Browse files
committed
Fixed icon index order on windows during build
1 parent 0c1aaf5 commit 09e78c9

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

tools/build/index.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
concat,
2828
defer,
2929
from,
30+
identity,
3031
map,
3132
merge,
3233
mergeMap,
@@ -132,15 +133,52 @@ let assurePosixSep = function (p: string): string {
132133
* @param p - string path
133134
* @returns String path
134135
*/
135-
const winSepRegex = new RegExp(`\\${path.win32.sep}`, "g");
136136
function assurePosixSepWin(p: string): string {
137137
return p.replace(winSepRegex, path.posix.sep)
138138
};
139139

140+
const winSepRegex = new RegExp(`\\${path.win32.sep}`, "g");
141+
140142
if (path.sep === path.win32.sep) {
141143
assurePosixSep = assurePosixSepWin;
142144
}
143145

146+
/**
147+
* Compare two path strings to decide on the order
148+
*
149+
* On Windows the default order of paths containing `_` from the resolve function
150+
* is different than on macOS. This function restores the order to the usual.
151+
* Implementation adapted based on https://t.ly/VJp78
152+
*
153+
* @param reference Left string to compare
154+
* @param compare Right string to compare against
155+
* @returns Number for the sort function to define the order
156+
*/
157+
function windowsPathSorter(reference: string, compare: string): number {
158+
reference = reference.toLowerCase();
159+
compare = compare.toLowerCase();
160+
161+
const length = Math.min(reference.length, compare.length);
162+
163+
for (let i = 0; i < length; i++) {
164+
const leftChar = reference[i];
165+
const rightChar = compare[i];
166+
167+
if (leftChar !== rightChar)
168+
return customAlphabet.indexOf(leftChar) - customAlphabet.indexOf(rightChar);
169+
}
170+
171+
if (reference.length !== compare.length) {
172+
if (compare.startsWith(reference) && compare[reference.length] === "-")
173+
return 1;
174+
return reference.length - compare.length;
175+
}
176+
177+
return 0;
178+
}
179+
180+
const customAlphabet: string = "_,-.0123456789abcdefghijklmnopqrstuvwxyz";
181+
144182
/* ----------------------------------------------------------------------------
145183
* Tasks
146184
* ------------------------------------------------------------------------- */
@@ -322,7 +360,13 @@ const icons$ = defer(() => resolve("**/*.svg", {
322360
reduce((index, file) => index.set(
323361
file.replace(/\.svg$/, "").replace(new RegExp(`\\${path.sep}`, "g"), "-"),
324362
assurePosixSep(file)
325-
), new Map<string, string>())
363+
), new Map<string, string>()),
364+
// The icons are stored in the index file, and the output needs to be OS
365+
// agnostic. Some icons contain the `_` character, which has different order
366+
// in the glob output in Windows.
367+
(path.sep === path.win32.sep) ? map(icons => new Map(
368+
[...icons].sort((a, b) => windowsPathSorter(a[0], b[0]))
369+
)) : identity
326370
)
327371

328372
/* Compute emoji mappings (based on Twemoji) */

0 commit comments

Comments
 (0)