-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prefabs): google webfonts support (#127)
- Loading branch information
Showing
44 changed files
with
4,505 additions
and
433 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash -f | ||
|
||
# Clean generated files from packages. | ||
for package in compiler/targets framework/stdlib; do | ||
TARGET=src/$package | ||
pushd $TARGET | ||
yarn regenerate-goldens | ||
pushd | ||
echo "regenerated goldens for $TARGET" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/compiler/targets/test/goldens/Primitives/diez-target-test-stub-web/static/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
|
||
:root { | ||
--primitives-number: 10; | ||
--primitives-number-px: 10px; | ||
|
1 change: 1 addition & 0 deletions
1
src/compiler/targets/test/goldens/Primitives/diez-target-test-stub-web/static/styles.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
|
||
|
||
$primitives-number: 10; | ||
$primitives-number-px: 10px; | ||
$primitives-number-pt: 10pt; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"providers": { | ||
"commands": [ | ||
"./scripts/lib/commands/generate-google-fonts" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/framework/prefabs/scripts/commands/generate-google-fonts.action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import {CliAction} from '@diez/cli-core'; | ||
import {writeFile} from 'fs-extra'; | ||
import {join} from 'path'; | ||
import phin from 'phin'; | ||
import {FontCollectionGenerator, GoogleFontParser} from '../utils'; | ||
|
||
interface GoogleFontsFamily { | ||
family: string; | ||
variants: string[]; | ||
} | ||
|
||
interface GoogleFontsCollection { | ||
items: GoogleFontsFamily[]; | ||
} | ||
|
||
interface GenerateGoogleFontsParams { | ||
apiKey: string; | ||
} | ||
|
||
type RequestLib = (options: {url: string, parse?: 'json'}) => Promise<{body: GoogleFontsCollection}>; | ||
|
||
/** | ||
* Fetch all fonts available in Google Fonts via their Developer API. | ||
*/ | ||
export const fetchGoogleFontsFromApi = async (apiKey: string, requestLib = phin as RequestLib) => { | ||
const response = await requestLib({ | ||
url: `https://www.googleapis.com/webfonts/v1/webfonts?key=${apiKey}&sort=popularity`, | ||
parse: 'json', | ||
}); | ||
|
||
return response.body.items; | ||
}; | ||
|
||
const generateGoogleFontsAction: CliAction = async ({apiKey}: GenerateGoogleFontsParams) => { | ||
const availableFonts = await fetchGoogleFontsFromApi(apiKey); | ||
const parser = new GoogleFontParser(); | ||
|
||
for (const {family, variants} of availableFonts) { | ||
for (const variant of variants) { | ||
parser.parse(family, variant); | ||
} | ||
} | ||
|
||
await writeFile( | ||
join('src', 'resources', 'web-google-fonts.ts'), | ||
FontCollectionGenerator.generateTypeScriptEnum(parser), | ||
); | ||
}; | ||
|
||
export default generateGoogleFontsAction; |
21 changes: 21 additions & 0 deletions
21
src/framework/prefabs/scripts/commands/generate-google-fonts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {CliCommandProvider} from '@diez/cli-core'; | ||
|
||
const provider: CliCommandProvider = { | ||
name: 'generate-google-fonts', | ||
description: 'Generates a collection of Font prefab instances with all available Google Fonts.', | ||
loadAction: () => import('./generate-google-fonts.action'), | ||
options: [ | ||
{ | ||
description: 'Your Google Developer API key (required)', | ||
longName: 'apiKey', | ||
valueName: 'apiKey', | ||
validator: async (options) => { | ||
if (!options.apiKey) { | ||
throw new Error('apiKey is required.'); | ||
} | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
export = provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../../tsconfig.shared.json", | ||
"compilerOptions": { | ||
"rootDir": ".", | ||
"outDir": "lib" | ||
}, | ||
"include": [ | ||
"**/**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {pascalCase} from 'change-case'; | ||
|
||
const FontWeightName: Record<number, string> = { | ||
100: 'Thin', | ||
200: 'ExtraLight', | ||
300: 'Light', | ||
400: 'Regular', | ||
500: 'Medium', | ||
600: 'SemiBold', | ||
700: 'Bold', | ||
800: 'ExtraBold', | ||
900: 'Black', | ||
}; | ||
|
||
const DiezFontStyle: Record<string, string> = { | ||
regular: 'FontStyle.Normal', | ||
italic: 'FontStyle.Italic', | ||
bold: 'FontStyle.Bold', | ||
}; | ||
|
||
/** | ||
* Abstraction to parse font data from different sources into an internal | ||
* standard format. | ||
* | ||
* @ignore | ||
*/ | ||
abstract class FontCollectionParser { | ||
readonly abstract name: string; | ||
readonly abstract instanceConstructor: string; | ||
collection = new Map<string, string>(); | ||
|
||
protected abstract parseVariation (variant: string): {style: string, weight: number}; | ||
|
||
protected addToCollection (family: string, style: string, weight: number) { | ||
this.collection.set( | ||
pascalCase(`${family}-${FontWeightName[weight]}${weight}-${style === 'regular' ? '' : style}`), | ||
`${this.instanceConstructor}('${family}', {weight: ${weight}, style: ${DiezFontStyle[style]}})`, | ||
); | ||
} | ||
|
||
parse (family: string, variant: string) { | ||
const {style, weight} = this.parseVariation(variant); | ||
this.addToCollection(family, style, weight); | ||
} | ||
} | ||
|
||
/** | ||
* Parses responses from the Google Fonts Developer API into a format that can | ||
* be easily consumed. | ||
* | ||
* @ignore | ||
*/ | ||
export class GoogleFontParser extends FontCollectionParser { | ||
readonly name = 'GoogleWebFonts'; | ||
readonly instanceConstructor = 'Font.googleWebFont'; | ||
|
||
protected parseVariation (variation: string) { | ||
const weight = variation.match(/^([0-9]+)/); | ||
const style = variation.match(/([A-Za-z]+)$/); | ||
return {weight: weight ? Number(weight[0]) : 400, style: style ? style[0] : 'regular'}; | ||
} | ||
} | ||
|
||
/** | ||
* Generates output based on data from a [[FontCollectionParser]] | ||
* | ||
* @ignore | ||
*/ | ||
export class FontCollectionGenerator { | ||
static generateTypeScriptEnum (parser: FontCollectionParser) { | ||
const entries = []; | ||
for (const [font, initializer] of parser.collection.entries()) { | ||
entries.push(`${font}: ${initializer}`); | ||
} | ||
|
||
return `import {Font, FontStyle} from '../font'; | ||
/** | ||
* As a convenience, this enumeration provides the names of all the core fonts supported on ${this.name}. | ||
*/ | ||
export const ${parser.name} = { | ||
${entries.join(',\n ')}, | ||
}; | ||
`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ export const enum FileType { | |
Raw = 'raw', | ||
Image = 'image', | ||
Font = 'font', | ||
Remote = 'remote', | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.