11#!/usr/bin/env node
22
3- 'use strict'
4-
5- var program = require ( 'commander' ) ;
6- var colors = require ( 'colors' ) ;
7- var GoogleFontList = require ( './lib/google-font-list' ) ;
8- var fontList = new GoogleFontList ( 'AIzaSyB1I5eF2kRqqs50DS8qBJtFkCTMMoQLusg' ) ;
9- var pjson = require ( './package.json' ) ;
10- var ncp = require ( "copy-paste-win32fix" ) ;
11-
12- program
13- . command ( 'search [family...]' )
14- . description ( 'Search for a family' )
15- . action ( function ( family ) {
16- var searchedTerm = family ? family . join ( " " ) : '' ;
17- fontList . on ( 'success' , function ( ) {
18- searchFont ( searchedTerm ) ;
19- } )
20-
21- } )
3+ "use strict" ;
4+
5+ const program = require ( "commander" ) ;
6+ const pc = require ( "picocolors" ) ;
7+ const ncp = require ( "copy-paste-win32fix" ) ;
8+ const GoogleFontList = require ( "./lib/google-font-list" ) ;
9+ const pjson = require ( "./package.json" ) ;
10+
11+ const fontList = new GoogleFontList ( ) ;
12+
13+ /**
14+ * Helper to wrap fontList initialization in a Promise
15+ */
16+ const ensureFontsLoaded = ( ) =>
17+ new Promise ( ( resolve ) => {
18+ if ( fontList . loaded ) return resolve ( ) ;
19+ console . log ( pc . bold ( pc . blue ( "\nDownloading Google Font List...\n" ) ) ) ;
20+ fontList . on ( "success" , resolve ) ;
21+ fontList . on ( "error" , ( err ) => {
22+ console . error ( pc . bold ( pc . red ( "Error loading font list!" ) ) ) ;
23+ console . error ( pc . red ( err . toString ( ) ) ) ;
24+ process . exit ( 1 ) ;
25+ } ) ;
26+ } ) ;
27+
28+ program . version ( pjson . version ) ;
2229
2330program
24- . command ( 'download <family...>' )
25- . option ( '-d, --dest <folder>' , 'Specify where to download' )
26- . option ( '-v, --variants <variants...>' , 'Specify variants/weights to install, separated with comma (and no spaces)' )
27- . description ( 'Download a font family' )
28- . action ( function ( family , options ) {
29- var searchedTerm = family . join ( " " ) ;
30- var variants = options . variants ? options . variants . split ( ',' ) : false ;
31- var path = options . dest || false ;
32- fontList . on ( 'success' , function ( ) {
33- downloadFont ( searchedTerm , variants , path ) ;
34- } )
35- } ) ;
31+ . command ( "search [family...]" )
32+ . description ( "Search for a font family" )
33+ . action ( async ( family ) => {
34+ await ensureFontsLoaded ( ) ;
35+ const term = family ? family . join ( " " ) : "" ;
36+ fontList . searchFontByName ( term , printFontList ) ;
37+ } ) ;
3638
3739program
38- . command ( 'install <family...>' )
39- . option ( '-v, --variants <variants...>' , 'Specify variants/weights to install, separated with comma (and no spaces)' )
40- . description ( 'Install a font family' )
41- . action ( function ( family , options ) {
42- var searchedTerm = family . join ( " " ) ;
43- var variants = options . variants ? options . variants . split ( ',' ) : false ;
44- fontList . on ( 'success' , function ( ) {
45- installFont ( searchedTerm , variants ) ;
46- } )
47- } ) ;
40+ . command ( "download <family...>" )
41+ . description ( "Download a font family" )
42+ . option ( "-d, --dest <folder>" , "Specify destination folder" )
43+ . option ( "-v, --variants <variants>" , "Variants separated by comma" )
44+ . action ( async ( family , options ) => {
45+ await ensureFontsLoaded ( ) ;
46+ const term = family . join ( " " ) ;
47+ const variants = options . variants ? options . variants . split ( "," ) : false ;
48+
49+ fontList . getFontByName ( term , ( err , filteredList ) => {
50+ if ( err || filteredList . data . length !== 1 ) {
51+ handleMatchError ( "Download" , term , err ) ;
52+ return ;
53+ }
54+ filteredList . getFirst ( ) . saveAt ( variants , options . dest , printResult ) ;
55+ } ) ;
56+ } ) ;
4857
4958program
50- . command ( 'copy <family...>' )
51- . option ( '-v, --variants <variants...>' , 'Specify variants/weights to copy clipboard, seperated with comma (and no spaces)' )
52- . description ( 'Copy stylesheet link' )
53- . action ( function ( family , options ) {
54- var searchedTerm = family . join ( " " ) ;
55- var variants = options . variants ? options . variants . split ( ',' ) : false ;
56- fontList . on ( 'success' , function ( ) {
57- copyFont ( searchedTerm , variants ) ;
58- } )
59-
60- } ) ;
59+ . command ( "install <family...>" )
60+ . description ( "Install a font family to the system" )
61+ . option ( "-v, --variants <variants>" , "Variants separated by comma" )
62+ . action ( async ( family , options ) => {
63+ await ensureFontsLoaded ( ) ;
64+ const term = family . join ( " " ) ;
65+ const variants = options . variants ? options . variants . split ( "," ) : false ;
66+
67+ fontList . getFontByName ( term , ( err , filteredList ) => {
68+ if ( err || filteredList . data . length !== 1 ) {
69+ handleMatchError ( "Installation" , term , err ) ;
70+ return ;
71+ }
72+ filteredList . getFirst ( ) . install ( variants , printResult ) ;
73+ } ) ;
74+ } ) ;
6175
6276program
63- . version ( pjson . version ) ;
77+ . command ( "copy <family...>" )
78+ . description ( "Copy Google Fonts stylesheet link to clipboard" )
79+ . option ( "-v, --variants <variants>" , "Variants separated by comma" )
80+ . action ( async ( family , options ) => {
81+ await ensureFontsLoaded ( ) ;
82+ const term = family . join ( " " ) ;
83+ const variants = options . variants ? options . variants . split ( "," ) : false ;
84+
85+ fontList . getFontByName ( term , ( err , filteredList ) => {
86+ if ( err || filteredList . data . length !== 1 ) {
87+ handleMatchError ( "Copy" , term , err ) ;
88+ return ;
89+ }
90+ const font = filteredList . getFirst ( ) ;
91+ const url = variants
92+ ? `${ font . cssUrl } :${ variants . join ( "," ) } `
93+ : font . cssUrl ;
94+
95+ ncp . copy ( url , ( ) => {
96+ console . log ( pc . green ( `"${ term } " CSS URL copied to clipboard.` ) ) ;
97+ } ) ;
98+ } ) ;
99+ } ) ;
64100
65101program . parse ( process . argv ) ;
66102
67-
68- // CHECK IF NO COMMANDS ARE PROVIDED AND PRINT HELP IN NONE
69- var found = false ;
70- program . args . forEach ( function ( arg ) {
71- if ( arg instanceof program . Command )
72- found = true ;
73- } )
74- if ( ! found )
75- program . help ( ) ;
76-
77- // HELPERS FUNCTIONS
78-
79- console . log ( '\nDonwloading Google Font List....\n' . blue . bold ) ;
80-
81- fontList . on ( 'error' , function ( err ) {
82- printError ( err ) ;
83- } )
84-
85- function searchFont ( searchedTerm ) {
86- fontList . searchFontByName ( searchedTerm , printFontList )
103+ // Handle empty commands
104+ if ( program . args . length === 0 ) {
105+ program . help ( ) ;
87106}
88107
89-
90- function downloadFont ( searchedTerm , variants , path ) {
91- fontList . getFontByName ( searchedTerm , function ( err , filteredList ) {
92- if ( err ) {
93- printError ( err ) ;
94- return ;
95- }
96- if ( filteredList . data . length === 1 ) {
97- filteredList . getFirst ( ) . saveAt ( variants , path , printResult ) ;
98- } else {
99- console . log ( 'Download failed: unable to find font family "%s". \n' . bold . red , searchedTerm ) ;
100- searchFont ( searchedTerm ) ;
101- }
102- } )
108+ /**
109+ * Output Helpers
110+ */
111+
112+ function handleMatchError ( action , term , err ) {
113+ if ( err ) {
114+ console . error ( pc . red ( err . toString ( ) ) ) ;
115+ } else {
116+ console . log (
117+ pc . bold ( pc . red ( `${ action } failed: unable to find font family "${ term } "` ) )
118+ ) ;
119+ fontList . searchFontByName ( term , printFontList ) ;
120+ }
103121}
104122
105- function installFont ( searchedTerm , variants ) {
106- fontList . getFontByName ( searchedTerm , function ( err , filteredList ) {
107- if ( err ) {
108- printError ( err ) ;
109- return ;
110- }
111- if ( filteredList . data . length === 1 ) {
112- filteredList . getFirst ( ) . install ( variants , printResult ) ;
113- } else {
114- console . log ( 'Installation failed: unable to find font family "%s". \n' . bold . red , searchedTerm ) ;
115- searchFont ( searchedTerm ) ;
116- }
117- } )
118- }
123+ function printFontList ( err , list , message = "Search results for:" ) {
124+ if ( err ) return console . error ( pc . red ( err . toString ( ) ) ) ;
125+ if ( list . data . length === 0 ) {
126+ return console . log ( pc . red ( `No results found for: ${ list . _filterTerm } ` ) ) ;
127+ }
119128
120- function copyFont ( searchedTerm , variants ) {
121- fontList . getFontByName ( searchedTerm , function ( err , filteredList ) {
122- if ( err ) {
123- printError ( err ) ;
124- return ;
125- }
126- if ( filteredList . data . length === 1 ) {
127-
128- var list = filteredList . getFirst ( ) ;
129- var cssUrl = variants ? list . cssUrl + ':' + variants . join ( ',' ) : list . cssUrl ;
130-
131- ncp . copy ( cssUrl , function ( ) {
132- console . log ( '"%s" font url has been copied to your clipboard.' . green , searchedTerm ) ;
133- } ) ;
134-
135- } else {
136- console . log ( 'Copy failed: unable to find font family "%s". \n' . bold . red , searchedTerm ) ;
137- searchFont ( searchedTerm ) ;
138- }
139- } )
140- }
129+ console . log ( pc . green ( `${ message } "${ pc . bold ( pc . blue ( list . _filterTerm ) ) } "\n` ) ) ;
141130
142- function printFontList ( err , list , message ) {
143- if ( err ) {
144- printError ( err )
145- return ;
146- } else if ( list . data . length === 0 ) {
147- console . log ( 'No results found for: %s\n' . red , list . _filterTerm ) ;
148- } else {
149- message = message || 'Search results for:'
150- if ( list . _filterTerm )
151- console . log ( '%s "%s"\n' . green , message , ( list . _filterTerm || '' ) . bold . blue ) ;
152- list . data . forEach ( function ( el ) {
153- console . log ( " * %s" . bold . blue , el . family ) ;
154- console . log ( " Category: %s\n Variants: %s\n CSS Url: %s\n" , el . getCategory ( ) , el . getVariants ( ) . join ( ", " ) , el . getCssUrl ( ) ) ;
155- } )
156- }
131+ list . data . forEach ( ( el ) => {
132+ console . log ( pc . bold ( pc . blue ( ` * ${ el . family } ` ) ) ) ;
133+ console . log ( ` Category: ${ el . getCategory ( ) } ` ) ;
134+ console . log ( ` Variants: ${ el . getVariants ( ) . join ( ", " ) } ` ) ;
135+ console . log ( ` CSS Url: ${ el . getCssUrl ( ) } \n` ) ;
136+ } ) ;
157137}
158138
159139function printResult ( err , result ) {
160- if ( err ) {
161- printError ( err ) ;
162- return ;
163- }
164- console . log ( '' ) ;
165- result . forEach ( function ( el ) {
166- console . log ( '%s variant %s downloaded in %s' . green , ( el . family || '??' ) . bold , ( el . variant || '??' ) . bold , ( el . path || '??' ) . underline ) ;
167- } )
168- console . log ( '' ) ;
169- }
170-
171- function printError ( err ) {
172- console . error ( 'Error, please try again!' . bold . red ) ;
173- console . error ( err . toString ( ) . bold . red ) ;
174- process . exit ( 1 ) ;
175- }
140+ if ( err ) return console . error ( pc . red ( err . toString ( ) ) ) ;
141+ console . log ( "" ) ;
142+ result . forEach ( ( el ) => {
143+ console . log (
144+ pc . green (
145+ `${ pc . bold ( el . family ) } variant ${ pc . bold ( el . variant ) } processed: ${ pc . underline ( el . path ) } `
146+ )
147+ ) ;
148+ } ) ;
149+ console . log ( "" ) ;
150+ }
0 commit comments