fix: #1734 fix local icons path (#1821)

This commit is contained in:
Manuel
2024-01-15 21:40:45 +01:00
committed by GitHub
parent 684ce37f4d
commit 52ca28d0ec
5 changed files with 35 additions and 10 deletions

View File

@@ -1,10 +1,13 @@
import Consola from 'consola';
export abstract class AbstractIconRepository { export abstract class AbstractIconRepository {
constructor(readonly copyright?: string) {} protected constructor(readonly copyright?: string) {}
async fetch(): Promise<NormalizedIconRepositoryResult> { async fetch(): Promise<NormalizedIconRepositoryResult> {
try { try {
return await this.fetchInternally(); return await this.fetchInternally();
} catch (err) { } catch (err) {
Consola.error(`Failed to fetch icons from repository '${this.name}': ${err}`);
return { return {
success: false, success: false,
count: 0, count: 0,
@@ -15,6 +18,8 @@ export abstract class AbstractIconRepository {
} }
} }
protected abstract fetchInternally(): Promise<NormalizedIconRepositoryResult>; protected abstract fetchInternally(): Promise<NormalizedIconRepositoryResult>;
protected abstract name: string;
} }
export type NormalizedIconRepositoryResult = { export type NormalizedIconRepositoryResult = {

View File

@@ -51,6 +51,8 @@ export class GitHubIconsRepository extends AbstractIconRepository {
copyright: this.copyright, copyright: this.copyright,
}; };
} }
protected name: string = "GitHub";
} }
type GitHubRepositoryUrl = { type GitHubRepositoryUrl = {

View File

@@ -46,6 +46,8 @@ export class JsdelivrIconsRepository extends AbstractIconRepository {
copyright: this.copyright, copyright: this.copyright,
}; };
} }
protected name: string = "JsDelivr";
} }
type JsdelivrRepositoryUrl = { type JsdelivrRepositoryUrl = {

View File

@@ -1,6 +1,13 @@
import fs from 'fs'; import fs from 'fs';
import { AbstractIconRepository, NormalizedIcon, NormalizedIconRepositoryResult } from './abstract-icons-repository'; import {
AbstractIconRepository,
NormalizedIcon,
NormalizedIconRepositoryResult,
} from './abstract-icons-repository';
import Consola from 'consola';
const iconsDirectory = './public/icons';
export class LocalIconsRepository extends AbstractIconRepository { export class LocalIconsRepository extends AbstractIconRepository {
constructor() { constructor() {
@@ -8,7 +15,8 @@ export class LocalIconsRepository extends AbstractIconRepository {
} }
protected async fetchInternally(): Promise<NormalizedIconRepositoryResult> { protected async fetchInternally(): Promise<NormalizedIconRepositoryResult> {
if (!fs.existsSync('./public/icons')) { if (!fs.existsSync(iconsDirectory)) {
Consola.info('Local icons repository directory does not exist');
return { return {
count: 0, count: 0,
entries: [], entries: [],
@@ -18,24 +26,30 @@ export class LocalIconsRepository extends AbstractIconRepository {
}; };
} }
const files = fs.readdirSync('./public/icons'); const files = fs.readdirSync(iconsDirectory);
Consola.info(`Local icons repository directory exists and contains ${files.length} icons`);
const normalizedEntries = files const normalizedEntries = files
.filter((file) => ['.png', '.svg', '.jpeg', '.jpg'].some((x) => file.endsWith(x))) .filter((file) => ['.png', '.svg', '.jpeg', '.jpg'].some((x) => file.endsWith(x)))
.map( .map(
(file): NormalizedIcon => ({ (file): NormalizedIcon => {
name: file, const stats = fs.statSync(`${iconsDirectory}/${file}`);
url: `./icons/${file}`, return {
size: 0, name: file,
}) url: `/icons/${file}`,
size: stats.size,
};
}
); );
return { return {
entries: normalizedEntries, entries: normalizedEntries,
count: normalizedEntries.length, count: normalizedEntries.length,
success: true, success: true,
name: 'Local', name: this.name,
copyright: this.copyright, copyright: this.copyright,
}; };
} }
protected name: string = "Local";
} }

View File

@@ -35,6 +35,8 @@ export class UnpkgIconsRepository extends AbstractIconRepository {
copyright: this.copyright, copyright: this.copyright,
}; };
} }
protected name: string = "UnPkg";
} }
type UnpkgResponse = { type UnpkgResponse = {