Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F8929335
get-fonts.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
get-fonts.ts
View Options
import
{
mkdir
,
readFile
,
writeFile
,
unlink
,
rmdir
}
from
"fs/promises"
;
import
{
constructURL
,
download
}
from
"google-fonts-helper"
;
import
{
resolve
}
from
"node:path"
;
import
{
target_locreq
}
from
"./target-locreq.js"
;
import
{
exec
}
from
"child_process"
;
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
promisify
}
from
"util"
;
import
_locreq
from
"locreq"
;
const
locreq
=
_locreq
(
import
.
meta
.
dirname
);
const
path_to_fonsta
=
locreq
.
resolve
(
"node_modules/fonsta/bin/fonsta"
);
import
JSON5
from
"json5"
;
export
const
FONTS_CONFIG_PATH
=
"src/fonts.json"
;
const
execPromise
=
promisify
(
exec
);
export
async
function
getFonts
(
_
:
Record
<
string
,
string
|
boolean
>
=
{})
{
const
fonts_config_path
=
target_locreq
.
resolve
(
FONTS_CONFIG_PATH
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const
fonts_config
=
JSON5
.
parse
(
await
readFile
(
fonts_config_path
,
"utf8"
));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
const
google_fonts
=
constructURL
(
fonts_config
.
googleFonts
);
if
(
!
google_fonts
)
{
throw
new
Error
(
"Invalid font config"
);
}
const
downloader
=
download
(
google_fonts
,
{
base64
:
false
,
overwriting
:
false
,
outputDir
:
target_locreq
.
resolve
(
"public/dist/fonts"
),
stylePath
:
"fonts.css"
,
fontsDir
:
"./"
,
fontsPath
:
"/dist/fonts"
,
});
await
downloader
.
execute
();
console
.
log
(
"Downloaded new fonts pack from google fonts"
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const
squirrel_fonts
=
fonts_config
.
fontSquirrel
;
if
(
!
is
(
squirrel_fonts
,
predicates
.
record
(
predicates
.
string
,
predicates
.
array
(
predicates
.
string
)
)
)
)
{
throw
new
Error
(
"Inocorrect squirrelfonts file"
);
}
await
mkdir
(
target_locreq
.
resolve
(
"public/dist/fontsquirrel/"
),
{
recursive
:
true
,
});
const
config_file
=
`{
"tmpDir": "/tmp/fonts",
"fontsDir": "/dist/fontsquirrel/fonts",
"cssDir": "/dist/fontsquirrel/css",
"cssFile": "fonts.css"
}`
;
await
writeFile
(
"public/fonsta.config.json"
,
config_file
);
await
Promise
.
all
(
Object
.
keys
(
squirrel_fonts
).
map
((
font
)
=>
{
if
(
squirrel_fonts
[
font
].
length
)
{
return
execPromise
(
`node
${
path_to_fonsta
}
install
${
font
}
:
${
squirrel_fonts
[
font
].
join
(
","
)
}
`
,
{
cwd
:
target_locreq
.
resolve
(
"public"
),
}
);
}
else
{
return
execPromise
(
`node
${
path_to_fonsta
}
install
${
font
}
`
,
{
cwd
:
target_locreq
.
resolve
(
"public"
),
});
}
})
);
const
fonts_css_file_path
=
resolve
(
target_locreq
.
resolve
(
"public/dist/fonts"
),
"fonts.css"
);
const
content
=
await
readFile
(
fonts_css_file_path
,
"utf-8"
);
await
writeFile
(
fonts_css_file_path
,
content
.
replace
(
/@font-face {/g
,
"@font-face {\nfont-display: swap;"
)
);
await
unlink
(
"public/fonsta.config.json"
);
await
rmdir
(
"public/tmp"
);
console
.
log
(
"Downloaded new fonts pack from Font Squirrel"
);
console
.
log
(
"Downloaded new fonts pack"
);
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Sat, Sep 20, 22:29 (17 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
931938
Default Alt Text
get-fonts.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment