Skip to content

Commit a6b0e3c

Browse files
feat: fonts command (#5452)
* feat: fonts command * handle missing fonts folder Co-authored-by: Igor Randjelovic <[email protected]> * handle missing custom fonts Co-authored-by: Igor Randjelovic <[email protected]>
1 parent ee6bfb5 commit a6b0e3c

File tree

6 files changed

+182
-66
lines changed

6 files changed

+182
-66
lines changed

lib/bootstrap.ts

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ injector.requireCommand("preview", "./commands/preview");
188188

189189
injector.requireCommand("debug|ios", "./commands/debug");
190190
injector.requireCommand("debug|android", "./commands/debug");
191+
injector.requireCommand("fonts", "./commands/fonts");
191192

192193
injector.requireCommand("prepare", "./commands/prepare");
193194
injector.requireCommand("build|ios", "./commands/build");

lib/commands/fonts.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { IProjectConfigService, IProjectData } from "../definitions/project";
2+
import { ICommand, ICommandParameter } from "../common/definitions/commands";
3+
import { injector } from "../common/yok";
4+
import { IFileSystem } from "../common/declarations";
5+
import * as constants from "../constants";
6+
import * as fontFinder from "font-finder";
7+
import { createTable } from "../common/helpers";
8+
import * as path from "path";
9+
10+
export class FontsCommand implements ICommand {
11+
public allowedParameters: ICommandParameter[] = [];
12+
13+
constructor(
14+
private $projectData: IProjectData,
15+
private $fs: IFileSystem,
16+
private $logger: ILogger,
17+
private $projectConfigService: IProjectConfigService
18+
) {
19+
this.$projectData.initializeProjectData();
20+
}
21+
22+
public async execute(args: string[]): Promise<void> {
23+
const supportedExtensions = [".ttf", ".otf"];
24+
25+
const defaultFontsFolderPaths = [
26+
path.join(
27+
this.$projectConfigService.getValue("appPath") ?? "",
28+
constants.FONTS_DIR
29+
),
30+
path.join(constants.APP_FOLDER_NAME, constants.FONTS_DIR),
31+
path.join(constants.SRC_DIR, constants.FONTS_DIR),
32+
].map((entry) => path.resolve(this.$projectData.projectDir, entry));
33+
34+
const fontsFolderPath = defaultFontsFolderPaths.find((entry) =>
35+
this.$fs.exists(entry)
36+
);
37+
38+
if (!fontsFolderPath) {
39+
this.$logger.warn("No fonts folder found.");
40+
return;
41+
}
42+
43+
const files = this.$fs
44+
.readDirectory(fontsFolderPath)
45+
.map((entry) => path.parse(entry))
46+
.filter((entry) => {
47+
return supportedExtensions.includes(entry.ext);
48+
});
49+
50+
if (!files.length) {
51+
this.$logger.warn("No custom fonts found.");
52+
return;
53+
}
54+
55+
const table: any = createTable(["Font", "CSS Properties"], []);
56+
57+
for (const file of files) {
58+
const font = await fontFinder.get(fontsFolderPath + "/" + file.base);
59+
table.push([
60+
file.base,
61+
`font-family: "${font.name}", "${file.name}"; font-weight: ${font.weight};`,
62+
]);
63+
}
64+
65+
this.$logger.info(table.toString());
66+
}
67+
}
68+
69+
injector.registerCommand("fonts", FontsCommand);

lib/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export const AWAIT_NOTIFICATION_TIMEOUT_SECONDS = 9;
4545
export const SRC_DIR = "src";
4646
export const MAIN_DIR = "main";
4747
export const ASSETS_DIR = "assets";
48+
export const FONTS_DIR = "fonts";
4849
export const ANDROID_ANALYTICS_DATA_DIR = "analytics";
4950
export const ANDROID_ANALYTICS_DATA_FILE = "build-statistics.json";
5051
export const MANIFEST_FILE_NAME = "AndroidManifest.xml";

package-lock.json

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"detect-newline": "3.1.0",
6969
"email-validator": "2.0.4",
7070
"esprima": "4.0.1",
71+
"font-finder": "1.1.0",
7172
"glob": "7.1.6",
7273
"ios-device-lib": "0.8.0",
7374
"ios-mobileprovision-finder": "1.0.11",

0 commit comments

Comments
 (0)