|
| 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); |
0 commit comments