-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.ts
More file actions
103 lines (80 loc) · 2.82 KB
/
image.ts
File metadata and controls
103 lines (80 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { Config } from "../types/config";
import { hasComposer, hasNpm, hasYarn } from "./packageManagers";
import type { LockFile } from "../types/lockFile";
import { detectIcon } from "./icons";
import type { Package } from "../types/package";
import { encodeUri } from "./strings";
const command = (manager: string, dev: boolean, global: boolean): string => {
switch (manager) {
case "composer":
return `composer${global ? " global" : ""} require${dev ? " --dev" : ""}`;
case "npm":
return `npm install${global ? " -g" : ""}${dev ? " -D" : ""}`;
case "yarn":
return `yarn${global ? " global" : ""} add${dev ? " -D" : ""}`;
default:
return manager;
}
};
const detectPackageManager = (config: Config): string => {
if (hasComposer(config)) {
return "composer";
}
if (hasNpm(config)) {
return "npm";
}
if (hasYarn(config)) {
return "yarn";
}
return "none";
};
const packageManager = (config: Config): string => {
const global: boolean = config.package?.global || false;
const dev: boolean = config.package?.dev || false;
let name: string = config.package?.manager || "auto";
if (name === "none") {
return "";
}
if (name === "auto") {
name = detectPackageManager(config);
}
if (["composer", "npm", "yarn"].includes(name)) {
return command(name, dev, global);
}
return name.trim();
};
const packageName = (data?: Package): string => {
if (data?.manager === "none") {
return "";
}
return data?.name || "";
};
const render = (
config: Config,
packageData: LockFile,
theme: "light" | "dark",
): string => {
let url: string = config.image?.url || "";
const parameters: Record<string, string> = config.image?.parameters || {};
parameters.theme = theme;
parameters.packageManager = packageManager(config);
parameters.packageName = packageName(config.package);
parameters.images = detectIcon(config.image, packageData);
url = url
.replace("{title}", encodeUri(config.data?.title || ""))
.replace("{description}", encodeUri(config.data?.description || ""));
for (const [key, value] of Object.entries(parameters)) {
url = url.replace(`{${key}}`, encodeUri(value));
}
const query: string = new URLSearchParams(parameters).toString();
return `${url}?${query}`;
};
export const getImages = (config: Config, packageData: LockFile): string => {
const title: string | undefined = config.data?.title;
const light: string = render(config, packageData, "light");
const dark: string = render(config, packageData, "dark");
return `<picture>
<source media="(prefers-color-scheme: dark)" srcset="${dark}">
<img src="${light}" alt="${title}">
</picture>`;
};