Merged in fix/update-icons-script-for-partner-sas (pull request #2548)

fix: Update material-symbols-update script to also update partner-sas

* Update material-symbols-update script to also update partner-sas


Approved-by: Matilda Landström
This commit is contained in:
Anton Gunnarsson
2025-07-11 07:07:53 +00:00
parent 2fee29f3f9
commit e1d85ae8b3
3 changed files with 59 additions and 56 deletions

View File

@@ -1,19 +1,20 @@
// @ts-check // @ts-check
import crypto from 'node:crypto'; import crypto from 'node:crypto'
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'; import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import { createWriteStream } from 'node:fs'; import { createWriteStream } from 'node:fs'
import { resolve, join } from 'node:path'; import { resolve, join } from 'node:path'
import { Readable } from 'node:stream'; import { Readable } from 'node:stream'
import { pipeline } from 'node:stream/promises'; import { pipeline } from 'node:stream/promises'
import stringify from 'json-stable-stringify-without-jsonify'; import stringify from 'json-stable-stringify-without-jsonify'
// Defines where the font lives // Defines where the font lives
const DESIGN_SYSTEM_FONT_DIR = `./packages/design-system/public/_static/fonts/material-symbols`; const DESIGN_SYSTEM_FONT_DIR = `./packages/design-system/public/_static/fonts/material-symbols`
const WEB_FONT_DIR = `./apps/scandic-web/public/_static/fonts/material-symbols`; const WEB_FONT_DIR = `./apps/scandic-web/public/_static/fonts/material-symbols`
const SAS_FONT_DIR = `./apps/partner-sas/public/_static/fonts/material-symbols`
// Defines the settings for the font // Defines the settings for the font
const FONT_BASE_URL = `https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0..1,0`; const FONT_BASE_URL = `https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@24,400,0..1,0`
// Defines the subset of icons for the font // Defines the subset of icons for the font
const icons = [ const icons = [
@@ -217,16 +218,16 @@ const icons = [
'water_full', 'water_full',
'wifi', 'wifi',
'yard', 'yard',
].sort(); ].sort()
function createHash(value) { function createHash(value) {
const stringified = stringify(value); const stringified = stringify(value)
const hash = crypto.createHash('sha256'); const hash = crypto.createHash('sha256')
hash.update(stringified); hash.update(stringified)
return hash.digest('hex'); return hash.digest('hex')
} }
const hash = createHash(icons).substring(0, 8); const hash = createHash(icons).substring(0, 8)
async function fetchIconUrl(url) { async function fetchIconUrl(url) {
const response = await fetch(url, { const response = await fetch(url, {
@@ -236,71 +237,74 @@ async function fetchIconUrl(url) {
'User-Agent': 'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
}, },
}); })
if (!response.ok) { if (!response.ok) {
console.error(`Unable to fetch woff2 for ${url}`); console.error(`Unable to fetch woff2 for ${url}`)
process.exit(1); process.exit(1)
} }
const text = await response.text(); const text = await response.text()
const isWoff2 = /format\('woff2'\)/.test(text); const isWoff2 = /format\('woff2'\)/.test(text)
if (!isWoff2) { if (!isWoff2) {
console.error(`Unable to identify woff2 font in response`); console.error(`Unable to identify woff2 font in response`)
process.exit(1); process.exit(1)
} }
const srcUrl = text.match(/src: url\(([^)]+)\)/); const srcUrl = text.match(/src: url\(([^)]+)\)/)
if (srcUrl && srcUrl[1]) { if (srcUrl && srcUrl[1]) {
return srcUrl[1]; return srcUrl[1]
} }
return null; return null
} }
async function download(url, destFolder) { async function download(url, destFolder) {
const dest = resolve(join(destFolder, `/rounded-${hash}.woff2`)); const dest = resolve(join(destFolder, `/rounded-${hash}.woff2`))
try { try {
const response = await fetch(url); const response = await fetch(url)
if (!response.ok) { if (!response.ok) {
console.error(`Unable to fetch ${url}`); console.error(`Unable to fetch ${url}`)
process.exit(1); process.exit(1)
} }
if (!response.body) { if (!response.body) {
console.error(`Bad response from ${url}`); console.error(`Bad response from ${url}`)
process.exit(1); process.exit(1)
} }
const fileStream = createWriteStream(dest); const fileStream = createWriteStream(dest)
// @ts-expect-error: type mismatch // @ts-expect-error: type mismatch
const readableNodeStream = Readable.fromWeb(response.body); const readableNodeStream = Readable.fromWeb(response.body)
await pipeline(readableNodeStream, fileStream); await pipeline(readableNodeStream, fileStream)
} catch (error) { } catch (error) {
console.error(`Error downloading file from ${url}:`, error); console.error(`Error downloading file from ${url}:`, error)
process.exit(1); process.exit(1)
} }
} }
async function cleanFontDirs(folderPath) { async function cleanFontDirs(folderPath) {
await rm(DESIGN_SYSTEM_FONT_DIR, { recursive: true, force: true }); await rm(DESIGN_SYSTEM_FONT_DIR, { recursive: true, force: true })
await mkdir(DESIGN_SYSTEM_FONT_DIR, { recursive: true }); await mkdir(DESIGN_SYSTEM_FONT_DIR, { recursive: true })
await rm(WEB_FONT_DIR, { recursive: true, force: true }); await rm(WEB_FONT_DIR, { recursive: true, force: true })
await mkdir(WEB_FONT_DIR, { recursive: true }); await mkdir(WEB_FONT_DIR, { recursive: true })
await rm(SAS_FONT_DIR, { recursive: true, force: true })
await mkdir(SAS_FONT_DIR, { recursive: true })
} }
async function updateFontCSS() { async function updateFontCSS() {
const file = './packages/design-system/lib/fonts.css'; const file = './packages/design-system/lib/fonts.css'
const css = await readFile(file, { const css = await readFile(file, {
encoding: 'utf-8', encoding: 'utf-8',
}); })
await writeFile( await writeFile(
file, file,
@@ -311,29 +315,28 @@ async function updateFontCSS() {
{ {
encoding: 'utf-8', encoding: 'utf-8',
} }
); )
} }
async function main() { async function main() {
const fontUrl = `${FONT_BASE_URL}&icon_names=${icons.join(',')}&display=block`; const fontUrl = `${FONT_BASE_URL}&icon_names=${icons.join(',')}&display=block`
const iconUrl = await fetchIconUrl(fontUrl); const iconUrl = await fetchIconUrl(fontUrl)
if (iconUrl) { if (iconUrl) {
await cleanFontDirs(); await cleanFontDirs()
await download(iconUrl, DESIGN_SYSTEM_FONT_DIR); await download(iconUrl, DESIGN_SYSTEM_FONT_DIR)
await download(iconUrl, WEB_FONT_DIR); await download(iconUrl, WEB_FONT_DIR)
await download(iconUrl, SAS_FONT_DIR)
await updateFontCSS(); await updateFontCSS()
console.log('Successfully updated icons!'); console.log('Successfully updated icons!')
process.exit(0); process.exit(0)
} else { } else {
console.error( console.error(`Unable to find the icon font src URL in CSS response from Google Fonts at ${fontUrl}`)
`Unable to find the icon font src URL in CSS response from Google Fonts at ${fontUrl}`
);
} }
} }
main(); main()