Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hooks download:start, download:complete #54

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 39 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,73 +25,85 @@ pnpm add google-fonts-helper
Import into your Node.js project:

```js
const { constructURL, merge, isValidURL, parse, download } = require('google-fonts-helper')
const {
constructURL,
merge,
isValidURL,
parse,
download,
} = require("google-fonts-helper");
// or
import { constructURL, merge, isValidURL, parse, download } from 'google-fonts-helper'
import {
constructURL,
merge,
isValidURL,
parse,
download,
} from "google-fonts-helper";
```

## Usage

### `constructURL(): string`

```ts
constructURL({ families: { Roboto: true } })
constructURL({ families: { Roboto: true } });
// https://fonts.googleapis.com/css2?family=Roboto

constructURL({ families: { Roboto: true, Lato: true } })
constructURL({ families: { Roboto: true, Lato: true } });
// https://fonts.googleapis.com/css2?family=Roboto&family=Lato

constructURL({ families: { 'Crimson Pro': { wght: '200..400' } } })
constructURL({ families: { "Crimson Pro": { wght: "200..400" } } });
// https://fonts.googleapis.com/css2?family=Crimson+Pro:[email protected]

constructURL({
families: {
Roboto: true,
Lato: {
wght: 100
wght: 100,
},
Raleway: {
wght: [400],
ital: [100, 400]
}
}
})
ital: [100, 400],
},
},
});
// https://fonts.googleapis.com/css2?family=Roboto&family=Lato:wght@100&family=Raleway:ital,wght@0,400;1,100;1,400
```

### `merge(...fonts: GoogleFonts[]): GoogleFonts`

```ts
merge({ families: { Roboto: true } }, { families: { Lato: true } })
merge({ families: { Roboto: true } }, { families: { Lato: true } });
// { families: { Roboto: true, Lato: true } }

merge({ families: { Roboto: true } }, { families: { Roboto: [300, 400] } })
merge({ families: { Roboto: true } }, { families: { Roboto: [300, 400] } });
// { families: { Roboto: [300, 400] } }
```

### `isValidURL(url: string): boolean`

```ts
isValidURL('https://fonts.googleapis.com/css2?family=Roboto')
isValidURL("https://fonts.googleapis.com/css2?family=Roboto");
// true

isValidURL('https://foo.bar')
isValidURL("https://foo.bar");
// false
```

### `parse(url: string): GoogleFonts`

```ts
parse('https://fonts.googleapis.com/css2?family=Roboto')
parse("https://fonts.googleapis.com/css2?family=Roboto");
// { families: { Roboto: true } }

parse('https://fonts.googleapis.com/css2?family=Roboto&family=Lato')
parse("https://fonts.googleapis.com/css2?family=Roboto&family=Lato");
// { families: { Roboto: true, Lato: true } }

parse('https://fonts.googleapis.com/css2?family=Crimson+Pro:[email protected]')
parse("https://fonts.googleapis.com/css2?family=Crimson+Pro:[email protected]");
// { families: { 'Crimson Pro': { wght: '200..400' } }

parse('https://foo.bar')
parse("https://foo.bar");
// {}
```

Expand All @@ -115,6 +127,14 @@ downloader.hook('download-font:done', (font: FontInputOutput) {
console.log(font)
})

downloader.hook('download:start', () => {
console.log('Downloading fonts...')
})

downloader.hook('download:complete', () => {
console.log('Download fonts completed.')
})

await downloader.execute()
```

Expand All @@ -125,17 +145,14 @@ await downloader.execute()
Copyright (c) Datalogix

<!-- Badges -->

[npm-version-src]: https://img.shields.io/npm/v/google-fonts-helper/latest.svg
[npm-version-href]: https://npmjs.com/package/google-fonts-helper

[npm-downloads-src]: https://img.shields.io/npm/dt/google-fonts-helper.svg
[npm-downloads-href]: https://npmjs.com/package/google-fonts-helper

[github-actions-ci-src]: https://github.com/datalogix/google-fonts-helper/workflows/ci/badge.svg
[github-actions-ci-href]: https://github.com/datalogix/google-fonts-helper/actions?query=workflow%3Aci

[codecov-src]: https://img.shields.io/codecov/c/github/datalogix/google-fonts-helper.svg
[codecov-href]: https://codecov.io/gh/datalogix/google-fonts-helper

[license-src]: https://img.shields.io/npm/l/google-fonts-helper.svg
[license-href]: https://npmjs.com/package/google-fonts-helper
12 changes: 10 additions & 2 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface DownloadOptions {
}

export interface DownloaderHooks {
'download:start': () => void
'download:complete': () => void
'download-css:before': (url: string) => void
'download-css:done': (url: string, content: string, fonts: FontInputOutput[]) => void
'download-font:before': (font: FontInputOutput) => void
Expand Down Expand Up @@ -55,7 +57,7 @@ export class Downloader extends Hookable<DownloaderHooks> {
}
}

async execute (): Promise<void> {
async execute (): Promise<boolean> {
if (!isValidURL(this.url)) {
throw new Error('Invalid Google Fonts URL')
}
Expand All @@ -69,7 +71,7 @@ export class Downloader extends Hookable<DownloaderHooks> {
const currentUrl = (currentCssContent.split(/\r?\n/, 1).shift() || '').replace('/*', '').replace('*/', '').trim()

if (currentUrl === this.url) {
return
return false
}

overwriting = true
Expand All @@ -79,6 +81,8 @@ export class Downloader extends Hookable<DownloaderHooks> {
rmSync(outputDir, { recursive: true, force: true })
}

await this.callHook('download:start')

// download css content
await this.callHook('download-css:before', this.url)
const cssContent = await ofetch(this.url, { headers })
Expand All @@ -93,6 +97,10 @@ export class Downloader extends Hookable<DownloaderHooks> {
await this.callHook('write-css:before', cssPath, cssContent, fonts)
const newContent = this.writeCss(cssPath, `/* ${this.url} */\n${cssContent}`, fonts)
await this.callHook('write-css:done', cssPath, newContent, cssContent)

await this.callHook('download:complete')

return true
}

private downloadFonts (fonts: FontInputOutput[]) {
Expand Down