11#!/usr/bin/env node
22import imageminZopfli from 'imagemin-zopfli' ;
33import { optimize } from 'svgo' ;
4- import { fabric } from 'fabric' ;
4+ import { loadSVGFromString , Canvas , Rect , util } from 'fabric/node ' ;
55import { readFile , writeFile } from 'node:fs/promises' ;
6+ import { argv , exit } from 'node:process' ;
67
7- function exit ( err ) {
8+ function doExit ( err ) {
89 if ( err ) console . error ( err ) ;
9- process . exit ( err ? 1 : 0 ) ;
10- }
11-
12- function loadSvg ( svg ) {
13- return new Promise ( ( resolve ) => {
14- fabric . loadSVGFromString ( svg , ( objects , options ) => {
15- resolve ( { objects, options} ) ;
16- } ) ;
17- } ) ;
10+ exit ( err ? 1 : 0 ) ;
1811}
1912
2013async function generate ( svg , path , { size, bg} ) {
@@ -35,14 +28,14 @@ async function generate(svg, path, {size, bg}) {
3528 return ;
3629 }
3730
38- const { objects, options} = await loadSvg ( svg ) ;
39- const canvas = new fabric . Canvas ( ) ;
31+ const { objects, options} = await loadSVGFromString ( svg ) ;
32+ const canvas = new Canvas ( ) ;
4033 canvas . setDimensions ( { width : size , height : size } ) ;
4134 const ctx = canvas . getContext ( '2d' ) ;
4235 ctx . scale ( options . width ? ( size / options . width ) : 1 , options . height ? ( size / options . height ) : 1 ) ;
4336
4437 if ( bg ) {
45- canvas . add ( new fabric . Rect ( {
38+ canvas . add ( new Rect ( {
4639 left : 0 ,
4740 top : 0 ,
4841 height : size * ( 1 / ( size / options . height ) ) ,
@@ -51,7 +44,7 @@ async function generate(svg, path, {size, bg}) {
5144 } ) ) ;
5245 }
5346
54- canvas . add ( fabric . util . groupSVGElements ( objects , options ) ) ;
47+ canvas . add ( util . groupSVGElements ( objects , options ) ) ;
5548 canvas . renderAll ( ) ;
5649
5750 let png = Buffer . from ( [ ] ) ;
@@ -64,7 +57,7 @@ async function generate(svg, path, {size, bg}) {
6457}
6558
6659async function main ( ) {
67- const gitea = process . argv . slice ( 2 ) . includes ( 'gitea' ) ;
60+ const gitea = argv . slice ( 2 ) . includes ( 'gitea' ) ;
6861 const logoSvg = await readFile ( new URL ( '../assets/logo.svg' , import . meta. url ) , 'utf8' ) ;
6962 const faviconSvg = await readFile ( new URL ( '../assets/favicon.svg' , import . meta. url ) , 'utf8' ) ;
7063
@@ -80,7 +73,7 @@ async function main() {
8073}
8174
8275try {
83- exit ( await main ( ) ) ;
76+ doExit ( await main ( ) ) ;
8477} catch ( err ) {
85- exit ( err ) ;
78+ doExit ( err ) ;
8679}
0 commit comments