Skip to content

Commit 2826435

Browse files
Integrate package data handling and dynamic icon detection for preview generation, with comprehensive test coverage and fixture additions.
1 parent 8c0ad4f commit 2826435

File tree

15 files changed

+338
-84
lines changed

15 files changed

+338
-84
lines changed

dist/index.js

Lines changed: 167 additions & 70 deletions
Large diffs are not rendered by default.

src/main.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { setOutputs } from "./utils/outputs";
99
import { getPackageManager } from "./utils/packageManagers";
1010
import { titleCase } from "./utils/strings";
1111
import { readConfig } from "./utils/config";
12+
import type { Package } from "./types/package";
1213

1314
const previewUpdater = async () => {
1415
// Inputs
@@ -28,12 +29,12 @@ const previewUpdater = async () => {
2829
);
2930

3031
// Read names
31-
const packageManager = getPackageManager(config);
32+
const packageData: Package = getPackageManager(config);
3233

33-
config.image.parameters.packageName ||= packageManager.name;
34+
config.image.parameters.packageName ||= packageData.name;
3435
config.image.parameters.title ||= titleCase(config.repository.repo);
3536
config.image.parameters.description ||=
36-
packageManager.description || config.repository.owner;
37+
packageData.description || config.repository.owner;
3738

3839
// Show working directory
3940
info(`Working directory: ${config.directory}`);
@@ -44,7 +45,7 @@ const previewUpdater = async () => {
4445

4546
// Read file
4647
const content = readFile(config, config.path.readme);
47-
const preview = setPreview(content, config);
48+
const preview = setPreview(content, config, packageData);
4849

4950
if (content === preview) {
5051
info(`File "${config.path.readme}" is up to date`);

src/types/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface ImageParameters {
22
pattern: string;
33
style: string;
44
fontSize: string;
5-
icon: string;
5+
icon?: string;
66

77
packageManager: "composer" | "npm" | "yarn" | "auto" | "none" | string;
88
packageName?: string;
@@ -69,7 +69,7 @@ export const defaultConfig: Config = {
6969
pattern: "topography",
7070
style: "style_2",
7171
fontSize: "100px",
72-
icon: "code",
72+
icon: undefined,
7373

7474
packageManager: "auto",
7575
packageGlobal: false,

src/types/icons.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Icon {
2+
query: string;
3+
icon: string;
4+
}

src/types/package.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export interface Package {
22
name: string;
33
description: string;
4+
5+
dependencies?: Record<string, string>[];
6+
require?: Record<string, string>[];
47
}

src/utils/icons.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Icon } from "../types/icons";
2+
import type { Package } from "../types/package";
3+
4+
export const phpIcons: Icon[] = [
5+
{ query: "laravel/", icon: "https://laravel.com/img/logomark.min.svg" },
6+
{ query: "illuminate/", icon: "https://laravel.com/img/logomark.min.svg" },
7+
{
8+
query: "symfony/",
9+
icon: "https://symfony.com/logos/symfony_black_03.svg",
10+
},
11+
];
12+
13+
export const jsIcons: Icon[] = [];
14+
15+
export const defaultPhpIcon =
16+
"https://www.php.net/images/logos/new-php-logo.svg";
17+
export const defaultJsIcon = "code";
18+
19+
const find = (
20+
dependencies: Record<string, string>[],
21+
icons: Icon[],
22+
): string | undefined => {
23+
const names: string[] = Object.keys(dependencies);
24+
25+
for (const name of names) {
26+
for (const icon of icons) {
27+
if (name.includes(icon.query)) {
28+
return icon.icon;
29+
}
30+
}
31+
}
32+
33+
return undefined;
34+
};
35+
36+
export const detectIcon = (packageData: Package): string => {
37+
if (packageData?.require !== undefined) {
38+
const phpIcon: string | undefined = find(packageData.require, phpIcons);
39+
40+
return phpIcon || defaultPhpIcon;
41+
}
42+
43+
if (packageData?.dependencies !== undefined) {
44+
const jsIcon: string | undefined = find(
45+
packageData.dependencies,
46+
jsIcons,
47+
);
48+
49+
return jsIcon || defaultJsIcon;
50+
}
51+
52+
return "code";
53+
};

src/utils/image.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Config, ImageParameters } from "../types/config";
22
import { hasComposer, hasNpm, hasYarn } from "./packageManagers";
33
import { encodeUri } from "./strings";
4+
import type { Package } from "../types/package";
5+
import { detectIcon } from "./icons";
46

57
const detectPackageManager = (config: Config, visibility: string): string => {
68
if (hasComposer(config)) {
@@ -45,15 +47,19 @@ const packageName = (image: ImageParameters): string => {
4547
return image?.packageName || "";
4648
};
4749

48-
const render = (config: Config, theme: "light" | "dark"): string => {
50+
const render = (
51+
config: Config,
52+
packageData: Package,
53+
theme: "light" | "dark",
54+
): string => {
4955
const image = config.image.parameters;
5056

5157
const params = new URLSearchParams({
5258
theme: theme,
5359
pattern: image.pattern,
5460
style: image.style,
5561
fontSize: image.fontSize,
56-
images: image.icon,
62+
images: image.icon || detectIcon(packageData),
5763
packageManager: packageManager(config),
5864
packageName: packageName(image),
5965
description: image.description || "",
@@ -66,11 +72,11 @@ const render = (config: Config, theme: "light" | "dark"): string => {
6672
);
6773
};
6874

69-
export const getImages = (config: Config): string => {
75+
export const getImages = (config: Config, packageData: Package): string => {
7076
const title = config.image.parameters.title;
7177

72-
const light = render(config, "light");
73-
const dark = render(config, "dark");
78+
const light = render(config, packageData, "light");
79+
const dark = render(config, packageData, "dark");
7480

7581
return `<picture>
7682
<source media="(prefers-color-scheme: dark)" srcset="${dark}">

src/utils/preview.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { getImages } from "./image";
22
import type { Config } from "../types/config";
33
import { removeImages, titleCase } from "./strings";
4+
import type { Package } from "../types/package";
45

56
const hasHeader = (content: string) => content.match(/^#\s+/);
67

7-
export const setPreview = (content: string, config: Config) => {
8+
export const setPreview = (
9+
content: string,
10+
config: Config,
11+
packageData: Package,
12+
): string => {
813
if (!hasHeader(content)) {
914
const title = titleCase(config.image.parameters.title);
1015

1116
content = `# ${title}\n\n${content}`;
1217
}
1318

14-
const images: string = getImages(config);
19+
const images: string = getImages(config, packageData);
1520

1621
const replace = "$1";
1722

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "dragon-code/any",
3+
"description": "Performing any actions during the deployment process",
4+
"require": {
5+
"php": "^8.5",
6+
"dragon-code/support": "^6.6"
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "dragon-code/any",
3+
"description": "Performing any actions during the deployment process",
4+
"require": {
5+
"php": "^8.5",
6+
"illuminate/support": "^12.0"
7+
}
8+
}

0 commit comments

Comments
 (0)