google-font-installer
Advanced tools
Comparing version 1.0.1 to 1.0.2
65
cli.js
@@ -8,4 +8,4 @@ #!/usr/bin/env node | ||
var GoogleFontList = require('./lib/google-font-list'); | ||
var fontList = new GoogleFontList('AIzaSyB1I5eF2kRqqs50DS8qBJtFkCTMMoQLusg'); | ||
var pjson = require('./package.json'); | ||
@@ -18,4 +18,5 @@ program | ||
fontList.on('success', function(){ | ||
this.searchFontByName(searchedTerm, printFontList) | ||
searchFont(searchedTerm); | ||
}) | ||
}) | ||
@@ -33,9 +34,3 @@ | ||
fontList.on('success', function(){ | ||
this.searchFontByName(searchedTerm, function(err, filteredList){ | ||
if (filteredList && filteredList.data.length === 1) { | ||
filteredList.getFirst().saveAt(variants, path, printResult); | ||
} else { | ||
printFontList(err, filteredList, 'More fonts mathces the request:'); | ||
} | ||
}) | ||
downloadFont(searchedTerm, variants, path); | ||
}) | ||
@@ -52,9 +47,3 @@ }); | ||
fontList.on('success', function(){ | ||
this.searchFontByName(searchedTerm, function(err, filteredList){ | ||
if (filteredList && filteredList.data.length === 1) { | ||
filteredList.getFirst().install(variants, printResult); | ||
} else { | ||
printFontList(err, filteredList, 'More fonts mathces the request:'); | ||
} | ||
}) | ||
installFont(searchedTerm, variants); | ||
}) | ||
@@ -64,3 +53,3 @@ }); | ||
program | ||
.version('1.0.0'); | ||
.version(pjson.version); | ||
@@ -77,5 +66,39 @@ program.parse(process.argv); | ||
if (!found) | ||
program.help(); | ||
program.help(); | ||
// HELPERS FUNCTIONS | ||
console.log('\nDonwloading Google Font List....\n'.blue.bold); | ||
fontList.on('error', function(err){ | ||
printError(err); | ||
}) | ||
function searchFont(searchedTerm) { | ||
fontList.searchFontByName(searchedTerm, printFontList) | ||
} | ||
function downloadFont(searchedTerm, variants, path) { | ||
fontList.getFontByName(searchedTerm, function(err, filteredList){ | ||
if (filteredList.data.length === 1) { | ||
filteredList.getFirst().saveAt(variants, path, printResult); | ||
} else { | ||
console.log('Impossible to download the font: no matches for "%s". \n'.bold.red, searchedTerm); | ||
searchFont(searchedTerm); | ||
} | ||
}) | ||
} | ||
function installFont(searchedTerm, variants) { | ||
fontList.getFontByName(searchedTerm, function(err, filteredList){ | ||
if (filteredList.data.length === 1) { | ||
filteredList.getFirst().install(variants, printResult); | ||
} else { | ||
console.log('Impossible to install the font: no matches for "%s". \n'.bold.red, searchedTerm); | ||
searchFont(searchedTerm); | ||
} | ||
}) | ||
} | ||
function printFontList(err, list, message){ | ||
@@ -86,10 +109,10 @@ if (err) { | ||
} else if (list.data.length === 0) { | ||
console.log('\nNo results found for: %s\n'.red, list._filterTerm); | ||
console.log('No results found for: %s\n'.red, list._filterTerm); | ||
} else { | ||
message = message || 'Search reuslts for:' | ||
if (list._filterTerm) | ||
console.log('\n%s "%s"\n'.green, message, list._filterTerm.bold.blue); | ||
console.log('%s "%s"\n'.green, message, list._filterTerm.bold.blue); | ||
list.data.forEach(function(el){ | ||
console.log(" * %s".bold.blue, el.family); | ||
console.log(" Category: %s\n Variants: %s\n CSS Url: %s", el.getCategory(), el.getVariants().join(", "), el.getCssUrl()); | ||
console.log(" Category: %s\n Variants: %s\n CSS Url: %s\n", el.getCategory(), el.getVariants().join(", "), el.getCssUrl()); | ||
}) | ||
@@ -96,0 +119,0 @@ } |
@@ -127,2 +127,28 @@ 'use strict' | ||
GoogleFontList.prototype.getFont = function(term, field, callback) { | ||
var self = this; | ||
var searchTerms = term.trim().toLowerCase(); | ||
async.filter( | ||
self.data, | ||
function(el, cb) { | ||
cb( el[field].toLowerCase() === searchTerms ); | ||
}, | ||
function(result) { | ||
var fontList = self.clone(); | ||
fontList.data = result; | ||
fontList._filterField = field; | ||
fontList._filterTerm = term; | ||
callback(null, fontList); | ||
} | ||
) | ||
} | ||
GoogleFontList.prototype.getFontByName = function(term, callback) { | ||
this.getFont(term, 'family', callback); | ||
} | ||
GoogleFontList.prototype.getFontByType = function(term, callback) { | ||
this.getFont(term, 'category', callback); | ||
} | ||
GoogleFontList.prototype.getFirst = function () { | ||
@@ -136,10 +162,10 @@ return this.data.length > 0 ? this.data[0] : false; | ||
GoogleFontList.prototype.forEachFont = function(fn) { | ||
GoogleFontList.prototype.forEachFont = function(fn, callback) { | ||
var self = this; | ||
async.each(this.data, function(el, cb){ | ||
fn.call(self, el); | ||
async.forEachOf(this.data, function(el, index, cb){ | ||
fn.call(self, el, index); | ||
cb(); | ||
}) | ||
}, callback); | ||
} | ||
module.exports = GoogleFontList; |
@@ -37,5 +37,12 @@ 'use strict' | ||
req.setTimeout(5000, function(){ | ||
// req.abort(); | ||
self.emit('error', new Error('Request timeout.')); | ||
}) | ||
req.on('error', function(e){ | ||
self.emit('error', e); | ||
var errorMessage = util.format('Connection to %s failed.', parsedUri['hostname']) | ||
self.emit('error', new Error(errorMessage)); | ||
}) | ||
} else { | ||
@@ -42,0 +49,0 @@ setImmediate(function(){ |
{ | ||
"name": "google-font-installer", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Download and install Google Web Fonts on your local machine", | ||
@@ -5,0 +5,0 @@ "author": "Lorenzo Zottar <lordgiotto@gmail.com>", |
Google Font Installer | ||
============= | ||
Google Font Installer is written in NodeJS and let you Search, Donwload and Install any font offered by Google Web Fonts. | ||
Google Font Installer is a NodeJS module/CLI that lets you Search, Download and Install fonts offered by Google Web Fonts. | ||
@@ -12,4 +12,19 @@ You can use it in two ways: | ||
In Linux and OSX, the font will be installed in the user's font directory (~/.fonts for Linux, ~/Library/Fonts for OSX). | ||
In Windows, due to the fact that font installation require some register modifications, I prefered to create a little WScript (a windows script that use ActiveX windows interface) and spawn a `cscript` process to install the font in _'windows native way'_. | ||
In Windows, due to the fact that font installation require some register modifications, I prefered to create a little WScript (a windows script that use ActiveX windows interface) and spawn a `cscript` process to install the font in a _'windows native way'_. | ||
- [CLI](#cli) | ||
- [Search a font](#search-a-font) | ||
- [Download a font](#download-a-font) | ||
- [Install a font](#install-a-font) | ||
- [Exemples](#cli-exemples) | ||
- [APIs](#apis) | ||
- [GoogleFontList](#googlefontlist) | ||
- [Events](#google-font-list-events) | ||
- [Public Properties](#google-font-list-properties) | ||
- [Public Methods](#google-font-list-methods) | ||
- [GoogleFont](#googlefont) | ||
- [Public Properties](#google-font-properties) | ||
- [Public Methods](#google-font-methods) | ||
- [Exemples](#api-exemples) | ||
# CLI | ||
@@ -56,2 +71,3 @@ | ||
<a id="cli-exemples"></a> | ||
### Exemples | ||
@@ -79,3 +95,3 @@ Search the _source_ keyword: | ||
``` | ||
$ gfi download source sans -v 600,700italic | ||
$ gfi download source sans pro -v 600,700italic | ||
@@ -93,2 +109,3 @@ Source Sans Pro variant 600 downloaded in /home/user/someFolder/SourceSansPro-600.ttf | ||
# APIs | ||
First of all you have to install the module in you NodeJS project: | ||
@@ -117,2 +134,3 @@ ``` | ||
``` | ||
<a id="google-font-list-events"></a> | ||
@@ -125,2 +143,3 @@ #### Events | ||
<a id="google-font-list-properties"></a> | ||
#### Public properties | ||
@@ -132,2 +151,3 @@ ###### `data` [Array] | ||
<a id="google-font-list-methods"></a> | ||
#### Public methods | ||
@@ -164,3 +184,3 @@ | ||
Function with callback used to search a specific string inside a specific property of googleFont object. | ||
Function with callback used to search a font inside the object `data` property: it's case insensitive, words order insensitive and test if the field CONTAIN that words (ex. `source sans`, `source Sans` and `sans source` will produce the same result). | ||
@@ -179,2 +199,21 @@ ###### `searchFontByName(term, callback)` | ||
###### `getFont(term, field, callback)` | ||
- term [String] The string to search for. | ||
- field [String] The property of the GoogleFont instance to test. | ||
- callback(err, fontList) [Function] Mandatory callback with optional error obj and a new instance of GoogleFontList with the searched subset of Fonts | ||
Function with callback used to get a specific font where a field and the term exactly match (case insensitive) | ||
###### `getFontByName(term, callback)` | ||
- term [String] The string to search for. | ||
- callback(err, fontList) [Function] Mandatory callback with optional error obj and a new instance of GoogleFontList with the searched subset of Fonts | ||
Same as getFont, but specific for the Family property. (the font name) | ||
###### `getFontByType(term, callback)` | ||
- term [String] The string to search for. | ||
- callback(err, fontList) [Function] Mandatory callback with optional error obj and a new instance of GoogleFontList with the searched subset of Fonts | ||
Same as getFont, but specific for the Category property. (for instance serif, sans-serif, display, etc) | ||
###### `getFirst()` | ||
@@ -187,4 +226,5 @@ - Returns [GoogleFont|Boolean] If not empty, return the first GoogleFont instance inside | ||
###### `forEachFont(fn)` | ||
- fn [Function] The function to execute. | ||
###### `forEachFont(fn, callback)` | ||
- fn(el, index) [Function] The function to execute for each element of the list, with element and index as parameters. | ||
- callback(err) [Function] Optional callback that will be executed after the end of the loop | ||
@@ -199,4 +239,5 @@ Execture fn foreach GoogleFOnt in GoogleFontList `data` property. It's async. | ||
**Every object inside the `data` property of GoogleFontList is an instance of this class.** | ||
**Every object inside the `data` property of [GoogleFontList](#googlefontlist) is an instance of this class.** | ||
<a id="google-font-properties"></a> | ||
#### Public properties | ||
@@ -215,2 +256,3 @@ | ||
<a id="google-font-methods"></a> | ||
#### Public methods | ||
@@ -264,2 +306,3 @@ | ||
<a id="api-exemples"></a> | ||
### Exemples | ||
@@ -278,3 +321,3 @@ ```js | ||
throw err; | ||
result.forEach(function(el){ | ||
result.forEach(function(el, index){ | ||
console.log('Variant %s of %s downloaded in %s', el.variant, el.family, el.path); | ||
@@ -281,0 +324,0 @@ }) |
35142
630
319