app-icon-badge-next 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +264 -0
- package/app.plugin.js +1 -0
- package/dist/app.plugin.js +268 -0
- package/dist/assets/banner-overlay-adaptive.png +0 -0
- package/dist/assets/banner-overlay.png +0 -0
- package/dist/assets/fonts/open-sans-128-black.fnt +297 -0
- package/dist/assets/fonts/open-sans-128-black.png +0 -0
- package/dist/assets/fonts/open-sans-128-white.fnt +297 -0
- package/dist/assets/fonts/open-sans-128-white.png +0 -0
- package/dist/assets/fonts/open-sans-64-black.fnt +297 -0
- package/dist/assets/fonts/open-sans-64-black.png +0 -0
- package/dist/assets/fonts/open-sans-64-white.fnt +297 -0
- package/dist/assets/fonts/open-sans-64-white.png +0 -0
- package/dist/assets/ribbon-overlay-adaptive.png +0 -0
- package/dist/assets/ribbon-overlay.png +0 -0
- package/dist/cli.js +292 -0
- package/dist/generate.js +298 -0
- package/dist/index.d.mts +53 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +252 -0
- package/dist/index.mjs +217 -0
- package/package.json +82 -0
- package/types.ts +55 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
+
mod
|
|
24
|
+
));
|
|
25
|
+
|
|
26
|
+
// src/cli.ts
|
|
27
|
+
var import_commander = require("commander");
|
|
28
|
+
var import_zod2 = require("zod");
|
|
29
|
+
|
|
30
|
+
// package.json
|
|
31
|
+
var version = "0.2.0";
|
|
32
|
+
|
|
33
|
+
// src/core/validation.ts
|
|
34
|
+
var import_zod = require("zod");
|
|
35
|
+
var hexColor = import_zod.z.string().regex(/^#[0-9a-fA-F]{6}$/);
|
|
36
|
+
var baseBadge = {
|
|
37
|
+
text: import_zod.z.string().min(1),
|
|
38
|
+
color: import_zod.z.enum(["white", "black"]).default("white"),
|
|
39
|
+
background: hexColor.optional(),
|
|
40
|
+
fontScale: import_zod.z.number().min(0.5).max(2).default(1),
|
|
41
|
+
letterSpacing: import_zod.z.number().min(0).max(10).default(0),
|
|
42
|
+
padding: import_zod.z.number().min(0).max(120).default(0),
|
|
43
|
+
opacity: import_zod.z.number().min(0).max(1).default(1)
|
|
44
|
+
};
|
|
45
|
+
var banner = import_zod.z.object({
|
|
46
|
+
type: import_zod.z.literal("banner"),
|
|
47
|
+
position: import_zod.z.enum(["top", "bottom"]).default("bottom"),
|
|
48
|
+
...baseBadge
|
|
49
|
+
});
|
|
50
|
+
var ribbon = import_zod.z.object({
|
|
51
|
+
type: import_zod.z.literal("ribbon"),
|
|
52
|
+
position: import_zod.z.enum(["left", "right"]).default("right"),
|
|
53
|
+
...baseBadge
|
|
54
|
+
});
|
|
55
|
+
var badgeSchema = import_zod.z.discriminatedUnion("type", [banner, ribbon]);
|
|
56
|
+
var addBadgeParamsSchema = import_zod.z.object({
|
|
57
|
+
icon: import_zod.z.string().min(1),
|
|
58
|
+
dstPath: import_zod.z.string().optional(),
|
|
59
|
+
badges: import_zod.z.array(badgeSchema).min(1),
|
|
60
|
+
isAdaptiveIcon: import_zod.z.boolean().default(false),
|
|
61
|
+
adaptiveSafeMode: import_zod.z.boolean().default(true),
|
|
62
|
+
safeZoneScale: import_zod.z.number().min(0.4).max(1).default(0.72),
|
|
63
|
+
debug: import_zod.z.boolean().default(false),
|
|
64
|
+
dryRun: import_zod.z.boolean().default(false)
|
|
65
|
+
});
|
|
66
|
+
var pluginOptionsSchema = import_zod.z.object({
|
|
67
|
+
enabled: import_zod.z.boolean().default(true),
|
|
68
|
+
badges: import_zod.z.array(badgeSchema).optional(),
|
|
69
|
+
ios: import_zod.z.object({ badges: import_zod.z.array(badgeSchema).optional() }).default({}),
|
|
70
|
+
android: import_zod.z.object({
|
|
71
|
+
badges: import_zod.z.array(badgeSchema).optional(),
|
|
72
|
+
safeZoneScale: import_zod.z.number().min(0.4).max(1).default(0.72)
|
|
73
|
+
}).default({}),
|
|
74
|
+
adaptiveSafeMode: import_zod.z.boolean().default(true),
|
|
75
|
+
cache: import_zod.z.boolean().default(true),
|
|
76
|
+
debug: import_zod.z.boolean().default(false),
|
|
77
|
+
dryRun: import_zod.z.boolean().default(false)
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// src/index.ts
|
|
81
|
+
var import_node_fs3 = __toESM(require("fs"));
|
|
82
|
+
var import_node_path4 = __toESM(require("path"));
|
|
83
|
+
var import_jimp2 = __toESM(require("jimp"));
|
|
84
|
+
|
|
85
|
+
// src/core/render.ts
|
|
86
|
+
var import_node_fs2 = __toESM(require("fs"));
|
|
87
|
+
var import_node_path3 = __toESM(require("path"));
|
|
88
|
+
var import_color_convert = __toESM(require("color-convert"));
|
|
89
|
+
var import_delta_e = __toESM(require("delta-e"));
|
|
90
|
+
var import_jimp = __toESM(require("jimp"));
|
|
91
|
+
|
|
92
|
+
// src/core/fonts.ts
|
|
93
|
+
var import_node_path = __toESM(require("path"));
|
|
94
|
+
function getFontPath(isAdaptiveIcon, isBlack, scale = 1) {
|
|
95
|
+
const useLarge = scale >= 1 && !isAdaptiveIcon;
|
|
96
|
+
const fontPrefix = useLarge ? "128" : "64";
|
|
97
|
+
const color = isBlack ? "black" : "white";
|
|
98
|
+
return import_node_path.default.resolve(__dirname, `./assets/fonts/open-sans-${fontPrefix}-${color}.fnt`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/core/utils.ts
|
|
102
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
103
|
+
var import_node_fs = __toESM(require("fs"));
|
|
104
|
+
var import_node_path2 = __toESM(require("path"));
|
|
105
|
+
function withLetterSpacing(input, spacing = 0) {
|
|
106
|
+
if (spacing <= 0) return input;
|
|
107
|
+
const gap = " ".repeat(Math.max(1, Math.round(spacing)));
|
|
108
|
+
return input.split("").join(gap);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/core/render.ts
|
|
112
|
+
function replaceColor(image, from, to) {
|
|
113
|
+
const fromColor = import_color_convert.default.hex.lab(from);
|
|
114
|
+
const toColor = import_color_convert.default.hex.rgb(to);
|
|
115
|
+
image.scan(0, 0, image.bitmap.width, image.bitmap.height, (_x, _y, idx) => {
|
|
116
|
+
if (image.bitmap.data[idx + 3] === 0) return;
|
|
117
|
+
const currentLABColor = import_color_convert.default.rgb.lab([
|
|
118
|
+
image.bitmap.data[idx],
|
|
119
|
+
image.bitmap.data[idx + 1],
|
|
120
|
+
image.bitmap.data[idx + 2]
|
|
121
|
+
]);
|
|
122
|
+
const delta = import_delta_e.default.getDeltaE00(
|
|
123
|
+
{ L: currentLABColor[0], A: currentLABColor[1], B: currentLABColor[2] },
|
|
124
|
+
{ L: fromColor[0], A: fromColor[1], B: fromColor[2] }
|
|
125
|
+
);
|
|
126
|
+
if (delta <= 2.3) {
|
|
127
|
+
image.bitmap.data[idx] = toColor[0];
|
|
128
|
+
image.bitmap.data[idx + 1] = toColor[1];
|
|
129
|
+
image.bitmap.data[idx + 2] = toColor[2];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
async function loadOverlay(overlayPath, background) {
|
|
134
|
+
const overlay = await import_jimp.default.read(overlayPath);
|
|
135
|
+
if (background) {
|
|
136
|
+
replaceColor(overlay, "#000000", background);
|
|
137
|
+
}
|
|
138
|
+
return overlay;
|
|
139
|
+
}
|
|
140
|
+
async function createBadgeImage(badge, isAdaptiveIcon) {
|
|
141
|
+
const variant = isAdaptiveIcon ? "-adaptive" : "";
|
|
142
|
+
const overlayPath = import_node_path3.default.resolve(__dirname, `./assets/${badge.type}-overlay${variant}.png`);
|
|
143
|
+
if (!import_node_fs2.default.existsSync(overlayPath)) {
|
|
144
|
+
throw new Error(`Overlay not found: ${overlayPath}`);
|
|
145
|
+
}
|
|
146
|
+
const overlay = await loadOverlay(overlayPath, badge.background);
|
|
147
|
+
const font = await import_jimp.default.loadFont(
|
|
148
|
+
getFontPath(isAdaptiveIcon, badge.color === "black", badge.fontScale)
|
|
149
|
+
);
|
|
150
|
+
const text = withLetterSpacing(
|
|
151
|
+
badge.type === "banner" ? badge.text.toUpperCase() : badge.text,
|
|
152
|
+
badge.letterSpacing
|
|
153
|
+
);
|
|
154
|
+
const padding = badge.padding;
|
|
155
|
+
const base = overlay.clone();
|
|
156
|
+
if (badge.opacity < 1) {
|
|
157
|
+
base.opacity(badge.opacity);
|
|
158
|
+
}
|
|
159
|
+
if (badge.type === "banner") {
|
|
160
|
+
const bannerHeight = isAdaptiveIcon ? 310 : 180;
|
|
161
|
+
const textContainer2 = new import_jimp.default(overlay.bitmap.width, bannerHeight, "transparent");
|
|
162
|
+
const alignY = isAdaptiveIcon ? badge.position === "top" ? import_jimp.default.VERTICAL_ALIGN_BOTTOM : import_jimp.default.VERTICAL_ALIGN_TOP : import_jimp.default.VERTICAL_ALIGN_MIDDLE;
|
|
163
|
+
textContainer2.print(
|
|
164
|
+
font,
|
|
165
|
+
padding,
|
|
166
|
+
0,
|
|
167
|
+
{ text, alignmentX: import_jimp.default.HORIZONTAL_ALIGN_CENTER, alignmentY: alignY },
|
|
168
|
+
overlay.bitmap.width - padding * 2,
|
|
169
|
+
bannerHeight
|
|
170
|
+
);
|
|
171
|
+
const top = badge.position === "top";
|
|
172
|
+
base.flip(false, top);
|
|
173
|
+
base.composite(textContainer2, 0, top ? 0 : base.bitmap.height - bannerHeight);
|
|
174
|
+
return base;
|
|
175
|
+
}
|
|
176
|
+
const ribbonHeight = isAdaptiveIcon ? 100 : 180;
|
|
177
|
+
const translateBy = (isAdaptiveIcon ? 80 : 270) - padding;
|
|
178
|
+
const textContainer = new import_jimp.default(overlay.bitmap.width, ribbonHeight, "transparent");
|
|
179
|
+
textContainer.print(
|
|
180
|
+
font,
|
|
181
|
+
0,
|
|
182
|
+
0,
|
|
183
|
+
{ text, alignmentX: import_jimp.default.HORIZONTAL_ALIGN_CENTER, alignmentY: import_jimp.default.VERTICAL_ALIGN_MIDDLE },
|
|
184
|
+
overlay.bitmap.width,
|
|
185
|
+
ribbonHeight
|
|
186
|
+
);
|
|
187
|
+
const left = badge.position === "left";
|
|
188
|
+
textContainer.rotate(left ? 45 : -45);
|
|
189
|
+
const translateX = left ? -translateBy : translateBy;
|
|
190
|
+
const textX = left ? translateX : overlay.bitmap.width - textContainer.bitmap.width + translateX;
|
|
191
|
+
const textY = left ? translateX : -translateX;
|
|
192
|
+
base.flip(left, false);
|
|
193
|
+
base.composite(textContainer, textX, textY);
|
|
194
|
+
return base;
|
|
195
|
+
}
|
|
196
|
+
function applyAdaptiveSafeZone(badge, safeZoneScale) {
|
|
197
|
+
const canvas = new import_jimp.default(badge.bitmap.width, badge.bitmap.height, "transparent");
|
|
198
|
+
const scaled = badge.resize(
|
|
199
|
+
Math.round(badge.bitmap.width * safeZoneScale),
|
|
200
|
+
Math.round(badge.bitmap.height * safeZoneScale)
|
|
201
|
+
);
|
|
202
|
+
const x = Math.round((canvas.bitmap.width - scaled.bitmap.width) / 2);
|
|
203
|
+
const y = Math.round((canvas.bitmap.height - scaled.bitmap.height) / 2);
|
|
204
|
+
canvas.composite(scaled, x, y);
|
|
205
|
+
return canvas;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// src/index.ts
|
|
209
|
+
function getResultPath(icon) {
|
|
210
|
+
const parts = icon.split(".");
|
|
211
|
+
parts.splice(parts.length - 1, 0, "result");
|
|
212
|
+
return parts.join(".");
|
|
213
|
+
}
|
|
214
|
+
function logDebug(debug, payload) {
|
|
215
|
+
if (!debug) return;
|
|
216
|
+
process.stdout.write(`[app-icon-badge-next] ${JSON.stringify(payload)}
|
|
217
|
+
`);
|
|
218
|
+
}
|
|
219
|
+
async function addBadge(input) {
|
|
220
|
+
const parsed = addBadgeParamsSchema.parse(input);
|
|
221
|
+
const sourcePath = import_node_path4.default.resolve(parsed.icon);
|
|
222
|
+
if (!import_node_fs3.default.existsSync(sourcePath)) {
|
|
223
|
+
throw new Error(`Icon does not exist: ${sourcePath}`);
|
|
224
|
+
}
|
|
225
|
+
const destination = import_node_path4.default.resolve(parsed.dstPath ?? getResultPath(parsed.icon));
|
|
226
|
+
logDebug(parsed.debug, {
|
|
227
|
+
icon: sourcePath,
|
|
228
|
+
dstPath: destination,
|
|
229
|
+
isAdaptiveIcon: parsed.isAdaptiveIcon,
|
|
230
|
+
adaptiveSafeMode: parsed.adaptiveSafeMode,
|
|
231
|
+
safeZoneScale: parsed.safeZoneScale,
|
|
232
|
+
badgeCount: parsed.badges.length
|
|
233
|
+
});
|
|
234
|
+
if (parsed.dryRun) {
|
|
235
|
+
return destination;
|
|
236
|
+
}
|
|
237
|
+
const image = await import_jimp2.default.read(sourcePath);
|
|
238
|
+
for (const badge of parsed.badges) {
|
|
239
|
+
const badgeImage = await createBadgeImage(badge, parsed.isAdaptiveIcon);
|
|
240
|
+
const finalBadge = parsed.isAdaptiveIcon && parsed.adaptiveSafeMode ? applyAdaptiveSafeZone(badgeImage, parsed.safeZoneScale) : badgeImage;
|
|
241
|
+
image.composite(finalBadge, 0, 0);
|
|
242
|
+
}
|
|
243
|
+
import_node_fs3.default.mkdirSync(import_node_path4.default.dirname(destination), { recursive: true });
|
|
244
|
+
await image.writeAsync(destination);
|
|
245
|
+
return destination;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// src/cli.ts
|
|
249
|
+
var program = new import_commander.Command();
|
|
250
|
+
program.name("app-icon-badge-next").description("Generate app icons with badges").version(version);
|
|
251
|
+
program.command("add-badge").argument("<iconPath>").requiredOption("--text <text>", "badge text").requiredOption("--type <type>", "badge type: banner | ribbon").option("--position <position>", "banner: top | bottom, ribbon: left | right").option("--background <background>", "badge background as #rrggbb").option("--color <color>", "badge text color: white | black", "white").option("--platform <platform>", "target platform: ios | android | all", "all").option("--preview", "write result to preview path", false).option("--safe-zone", "enable adaptive safe zone", false).option("--scale <scale>", "safe zone scale (0.4-1)", "0.72").option("--dry-run", "no file write", false).option("--debug", "debug logs", false).action(async (iconPath, options) => {
|
|
252
|
+
const badge = badgeSchema.parse({
|
|
253
|
+
type: options.type,
|
|
254
|
+
text: options.text,
|
|
255
|
+
position: options.position,
|
|
256
|
+
background: options.background,
|
|
257
|
+
color: options.color
|
|
258
|
+
});
|
|
259
|
+
const platform = options.platform;
|
|
260
|
+
if (platform === "android" || platform === "all") {
|
|
261
|
+
await addBadge({
|
|
262
|
+
icon: iconPath,
|
|
263
|
+
dstPath: options.preview ? iconPath.replace(/\.(png|jpg|jpeg)$/i, ".android.preview.png") : void 0,
|
|
264
|
+
badges: [badge],
|
|
265
|
+
isAdaptiveIcon: true,
|
|
266
|
+
adaptiveSafeMode: options.safeZone,
|
|
267
|
+
safeZoneScale: Number(options.scale),
|
|
268
|
+
dryRun: options.dryRun,
|
|
269
|
+
debug: options.debug
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
if (platform === "ios" || platform === "all") {
|
|
273
|
+
await addBadge({
|
|
274
|
+
icon: iconPath,
|
|
275
|
+
dstPath: options.preview ? iconPath.replace(/\.(png|jpg|jpeg)$/i, ".ios.preview.png") : void 0,
|
|
276
|
+
badges: [badge],
|
|
277
|
+
isAdaptiveIcon: false,
|
|
278
|
+
dryRun: options.dryRun,
|
|
279
|
+
debug: options.debug
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
program.parseAsync(process.argv).catch((error) => {
|
|
284
|
+
if (error instanceof import_zod2.ZodError) {
|
|
285
|
+
for (const issue of error.issues) {
|
|
286
|
+
console.error(`Invalid option ${issue.path.join(".") || "value"}: ${issue.message}`);
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
290
|
+
}
|
|
291
|
+
process.exit(1);
|
|
292
|
+
});
|
package/dist/generate.js
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
|
|
25
|
+
// src/plugin/generate.ts
|
|
26
|
+
var import_node_fs5 = __toESM(require("fs"));
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var import_node_fs3 = __toESM(require("fs"));
|
|
30
|
+
var import_node_path4 = __toESM(require("path"));
|
|
31
|
+
var import_jimp2 = __toESM(require("jimp"));
|
|
32
|
+
|
|
33
|
+
// src/core/validation.ts
|
|
34
|
+
var import_zod = require("zod");
|
|
35
|
+
var hexColor = import_zod.z.string().regex(/^#[0-9a-fA-F]{6}$/);
|
|
36
|
+
var baseBadge = {
|
|
37
|
+
text: import_zod.z.string().min(1),
|
|
38
|
+
color: import_zod.z.enum(["white", "black"]).default("white"),
|
|
39
|
+
background: hexColor.optional(),
|
|
40
|
+
fontScale: import_zod.z.number().min(0.5).max(2).default(1),
|
|
41
|
+
letterSpacing: import_zod.z.number().min(0).max(10).default(0),
|
|
42
|
+
padding: import_zod.z.number().min(0).max(120).default(0),
|
|
43
|
+
opacity: import_zod.z.number().min(0).max(1).default(1)
|
|
44
|
+
};
|
|
45
|
+
var banner = import_zod.z.object({
|
|
46
|
+
type: import_zod.z.literal("banner"),
|
|
47
|
+
position: import_zod.z.enum(["top", "bottom"]).default("bottom"),
|
|
48
|
+
...baseBadge
|
|
49
|
+
});
|
|
50
|
+
var ribbon = import_zod.z.object({
|
|
51
|
+
type: import_zod.z.literal("ribbon"),
|
|
52
|
+
position: import_zod.z.enum(["left", "right"]).default("right"),
|
|
53
|
+
...baseBadge
|
|
54
|
+
});
|
|
55
|
+
var badgeSchema = import_zod.z.discriminatedUnion("type", [banner, ribbon]);
|
|
56
|
+
var addBadgeParamsSchema = import_zod.z.object({
|
|
57
|
+
icon: import_zod.z.string().min(1),
|
|
58
|
+
dstPath: import_zod.z.string().optional(),
|
|
59
|
+
badges: import_zod.z.array(badgeSchema).min(1),
|
|
60
|
+
isAdaptiveIcon: import_zod.z.boolean().default(false),
|
|
61
|
+
adaptiveSafeMode: import_zod.z.boolean().default(true),
|
|
62
|
+
safeZoneScale: import_zod.z.number().min(0.4).max(1).default(0.72),
|
|
63
|
+
debug: import_zod.z.boolean().default(false),
|
|
64
|
+
dryRun: import_zod.z.boolean().default(false)
|
|
65
|
+
});
|
|
66
|
+
var pluginOptionsSchema = import_zod.z.object({
|
|
67
|
+
enabled: import_zod.z.boolean().default(true),
|
|
68
|
+
badges: import_zod.z.array(badgeSchema).optional(),
|
|
69
|
+
ios: import_zod.z.object({ badges: import_zod.z.array(badgeSchema).optional() }).default({}),
|
|
70
|
+
android: import_zod.z.object({
|
|
71
|
+
badges: import_zod.z.array(badgeSchema).optional(),
|
|
72
|
+
safeZoneScale: import_zod.z.number().min(0.4).max(1).default(0.72)
|
|
73
|
+
}).default({}),
|
|
74
|
+
adaptiveSafeMode: import_zod.z.boolean().default(true),
|
|
75
|
+
cache: import_zod.z.boolean().default(true),
|
|
76
|
+
debug: import_zod.z.boolean().default(false),
|
|
77
|
+
dryRun: import_zod.z.boolean().default(false)
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// src/core/render.ts
|
|
81
|
+
var import_node_fs2 = __toESM(require("fs"));
|
|
82
|
+
var import_node_path3 = __toESM(require("path"));
|
|
83
|
+
var import_color_convert = __toESM(require("color-convert"));
|
|
84
|
+
var import_delta_e = __toESM(require("delta-e"));
|
|
85
|
+
var import_jimp = __toESM(require("jimp"));
|
|
86
|
+
|
|
87
|
+
// src/core/fonts.ts
|
|
88
|
+
var import_node_path = __toESM(require("path"));
|
|
89
|
+
function getFontPath(isAdaptiveIcon, isBlack, scale = 1) {
|
|
90
|
+
const useLarge = scale >= 1 && !isAdaptiveIcon;
|
|
91
|
+
const fontPrefix = useLarge ? "128" : "64";
|
|
92
|
+
const color = isBlack ? "black" : "white";
|
|
93
|
+
return import_node_path.default.resolve(__dirname, `./assets/fonts/open-sans-${fontPrefix}-${color}.fnt`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/core/utils.ts
|
|
97
|
+
var import_node_crypto = __toESM(require("crypto"));
|
|
98
|
+
var import_node_fs = __toESM(require("fs"));
|
|
99
|
+
var import_node_path2 = __toESM(require("path"));
|
|
100
|
+
function ensureDir(dir) {
|
|
101
|
+
import_node_fs.default.mkdirSync(dir, { recursive: true });
|
|
102
|
+
}
|
|
103
|
+
function withLetterSpacing(input, spacing = 0) {
|
|
104
|
+
if (spacing <= 0) return input;
|
|
105
|
+
const gap = " ".repeat(Math.max(1, Math.round(spacing)));
|
|
106
|
+
return input.split("").join(gap);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// src/core/render.ts
|
|
110
|
+
function replaceColor(image, from, to) {
|
|
111
|
+
const fromColor = import_color_convert.default.hex.lab(from);
|
|
112
|
+
const toColor = import_color_convert.default.hex.rgb(to);
|
|
113
|
+
image.scan(0, 0, image.bitmap.width, image.bitmap.height, (_x, _y, idx) => {
|
|
114
|
+
if (image.bitmap.data[idx + 3] === 0) return;
|
|
115
|
+
const currentLABColor = import_color_convert.default.rgb.lab([
|
|
116
|
+
image.bitmap.data[idx],
|
|
117
|
+
image.bitmap.data[idx + 1],
|
|
118
|
+
image.bitmap.data[idx + 2]
|
|
119
|
+
]);
|
|
120
|
+
const delta = import_delta_e.default.getDeltaE00(
|
|
121
|
+
{ L: currentLABColor[0], A: currentLABColor[1], B: currentLABColor[2] },
|
|
122
|
+
{ L: fromColor[0], A: fromColor[1], B: fromColor[2] }
|
|
123
|
+
);
|
|
124
|
+
if (delta <= 2.3) {
|
|
125
|
+
image.bitmap.data[idx] = toColor[0];
|
|
126
|
+
image.bitmap.data[idx + 1] = toColor[1];
|
|
127
|
+
image.bitmap.data[idx + 2] = toColor[2];
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async function loadOverlay(overlayPath, background) {
|
|
132
|
+
const overlay = await import_jimp.default.read(overlayPath);
|
|
133
|
+
if (background) {
|
|
134
|
+
replaceColor(overlay, "#000000", background);
|
|
135
|
+
}
|
|
136
|
+
return overlay;
|
|
137
|
+
}
|
|
138
|
+
async function createBadgeImage(badge, isAdaptiveIcon) {
|
|
139
|
+
const variant = isAdaptiveIcon ? "-adaptive" : "";
|
|
140
|
+
const overlayPath = import_node_path3.default.resolve(__dirname, `./assets/${badge.type}-overlay${variant}.png`);
|
|
141
|
+
if (!import_node_fs2.default.existsSync(overlayPath)) {
|
|
142
|
+
throw new Error(`Overlay not found: ${overlayPath}`);
|
|
143
|
+
}
|
|
144
|
+
const overlay = await loadOverlay(overlayPath, badge.background);
|
|
145
|
+
const font = await import_jimp.default.loadFont(
|
|
146
|
+
getFontPath(isAdaptiveIcon, badge.color === "black", badge.fontScale)
|
|
147
|
+
);
|
|
148
|
+
const text = withLetterSpacing(
|
|
149
|
+
badge.type === "banner" ? badge.text.toUpperCase() : badge.text,
|
|
150
|
+
badge.letterSpacing
|
|
151
|
+
);
|
|
152
|
+
const padding = badge.padding;
|
|
153
|
+
const base = overlay.clone();
|
|
154
|
+
if (badge.opacity < 1) {
|
|
155
|
+
base.opacity(badge.opacity);
|
|
156
|
+
}
|
|
157
|
+
if (badge.type === "banner") {
|
|
158
|
+
const bannerHeight = isAdaptiveIcon ? 310 : 180;
|
|
159
|
+
const textContainer2 = new import_jimp.default(overlay.bitmap.width, bannerHeight, "transparent");
|
|
160
|
+
const alignY = isAdaptiveIcon ? badge.position === "top" ? import_jimp.default.VERTICAL_ALIGN_BOTTOM : import_jimp.default.VERTICAL_ALIGN_TOP : import_jimp.default.VERTICAL_ALIGN_MIDDLE;
|
|
161
|
+
textContainer2.print(
|
|
162
|
+
font,
|
|
163
|
+
padding,
|
|
164
|
+
0,
|
|
165
|
+
{ text, alignmentX: import_jimp.default.HORIZONTAL_ALIGN_CENTER, alignmentY: alignY },
|
|
166
|
+
overlay.bitmap.width - padding * 2,
|
|
167
|
+
bannerHeight
|
|
168
|
+
);
|
|
169
|
+
const top = badge.position === "top";
|
|
170
|
+
base.flip(false, top);
|
|
171
|
+
base.composite(textContainer2, 0, top ? 0 : base.bitmap.height - bannerHeight);
|
|
172
|
+
return base;
|
|
173
|
+
}
|
|
174
|
+
const ribbonHeight = isAdaptiveIcon ? 100 : 180;
|
|
175
|
+
const translateBy = (isAdaptiveIcon ? 80 : 270) - padding;
|
|
176
|
+
const textContainer = new import_jimp.default(overlay.bitmap.width, ribbonHeight, "transparent");
|
|
177
|
+
textContainer.print(
|
|
178
|
+
font,
|
|
179
|
+
0,
|
|
180
|
+
0,
|
|
181
|
+
{ text, alignmentX: import_jimp.default.HORIZONTAL_ALIGN_CENTER, alignmentY: import_jimp.default.VERTICAL_ALIGN_MIDDLE },
|
|
182
|
+
overlay.bitmap.width,
|
|
183
|
+
ribbonHeight
|
|
184
|
+
);
|
|
185
|
+
const left = badge.position === "left";
|
|
186
|
+
textContainer.rotate(left ? 45 : -45);
|
|
187
|
+
const translateX = left ? -translateBy : translateBy;
|
|
188
|
+
const textX = left ? translateX : overlay.bitmap.width - textContainer.bitmap.width + translateX;
|
|
189
|
+
const textY = left ? translateX : -translateX;
|
|
190
|
+
base.flip(left, false);
|
|
191
|
+
base.composite(textContainer, textX, textY);
|
|
192
|
+
return base;
|
|
193
|
+
}
|
|
194
|
+
function applyAdaptiveSafeZone(badge, safeZoneScale) {
|
|
195
|
+
const canvas = new import_jimp.default(badge.bitmap.width, badge.bitmap.height, "transparent");
|
|
196
|
+
const scaled = badge.resize(
|
|
197
|
+
Math.round(badge.bitmap.width * safeZoneScale),
|
|
198
|
+
Math.round(badge.bitmap.height * safeZoneScale)
|
|
199
|
+
);
|
|
200
|
+
const x = Math.round((canvas.bitmap.width - scaled.bitmap.width) / 2);
|
|
201
|
+
const y = Math.round((canvas.bitmap.height - scaled.bitmap.height) / 2);
|
|
202
|
+
canvas.composite(scaled, x, y);
|
|
203
|
+
return canvas;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/index.ts
|
|
207
|
+
function getResultPath(icon) {
|
|
208
|
+
const parts = icon.split(".");
|
|
209
|
+
parts.splice(parts.length - 1, 0, "result");
|
|
210
|
+
return parts.join(".");
|
|
211
|
+
}
|
|
212
|
+
function logDebug(debug, payload) {
|
|
213
|
+
if (!debug) return;
|
|
214
|
+
process.stdout.write(`[app-icon-badge-next] ${JSON.stringify(payload)}
|
|
215
|
+
`);
|
|
216
|
+
}
|
|
217
|
+
async function addBadge(input) {
|
|
218
|
+
const parsed = addBadgeParamsSchema.parse(input);
|
|
219
|
+
const sourcePath = import_node_path4.default.resolve(parsed.icon);
|
|
220
|
+
if (!import_node_fs3.default.existsSync(sourcePath)) {
|
|
221
|
+
throw new Error(`Icon does not exist: ${sourcePath}`);
|
|
222
|
+
}
|
|
223
|
+
const destination = import_node_path4.default.resolve(parsed.dstPath ?? getResultPath(parsed.icon));
|
|
224
|
+
logDebug(parsed.debug, {
|
|
225
|
+
icon: sourcePath,
|
|
226
|
+
dstPath: destination,
|
|
227
|
+
isAdaptiveIcon: parsed.isAdaptiveIcon,
|
|
228
|
+
adaptiveSafeMode: parsed.adaptiveSafeMode,
|
|
229
|
+
safeZoneScale: parsed.safeZoneScale,
|
|
230
|
+
badgeCount: parsed.badges.length
|
|
231
|
+
});
|
|
232
|
+
if (parsed.dryRun) {
|
|
233
|
+
return destination;
|
|
234
|
+
}
|
|
235
|
+
const image = await import_jimp2.default.read(sourcePath);
|
|
236
|
+
for (const badge of parsed.badges) {
|
|
237
|
+
const badgeImage = await createBadgeImage(badge, parsed.isAdaptiveIcon);
|
|
238
|
+
const finalBadge = parsed.isAdaptiveIcon && parsed.adaptiveSafeMode ? applyAdaptiveSafeZone(badgeImage, parsed.safeZoneScale) : badgeImage;
|
|
239
|
+
image.composite(finalBadge, 0, 0);
|
|
240
|
+
}
|
|
241
|
+
import_node_fs3.default.mkdirSync(import_node_path4.default.dirname(destination), { recursive: true });
|
|
242
|
+
await image.writeAsync(destination);
|
|
243
|
+
return destination;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/core/cache.ts
|
|
247
|
+
var import_node_fs4 = __toESM(require("fs"));
|
|
248
|
+
var import_node_path5 = __toESM(require("path"));
|
|
249
|
+
var EMPTY = { version: "1", entries: {} };
|
|
250
|
+
function readCache(projectRoot) {
|
|
251
|
+
const cachePath = import_node_path5.default.join(projectRoot, ".expo", "app-icon-badge", "cache.json");
|
|
252
|
+
try {
|
|
253
|
+
return JSON.parse(import_node_fs4.default.readFileSync(cachePath, "utf8"));
|
|
254
|
+
} catch {
|
|
255
|
+
return EMPTY;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function writeCache(projectRoot, manifest) {
|
|
259
|
+
const cachePath = import_node_path5.default.join(projectRoot, ".expo", "app-icon-badge", "cache.json");
|
|
260
|
+
ensureDir(import_node_path5.default.dirname(cachePath));
|
|
261
|
+
import_node_fs4.default.writeFileSync(cachePath, JSON.stringify(manifest, null, 2));
|
|
262
|
+
}
|
|
263
|
+
function setCacheEntry(manifest, destination, hash) {
|
|
264
|
+
return {
|
|
265
|
+
...manifest,
|
|
266
|
+
entries: {
|
|
267
|
+
...manifest.entries,
|
|
268
|
+
[destination]: hash
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// src/plugin/generate.ts
|
|
274
|
+
async function main() {
|
|
275
|
+
const payload = JSON.parse(import_node_fs5.default.readFileSync(process.argv[2], "utf8"));
|
|
276
|
+
let manifest = readCache(payload.projectRoot);
|
|
277
|
+
for (const job of payload.jobs) {
|
|
278
|
+
await addBadge({
|
|
279
|
+
icon: job.icon,
|
|
280
|
+
dstPath: job.dstPath,
|
|
281
|
+
badges: job.badges,
|
|
282
|
+
isAdaptiveIcon: job.isAdaptiveIcon,
|
|
283
|
+
adaptiveSafeMode: payload.adaptiveSafeMode,
|
|
284
|
+
safeZoneScale: payload.safeZoneScale,
|
|
285
|
+
debug: payload.debug
|
|
286
|
+
});
|
|
287
|
+
manifest = setCacheEntry(manifest, job.destination, job.hash);
|
|
288
|
+
}
|
|
289
|
+
if (payload.cache) {
|
|
290
|
+
writeCache(payload.projectRoot, manifest);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
main().catch((error) => {
|
|
294
|
+
console.error(
|
|
295
|
+
`[app-icon-badge-next] ${error instanceof Error ? error.message : String(error)}`
|
|
296
|
+
);
|
|
297
|
+
process.exit(1);
|
|
298
|
+
});
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
type BadgeTextColor = 'white' | 'black';
|
|
2
|
+
type BannerBadge = {
|
|
3
|
+
type: 'banner';
|
|
4
|
+
text: string;
|
|
5
|
+
position?: 'top' | 'bottom';
|
|
6
|
+
color?: BadgeTextColor;
|
|
7
|
+
background?: string;
|
|
8
|
+
fontScale?: number;
|
|
9
|
+
letterSpacing?: number;
|
|
10
|
+
padding?: number;
|
|
11
|
+
opacity?: number;
|
|
12
|
+
};
|
|
13
|
+
type RibbonBadge = {
|
|
14
|
+
type: 'ribbon';
|
|
15
|
+
text: string;
|
|
16
|
+
position?: 'left' | 'right';
|
|
17
|
+
color?: BadgeTextColor;
|
|
18
|
+
background?: string;
|
|
19
|
+
fontScale?: number;
|
|
20
|
+
letterSpacing?: number;
|
|
21
|
+
padding?: number;
|
|
22
|
+
opacity?: number;
|
|
23
|
+
};
|
|
24
|
+
type Badge = BannerBadge | RibbonBadge;
|
|
25
|
+
type PlatformBadgeConfig = {
|
|
26
|
+
badges?: Badge[];
|
|
27
|
+
};
|
|
28
|
+
type AppIconBadgeConfig = {
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
badges?: Badge[];
|
|
31
|
+
ios?: PlatformBadgeConfig;
|
|
32
|
+
android?: PlatformBadgeConfig & {
|
|
33
|
+
safeZoneScale?: number;
|
|
34
|
+
};
|
|
35
|
+
adaptiveSafeMode?: boolean;
|
|
36
|
+
cache?: boolean;
|
|
37
|
+
debug?: boolean;
|
|
38
|
+
dryRun?: boolean;
|
|
39
|
+
};
|
|
40
|
+
type AddBadgeParams = {
|
|
41
|
+
icon: string;
|
|
42
|
+
dstPath?: string;
|
|
43
|
+
badges: Badge[];
|
|
44
|
+
isAdaptiveIcon?: boolean;
|
|
45
|
+
adaptiveSafeMode?: boolean;
|
|
46
|
+
safeZoneScale?: number;
|
|
47
|
+
debug?: boolean;
|
|
48
|
+
dryRun?: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
declare function addBadge(input: AddBadgeParams): Promise<string>;
|
|
52
|
+
|
|
53
|
+
export { type AddBadgeParams, type AppIconBadgeConfig, type Badge, type BannerBadge, type RibbonBadge, addBadge };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
type BadgeTextColor = 'white' | 'black';
|
|
2
|
+
type BannerBadge = {
|
|
3
|
+
type: 'banner';
|
|
4
|
+
text: string;
|
|
5
|
+
position?: 'top' | 'bottom';
|
|
6
|
+
color?: BadgeTextColor;
|
|
7
|
+
background?: string;
|
|
8
|
+
fontScale?: number;
|
|
9
|
+
letterSpacing?: number;
|
|
10
|
+
padding?: number;
|
|
11
|
+
opacity?: number;
|
|
12
|
+
};
|
|
13
|
+
type RibbonBadge = {
|
|
14
|
+
type: 'ribbon';
|
|
15
|
+
text: string;
|
|
16
|
+
position?: 'left' | 'right';
|
|
17
|
+
color?: BadgeTextColor;
|
|
18
|
+
background?: string;
|
|
19
|
+
fontScale?: number;
|
|
20
|
+
letterSpacing?: number;
|
|
21
|
+
padding?: number;
|
|
22
|
+
opacity?: number;
|
|
23
|
+
};
|
|
24
|
+
type Badge = BannerBadge | RibbonBadge;
|
|
25
|
+
type PlatformBadgeConfig = {
|
|
26
|
+
badges?: Badge[];
|
|
27
|
+
};
|
|
28
|
+
type AppIconBadgeConfig = {
|
|
29
|
+
enabled?: boolean;
|
|
30
|
+
badges?: Badge[];
|
|
31
|
+
ios?: PlatformBadgeConfig;
|
|
32
|
+
android?: PlatformBadgeConfig & {
|
|
33
|
+
safeZoneScale?: number;
|
|
34
|
+
};
|
|
35
|
+
adaptiveSafeMode?: boolean;
|
|
36
|
+
cache?: boolean;
|
|
37
|
+
debug?: boolean;
|
|
38
|
+
dryRun?: boolean;
|
|
39
|
+
};
|
|
40
|
+
type AddBadgeParams = {
|
|
41
|
+
icon: string;
|
|
42
|
+
dstPath?: string;
|
|
43
|
+
badges: Badge[];
|
|
44
|
+
isAdaptiveIcon?: boolean;
|
|
45
|
+
adaptiveSafeMode?: boolean;
|
|
46
|
+
safeZoneScale?: number;
|
|
47
|
+
debug?: boolean;
|
|
48
|
+
dryRun?: boolean;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
declare function addBadge(input: AddBadgeParams): Promise<string>;
|
|
52
|
+
|
|
53
|
+
export { type AddBadgeParams, type AppIconBadgeConfig, type Badge, type BannerBadge, type RibbonBadge, addBadge };
|