Skip to content

Commit

Permalink
feat: fonts command (#5452)
Browse files Browse the repository at this point in the history
* feat: fonts command

* handle missing fonts folder

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>

* handle missing custom fonts

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
  • Loading branch information
janoshrubos and rigor789 authored Dec 29, 2020
1 parent ee6bfb5 commit a6b0e3c
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 66 deletions.
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ injector.requireCommand("preview", "./commands/preview");

injector.requireCommand("debug|ios", "./commands/debug");
injector.requireCommand("debug|android", "./commands/debug");
injector.requireCommand("fonts", "./commands/fonts");

injector.requireCommand("prepare", "./commands/prepare");
injector.requireCommand("build|ios", "./commands/build");
Expand Down
69 changes: 69 additions & 0 deletions lib/commands/fonts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { IProjectConfigService, IProjectData } from "../definitions/project";
import { ICommand, ICommandParameter } from "../common/definitions/commands";
import { injector } from "../common/yok";
import { IFileSystem } from "../common/declarations";
import * as constants from "../constants";
import * as fontFinder from "font-finder";
import { createTable } from "../common/helpers";
import * as path from "path";

export class FontsCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];

constructor(
private $projectData: IProjectData,
private $fs: IFileSystem,
private $logger: ILogger,
private $projectConfigService: IProjectConfigService
) {
this.$projectData.initializeProjectData();
}

public async execute(args: string[]): Promise<void> {
const supportedExtensions = [".ttf", ".otf"];

const defaultFontsFolderPaths = [
path.join(
this.$projectConfigService.getValue("appPath") ?? "",
constants.FONTS_DIR
),
path.join(constants.APP_FOLDER_NAME, constants.FONTS_DIR),
path.join(constants.SRC_DIR, constants.FONTS_DIR),
].map((entry) => path.resolve(this.$projectData.projectDir, entry));

const fontsFolderPath = defaultFontsFolderPaths.find((entry) =>
this.$fs.exists(entry)
);

if (!fontsFolderPath) {
this.$logger.warn("No fonts folder found.");
return;
}

const files = this.$fs
.readDirectory(fontsFolderPath)
.map((entry) => path.parse(entry))
.filter((entry) => {
return supportedExtensions.includes(entry.ext);
});

if (!files.length) {
this.$logger.warn("No custom fonts found.");
return;
}

const table: any = createTable(["Font", "CSS Properties"], []);

for (const file of files) {
const font = await fontFinder.get(fontsFolderPath + "/" + file.base);
table.push([
file.base,
`font-family: "${font.name}", "${file.name}"; font-weight: ${font.weight};`,
]);
}

this.$logger.info(table.toString());
}
}

injector.registerCommand("fonts", FontsCommand);
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const AWAIT_NOTIFICATION_TIMEOUT_SECONDS = 9;
export const SRC_DIR = "src";
export const MAIN_DIR = "main";
export const ASSETS_DIR = "assets";
export const FONTS_DIR = "fonts";
export const ANDROID_ANALYTICS_DATA_DIR = "analytics";
export const ANDROID_ANALYTICS_DATA_FILE = "build-statistics.json";
export const MANIFEST_FILE_NAME = "AndroidManifest.xml";
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"detect-newline": "3.1.0",
"email-validator": "2.0.4",
"esprima": "4.0.1",
"font-finder": "1.1.0",
"glob": "7.1.6",
"ios-device-lib": "0.8.0",
"ios-mobileprovision-finder": "1.0.11",
Expand Down
Loading

0 comments on commit a6b0e3c

Please sign in to comment.