From e851ee99fdedde5fb6d2534cf7b3ce42717d282f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ionic=C4=83=20Biz=C4=83u?= Date: Tue, 29 May 2018 16:21:16 +0200 Subject: [PATCH] :arrow_up: 1.0.0 :tada: --- .gitignore | 8 ++ CONTRIBUTING.md | 66 ++++++++++++++++ DOCUMENTATION.md | 15 ++++ LICENSE | 21 +++++ README.md | 141 ++++++++++++++++++++++++++++++++++ bin/google-font-downloader.js | 72 +++++++++++++++++ package.json | 34 ++++++++ 7 files changed, 357 insertions(+) create mode 100644 .gitignore create mode 100644 CONTRIBUTING.md create mode 100644 DOCUMENTATION.md create mode 100644 LICENSE create mode 100644 README.md create mode 100755 bin/google-font-downloader.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..af96bda --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.swp +*.swo +*~ +*.log +node_modules +*.env +.DS_Store +package-lock.json diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4ae15cb --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,66 @@ +# :eight_spoked_asterisk: :stars: :sparkles: :dizzy: :star2: :star2: :sparkles: :dizzy: :star2: :star2: Contributing :star: :star2: :dizzy: :sparkles: :star: :star2: :dizzy: :sparkles: :stars: :eight_spoked_asterisk: + +So, you want to contribute to this project! That's awesome. However, before +doing so, please read the following simple steps how to contribute. This will +make the life easier and will avoid wasting time on things which are not +requested. :sparkles: + +## Discuss the changes before doing them + - First of all, open an issue in the repository, using the [bug tracker][1], + describing the contribution you would like to make, the bug you found or any + other ideas you have. This will help us to get you started on the right + foot. + + - If it makes sense, add the platform and software information (e.g. operating + system, Node.JS version etc.), screenshots (so we can see what you are + seeing). + + - It is recommended to wait for feedback before continuing to next steps. + However, if the issue is clear (e.g. a typo) and the fix is simple, you can + continue and fix it. + +## Fixing issues + - Fork the project in your account and create a branch with your fix: + `some-great-feature` or `some-issue-fix`. + + - Commit your changes in that branch, writing the code following the + [code style][2]. If the project contains tests (generally, the `test` + directory), you are encouraged to add a test as well. :memo: + + - If the project contains a `package.json` or a `bower.json` file add yourself + in the `contributors` array (or `authors` in the case of `bower.json`; + if the array does not exist, create it): + + ```json + { + "contributors": [ + "Your Name (http://your.website)" + ] + } + ``` + +## Creating a pull request + + - Open a pull request, and reference the initial issue in the pull request + message (e.g. *fixes #*). Write a good description and + title, so everybody will know what is fixed/improved. + + - If it makes sense, add screenshots, gifs etc., so it is easier to see what + is going on. + +## Wait for feedback +Before accepting your contributions, we will review them. You may get feedback +about what should be fixed in your modified code. If so, just keep committing +in your branch and the pull request will be updated automatically. + +## Everyone is happy! +Finally, your contributions will be merged, and everyone will be happy! :smile: +Contributions are more than welcome! + +Thanks! :sweat_smile: + + + +[1]: https://github.com/IonicaBizau/google-font-downloader/issues + +[2]: https://github.com/IonicaBizau/code-style diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md new file mode 100644 index 0000000..3462e50 --- /dev/null +++ b/DOCUMENTATION.md @@ -0,0 +1,15 @@ +## Documentation + +You can see below the API reference of this module. + +### `googleFontDownloader(a, b)` +Download Google fonts by providing the url + +#### Params + +- **Number** `a`: Param descrpition. +- **Number** `b`: Param descrpition. + +#### Return +- **Number** Return description. + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..772ac15 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2018 Ionică Bizău (https://ionicabizau.net) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..28337e2 --- /dev/null +++ b/README.md @@ -0,0 +1,141 @@ + + + +# `$ google-font-downloader` + + [![Support me on Patreon][badge_patreon]][patreon] [![Buy me a book][badge_amazon]][amazon] [![PayPal][badge_paypal_donate]][paypal-donations] [![Ask me anything](https://img.shields.io/badge/ask%20me-anything-1abc9c.svg)](https://github.com/IonicaBizau/ama) [![Version](https://img.shields.io/npm/v/google-font-downloader.svg)](https://www.npmjs.com/package/google-font-downloader) [![Downloads](https://img.shields.io/npm/dt/google-font-downloader.svg)](https://www.npmjs.com/package/google-font-downloader) + +> Download Google fonts by providing the url + +#### Usage + + +You can use this tool to download Google Fonts for offline use, just by providing the Google APIs url. + + +:bulb: **Note**: It's not clear yet if Google Fonts are EU GDPR compliant (see [this issue](https://github.com/google/fonts/issues/1495)). This may be a good reason to download the Google Fonts you use on your server. + +#### How it works + + +You need to provide the url to the Google APIs endpoint (e.g. `https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i`) and you will get the following files/directories in the current working directory: + + + - A file named `google-fonts-.css—this will contain the CSS snippets that you need to copy in your app. You may need to update the paths to the font files. + - A directory structure looking like this: `fonts///` + + +## :cloud: Installation + +You can install the package globally and use it as command line tool: + + +```sh +# Using npm +npm install --global google-font-downloader + +# Using yarn +yarn global add google-font-downloader +``` + + +Then, run `google-font-downloader --help` and see what the CLI tool can do. + + +``` +$ google-font-downloader --help +Usage: google-font-downloader [options] + +Download Google fonts by providing the url + +Command arguments: + The Google APIs url. + +Options: + -v, --version Displays version information. + -h, --help Displays this help. + +Examples: + $ google-font-downloader https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i + +Documentation can be found at https://github.com/IonicaBizau/google-font-downloader#readme. +``` + +## :clipboard: Example + + +Here is an example how to use this package as library. To install it locally, as library, you can do that using `npm` (or `yarn`): + +```sh +# Using npm +npm install --save google-font-downloader + +# Using yarn +yarn add google-font-downloader +``` + + + +```js +const googleFontDownloader = require("google-font-downloader"); + +console.log(googleFontDownloader()); +``` + + + +## :question: Get Help + +There are few ways to get help: + + 1. Please [post questions on Stack Overflow](https://stackoverflow.com/questions/ask). You can open issues with questions, as long you add a link to your Stack Overflow question. + 2. For bug reports and feature requests, open issues. :bug: + + 3. For direct and quick help, you can [use Codementor](https://www.codementor.io/johnnyb). :rocket: + + + +## :memo: Documentation + +For full API reference, see the [DOCUMENTATION.md][docs] file. + +## :yum: How to contribute +Have an idea? Found a bug? See [how to contribute][contributing]. + + +## :sparkling_heart: Support my projects + +I open-source almost everything I can, and I try to reply everyone needing help using these projects. Obviously, +this takes time. You can integrate and use these projects in your applications *for free*! You can even change the source code and redistribute (even resell it). + +However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it: + + - Starring and sharing the projects you like :rocket: + - [![Buy me a book][badge_amazon]][amazon]—I love books! I will remember you after years if you buy me one. :grin: :book: + - [![PayPal][badge_paypal]][paypal-donations]—You can make one-time donations via PayPal. I'll probably buy a ~~coffee~~ tea. :tea: + - [![Support me on Patreon][badge_patreon]][patreon]—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone). + - **Bitcoin**—You can send me bitcoins at this address (or scanning the code below): `1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6` + + ![](https://i.imgur.com/z6OQI95.png) + +Thanks! :heart: + + + +## :scroll: License + +[MIT][license] © [Ionică Bizău][website] + +[badge_patreon]: http://ionicabizau.github.io/badges/patreon.svg +[badge_amazon]: http://ionicabizau.github.io/badges/amazon.svg +[badge_paypal]: http://ionicabizau.github.io/badges/paypal.svg +[badge_paypal_donate]: http://ionicabizau.github.io/badges/paypal_donate.svg +[patreon]: https://www.patreon.com/ionicabizau +[amazon]: http://amzn.eu/hRo9sIZ +[paypal-donations]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=RVXDDLKKLQRJW +[donate-now]: http://i.imgur.com/6cMbHOC.png + +[license]: http://showalicense.com/?fullname=Ionic%C4%83%20Biz%C4%83u%20%3Cbizauionica%40gmail.com%3E%20(https%3A%2F%2Fionicabizau.net)&year=2018#license-mit +[website]: https://ionicabizau.net +[contributing]: /CONTRIBUTING.md +[docs]: /DOCUMENTATION.md diff --git a/bin/google-font-downloader.js b/bin/google-font-downloader.js new file mode 100755 index 0000000..5b9d284 --- /dev/null +++ b/bin/google-font-downloader.js @@ -0,0 +1,72 @@ +#!/usr/bin/env node + +"use strict"; + +const Tilda = require("tilda") + , WritableStream = require("streamp").writable + , tinyreq = require("tinyreq") + , matchAll = require("match-all") + ; + +const USER_AGENT = "User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" + +new Tilda(`${__dirname}/../package.json`, { + args: [ + { + name: "url" + , desc: "The Google APIs url." + , required: true + } + ], + examples: [ + "google-font-downloader https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" + ] +}).main(action => { + const url = action.args.url + const data = {} + console.log(`Getting the external CSS: ${url}`) + tinyreq({ + url, + headers: { + "user-agent": USER_AGENT + } + }).then(body => { + const matchFontFilesRegex = /url\((https\:\/\/fonts\.gstatic\.com\/.*)\) format/gm + + data.original_stylesheet = body + data.local_stylesheet = body + data.font_urls = matchAll(body, matchFontFilesRegex).toArray() + data.local_font_paths = data.font_urls.map(c => `fonts/${c.split("/").slice(4).join("/")}`) + data.fonts = data.font_urls.map((c, index) => ({ + remote: c, + local: data.local_font_paths[index] + })) + + console.log(`Detected ${data.fonts.length} font files to download.`) + return Promise.all(data.fonts.map(c => { + data.local_stylesheet = data.local_stylesheet.replace(c.remote, c.local) + return new Promise(res => { + const req = tinyreq({ url: c.remote, encoding: null, headers: { "user-agent": USER_AGENT } }) + , stream = new WritableStream(c.local) + + req.on("data", data => { + stream.write(data) + }).on("error", e => { + console.error("Failed to download " + c.remote) + console.error(e) + res() + }).on("end", () => { + console.log(`Downloaded ${c.remote} in ${c.local}`) + stream.end() + res() + }) + }) + })) + }).then(() => { + const fileName = `google-fonts-${Date.now()}.css` + , cssStream = new WritableStream(fileName) + + console.log(`Writting the CSS into ${fileName}`) + cssStream.end(data.local_stylesheet) + }) +}); diff --git a/package.json b/package.json index 51bad0c..f5ff867 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,7 @@ { + "bin": { + "google-font-downloader": "bin/google-font-downloader.js" + }, "name": "google-font-downloader", "description": "Download Google fonts by providing the url", "keywords": [ @@ -40,5 +43,36 @@ }, "bugs": { "url": "https://github.com/IonicaBizau/google-font-downloader/issues" + }, + "dependencies": { + "match-all": "^1.2.4", + "streamp": "^2.2.8", + "tilda": "^4.4.13", + "tinyreq": "^3.4.0" + }, + "blah": { + "description": [ + { + "h4": "Usage" + }, + { + "p": "You can use this tool to download Google Fonts for offline use, just by providing the Google APIs url." + }, + { + "p": ":bulb: **Note**: It's not clear yet if Google Fonts are EU GDPR compliant (see [this issue](https://github.com/google/fonts/issues/1495)). This may be a good reason to download the Google Fonts you use on your server." + }, + { + "h4": "How it works" + }, + { + "p": "You need to provide the url to the Google APIs endpoint (e.g. `https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i`) and you will get the following files/directories in the current working directory:" + }, + { + "ul": [ + "A file named `google-fonts-.css—this will contain the CSS snippets that you need to copy in your app. You may need to update the paths to the font files.", + "A directory structure looking like this: `fonts///`" + ] + } + ] } } \ No newline at end of file