Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 167 additions & 70 deletions dist/index.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { setOutputs } from "./utils/outputs";
import { getPackageManager } from "./utils/packageManagers";
import { titleCase } from "./utils/strings";
import { readConfig } from "./utils/config";
import type { Package } from "./types/package";

const previewUpdater = async () => {
// Inputs
Expand All @@ -28,12 +29,12 @@ const previewUpdater = async () => {
);

// Read names
const packageManager = getPackageManager(config);
const packageData: Package = getPackageManager(config);

config.image.parameters.packageName ||= packageManager.name;
config.image.parameters.packageName ||= packageData.name;
config.image.parameters.title ||= titleCase(config.repository.repo);
config.image.parameters.description ||=
packageManager.description || config.repository.owner;
packageData.description || config.repository.owner;

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

// Read file
const content = readFile(config, config.path.readme);
const preview = setPreview(content, config);
const preview = setPreview(content, config, packageData);

if (content === preview) {
info(`File "${config.path.readme}" is up to date`);
Expand Down
4 changes: 2 additions & 2 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface ImageParameters {
pattern: string;
style: string;
fontSize: string;
icon: string;
icon?: string;

packageManager: "composer" | "npm" | "yarn" | "auto" | "none" | string;
packageName?: string;
Expand Down Expand Up @@ -69,7 +69,7 @@ export const defaultConfig: Config = {
pattern: "topography",
style: "style_2",
fontSize: "100px",
icon: "code",
icon: undefined,

packageManager: "auto",
packageGlobal: false,
Expand Down
4 changes: 4 additions & 0 deletions src/types/icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Icon {
query: string;
icon: string;
}
3 changes: 3 additions & 0 deletions src/types/package.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export interface Package {
name: string;
description: string;

dependencies?: Record<string, string>[];
require?: Record<string, string>[];
}
53 changes: 53 additions & 0 deletions src/utils/icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Icon } from "../types/icons";
import type { Package } from "../types/package";

export const phpIcons: Icon[] = [
{ query: "laravel/", icon: "https://laravel.com/img/logomark.min.svg" },
{ query: "illuminate/", icon: "https://laravel.com/img/logomark.min.svg" },
{
query: "symfony/",
icon: "https://symfony.com/logos/symfony_black_03.svg",
},
];

export const jsIcons: Icon[] = [];

export const defaultPhpIcon =
"https://www.php.net/images/logos/new-php-logo.svg";
export const defaultJsIcon = "code";

const find = (
dependencies: Record<string, string>[],
icons: Icon[],
): string | undefined => {
const names: string[] = Object.keys(dependencies);

for (const name of names) {
for (const icon of icons) {
if (name.includes(icon.query)) {
return icon.icon;
}
}
}

return undefined;
};

export const detectIcon = (packageData: Package): string => {
if (packageData?.require !== undefined) {
const phpIcon: string | undefined = find(packageData.require, phpIcons);

return phpIcon || defaultPhpIcon;
}

if (packageData?.dependencies !== undefined) {
const jsIcon: string | undefined = find(
packageData.dependencies,
jsIcons,
);

return jsIcon || defaultJsIcon;
}

return "code";
};
16 changes: 11 additions & 5 deletions src/utils/image.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Config, ImageParameters } from "../types/config";
import { hasComposer, hasNpm, hasYarn } from "./packageManagers";
import { encodeUri } from "./strings";
import type { Package } from "../types/package";
import { detectIcon } from "./icons";

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

const render = (config: Config, theme: "light" | "dark"): string => {
const render = (
config: Config,
packageData: Package,
theme: "light" | "dark",
): string => {
const image = config.image.parameters;

const params = new URLSearchParams({
theme: theme,
pattern: image.pattern,
style: image.style,
fontSize: image.fontSize,
images: image.icon,
images: image.icon || detectIcon(packageData),
packageManager: packageManager(config),
packageName: packageName(image),
description: image.description || "",
Expand All @@ -66,11 +72,11 @@ const render = (config: Config, theme: "light" | "dark"): string => {
);
};

export const getImages = (config: Config): string => {
export const getImages = (config: Config, packageData: Package): string => {
const title = config.image.parameters.title;

const light = render(config, "light");
const dark = render(config, "dark");
const light = render(config, packageData, "light");
const dark = render(config, packageData, "dark");

return `<picture>
<source media="(prefers-color-scheme: dark)" srcset="${dark}">
Expand Down
9 changes: 7 additions & 2 deletions src/utils/preview.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { getImages } from "./image";
import type { Config } from "../types/config";
import { removeImages, titleCase } from "./strings";
import type { Package } from "../types/package";

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

export const setPreview = (content: string, config: Config) => {
export const setPreview = (
content: string,
config: Config,
packageData: Package,
): string => {
if (!hasHeader(content)) {
const title = titleCase(config.image.parameters.title);

content = `# ${title}\n\n${content}`;
}

const images: string = getImages(config);
const images: string = getImages(config, packageData);

const replace = "$1";

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/packages/composer-default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "dragon-code/any",
"description": "Performing any actions during the deployment process",
"require": {
"php": "^8.5",
"dragon-code/support": "^6.6"
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/packages/composer-illuminate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "dragon-code/any",
"description": "Performing any actions during the deployment process",
"require": {
"php": "^8.5",
"illuminate/support": "^12.0"
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/packages/composer-laravel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "dragon-code/any",
"description": "Performing any actions during the deployment process",
"require": {
"php": "^8.5",
"laravel/framework": "^12.0"
}
}
8 changes: 8 additions & 0 deletions tests/fixtures/packages/composer-symfony.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "dragon-code/any",
"description": "Performing any actions during the deployment process",
"require": {
"php": "^8.5",
"symfony/framework": "^12.0"
}
}
10 changes: 10 additions & 0 deletions tests/fixtures/packages/npm-default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "preview-updater",
"description": "Lightweight preview update in your repository",
"dependencies": {
"@actions/core": "^2.0.2",
"@actions/github": "^7.0.0",
"deepmerge-ts": "^7.1.5",
"js-yaml": "^4.1.1"
}
}
8 changes: 7 additions & 1 deletion tests/helpers/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { readFile } from "../../src/utils/filesystem";
import { setPreview } from "../../src/utils/preview";
import { testConfig } from "./config";
import type { Package } from "../../src/types/package";
import { getNpm } from "../../src/utils/packageManagers";

export const getReadme = (filename: string): string => {
const content = readFile(testConfig, `tests/fixtures/readme/${filename}`);

return setPreview(content, testConfig);
return setPreview(content, testConfig, getNpm(testConfig));
};

export const getPackage = (filename: string): Package => {
return <Package>JSON.parse(readFile(testConfig, filename));
};
37 changes: 37 additions & 0 deletions tests/unit/icons.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getPackage } from "../helpers/filesystem";
import {
defaultJsIcon,
defaultPhpIcon,
detectIcon,
phpIcons,
} from "../../src/utils/icons";

test("composer laravel", () => {
const data = getPackage("tests/fixtures/packages/composer-laravel.json");

expect(detectIcon(data)).toBe(phpIcons[0].icon);
});

test("composer illuminate", () => {
const data = getPackage("tests/fixtures/packages/composer-illuminate.json");

expect(detectIcon(data)).toBe(phpIcons[1].icon);
});

test("composer symfony", () => {
const data = getPackage("tests/fixtures/packages/composer-symfony.json");

expect(detectIcon(data)).toBe(phpIcons[2].icon);
});

test("composer default", () => {
const data = getPackage("tests/fixtures/packages/composer-default.json");

expect(detectIcon(data)).toBe(defaultPhpIcon);
});

test("npm default", () => {
const data = getPackage("tests/fixtures/packages/npm-default.json");

expect(detectIcon(data)).toBe(defaultJsIcon);
});