@gmcb/cli 0.8.12 → 0.9.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/CHANGELOG.md CHANGED
@@ -3,6 +3,25 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [0.9.0](http://10.10.10.16/caoben/front-end/compare/@gmcb/cli@0.8.13...@gmcb/cli@0.9.0) (2026-10-29)
7
+
8
+
9
+ ### Features
10
+
11
+ * 增加splash命令,可配置app启动图 ([74f06b9](http://10.10.10.16/caoben/front-end/commits/74f06b98216ae9fa9f567abb96909ccb766f1240))
12
+
13
+
14
+
15
+
16
+
17
+ ## [0.8.13](http://10.10.10.16/caoben/front-end/compare/@gmcb/cli@0.8.12...@gmcb/cli@0.8.13) (2026-08-17)
18
+
19
+ **Note:** Version bump only for package @gmcb/cli
20
+
21
+
22
+
23
+
24
+
6
25
  ## [0.8.12](http://10.10.10.16/caoben/front-end/compare/@gmcb/cli@0.8.11...@gmcb/cli@0.8.12) (2026-08-17)
7
26
 
8
27
  **Note:** Version bump only for package @gmcb/cli
package/README.md CHANGED
@@ -19,6 +19,7 @@ npm i @gmcb/cli -g
19
19
  | `gmcb copy` | 复制资源到指定目录 |
20
20
  | `gmcb config` | 配置管理 |
21
21
  | `gmcb icon` | 生成应用图标(圆角 PNG / ICO) |
22
+ | `gmcb splash` | 按 LOGO 生成启动图(白底 + 可选 9-patch 边框) |
22
23
  | `gmcb skill sync` | 同步技能到本地项目 |
23
24
  | `gmcb skill list` | 列出当前项目所有技能 |
24
25
 
@@ -103,7 +104,8 @@ gmcb publish -p app --wgt -a
103
104
  |---|---|
104
105
  | `-s, --source <path>` | 源图片路径(必填,支持相对/绝对路径) |
105
106
  | `-r, --radius <number>` | 圆角半径,默认 16 |
106
- | `--size <number>` | 输出 PNG 尺寸(宽高相同),默认原尺寸 |
107
+ | `--size <number>` | 输出 PNG 尺寸(宽高相同),覆盖原图,默认原尺寸 |
108
+ | `--sizes <numbers>` | 输出 PNG 副本,可传一个或多个(逗号分隔),文件名为 原文件名_size |
107
109
  | `--ico` | 是否输出 ICO 文件 |
108
110
  | `-d, --dest <path>` | 输出目录,默认为当前目录 |
109
111
 
@@ -113,13 +115,58 @@ gmcb publish -p app --wgt -a
113
115
  # 生成带圆角的各尺寸 PNG
114
116
  gmcb icon --source logo.png
115
117
 
116
- # 输出 256x256 的圆角 PNG
118
+ # 覆盖原图为 256x256 的圆角 PNG
117
119
  gmcb icon --source logo.png --size 256
118
120
 
121
+ # 输出副本 logo_256.png,不覆盖原图
122
+ gmcb icon --source logo.png --sizes 256
123
+
124
+ # 一次输出多个尺寸副本 logo_64.png、logo_128.png、logo_256.png
125
+ gmcb icon --source logo.png --sizes 64,128,256
126
+
119
127
  # 生成 ICO 文件,圆角半径 24,输出到 out 目录
120
128
  gmcb icon --source logo.png --radius 24 --ico --dest out
121
129
  ```
122
130
 
131
+ ## `gmcb splash`
132
+
133
+ 按品牌 LOGO 生成启动图:默认白底、LOGO 水平居中,按画布比例计算 LOGO 尺寸,不依赖项目模板。适用于白标项目换品牌时生成/更新 `splash.9.png`。
134
+
135
+ | 选项 | 描述 |
136
+ |---|---|
137
+ | `-s, --source <path>` | 品牌 LOGO 图片路径(必填,支持 PNG/JPG/WebP) |
138
+ | `-d, --dest <dir>` | 输出目录,默认与源 LOGO 图片同目录 |
139
+ | `--name <name>` | 输出文件名(不含 `.png`),默认 `splash.9`;以 `.9` 结尾生成 9-patch,否则生成普通 PNG |
140
+ | `--width <px>` | 画布宽度,默认取输出目录已有启动图宽度,否则 1082 |
141
+ | `--height <px>` | 画布高度,默认取输出目录已有启动图高度,否则 1884 |
142
+ | `--left <px\|%>` | LOGO 左右边距(右=左,水平居中),支持 px 或百分比;默认按 1082 画布 421px 等比换算 |
143
+ | `--top <px\|%>` | LOGO 距画布顶部距离,支持 px 或百分比;默认按 1884 画布 451px 等比换算 |
144
+ | `--bg <color>` | 背景色,默认 `#ffffff` |
145
+
146
+ 规则说明:
147
+
148
+ - LOGO 尺寸不单独设置:可见宽度 = `画布宽 - 2 × left`,高度按 LOGO 内容宽高比自动计算,不拉伸。
149
+ - `--left 40%` 表示左右各留 40% 边距,LOGO 宽度为画布 20%。
150
+ - 输出名以 `.9` 结尾时生成 1px 透明边框与拉伸标记(与现有 `splash.9.png` 约定一致);不带 `.9` 则输出普通 PNG。
151
+ - 画布尺寸默认读取输出目录中已有的启动图(先目标文件,再 `splash.9.png`、`splash.png`)。
152
+ - LOGO 建议使用透明底或与 `--bg` 同色的背景;若 LOGO 自带不透明背景色且与 `--bg` 不一致,输出会保留 LOGO 自身背景(命令会给出提示)。
153
+
154
+ 示例:
155
+
156
+ ```bash
157
+ # uni-app 项目:直接生成/更新 static/splash.9.png
158
+ gmcb splash -s ./static/appIcon.png
159
+
160
+ # 生成普通 PNG(无 9-patch 边框)
161
+ gmcb splash -s ./logo.png --name splash
162
+
163
+ # 左右各留 40%,LOGO 宽度为画布 20%,距顶部 20%
164
+ gmcb splash -s ./logo.png --left 40% --top 20%
165
+
166
+ # 自定义画布与位置
167
+ gmcb splash -s ./logo.png --width 2050 --height 1538 --left 500 --top 200 --bg '#f7f7f7'
168
+ ```
169
+
123
170
  ## `gmcb copy`
124
171
 
125
172
  将资源从源目录复制到目标目录,支持 `{...}` 占位符自动解析:
@@ -16,11 +16,23 @@ async function default_1(opts) {
16
16
  const sourcePath = (0, path_1.resolve)(utils_1.COMMAND_PATH, opts.source);
17
17
  const destDir = (0, path_1.resolve)(utils_1.COMMAND_PATH, opts.dest ?? '.');
18
18
  const radius = parseInt(opts.radius ?? '16', 10);
19
+ if (opts.size && opts.sizes) {
20
+ utils_1.log.error('--size 与 --sizes 不能同时使用');
21
+ return;
22
+ }
19
23
  const size = opts.size ? parseInt(opts.size, 10) : undefined;
20
24
  if (opts.size && (!Number.isInteger(size) || size <= 0)) {
21
25
  utils_1.log.error('--size 必须为正整数');
22
26
  return;
23
27
  }
28
+ let sizeList;
29
+ if (opts.sizes) {
30
+ sizeList = opts.sizes.split(',').map((s) => parseInt(s.trim(), 10));
31
+ if (sizeList.some((s) => !Number.isInteger(s) || s <= 0)) {
32
+ utils_1.log.error('--sizes 必须为正整数,多个用英文逗号分隔');
33
+ return;
34
+ }
35
+ }
24
36
  if (!(0, fs_1.existsSync)(sourcePath)) {
25
37
  utils_1.log.error(`源文件不存在: ${sourcePath}`);
26
38
  return;
@@ -29,14 +41,7 @@ async function default_1(opts) {
29
41
  (0, fs_1.mkdirSync)(destDir, { recursive: true });
30
42
  }
31
43
  const nameWithoutExt = (0, path_1.basename)(sourcePath, (0, path_1.extname)(sourcePath));
32
- // 输出圆角 PNG,传 --size 时缩放到指定尺寸,否则保留原尺寸
33
- const roundedPng = await (0, utils_1.generateRoundedPng)(sourcePath, size, radius);
34
- const pngPath = (0, path_1.resolve)(destDir, `${nameWithoutExt}.png`);
35
- (0, fs_1.writeFileSync)(pngPath, roundedPng);
36
- const pngKb = (roundedPng.length / 1024).toFixed(1);
37
- const pngSizeText = size ? ` (${size}x${size})` : '';
38
- console.log(`圆角 PNG 生成完成: ${pngPath}${pngSizeText} (${pngKb} KB)`);
39
- // 传 --ico 时额外生成 ICO 文件
44
+ // 传 --ico 时先生成 ICO 文件,避免 --size 覆盖原图后读取到缩放后的源文件
40
45
  if (opts.ico) {
41
46
  const pngBuffers = await (0, utils_1.generateRoundedPngs)(sourcePath, utils_1.ICO_SIZES, radius);
42
47
  const icoBuffer = (0, utils_1.packIcoBuffer)(pngBuffers);
@@ -45,4 +50,22 @@ async function default_1(opts) {
45
50
  const icoKb = (icoBuffer.length / 1024).toFixed(1);
46
51
  console.log(`ICO 生成完成: ${icoPath} (${icoKb} KB, sizes: ${utils_1.ICO_SIZES.join(', ')})`);
47
52
  }
53
+ // 未传尺寸时输出原尺寸圆角 PNG;--size 缩放后覆盖原图;--sizes 按 原文件名_size 命名生成副本
54
+ if (sizeList) {
55
+ for (const s of sizeList) {
56
+ const roundedPng = await (0, utils_1.generateRoundedPng)(sourcePath, s, radius);
57
+ const pngPath = (0, path_1.resolve)(destDir, `${nameWithoutExt}_${s}.png`);
58
+ (0, fs_1.writeFileSync)(pngPath, roundedPng);
59
+ const pngKb = (roundedPng.length / 1024).toFixed(1);
60
+ console.log(`圆角 PNG 生成完成: ${pngPath} (${s}x${s}, ${pngKb} KB)`);
61
+ }
62
+ }
63
+ else {
64
+ const roundedPng = await (0, utils_1.generateRoundedPng)(sourcePath, size, radius);
65
+ const pngPath = (0, path_1.resolve)(destDir, `${nameWithoutExt}.png`);
66
+ (0, fs_1.writeFileSync)(pngPath, roundedPng);
67
+ const pngKb = (roundedPng.length / 1024).toFixed(1);
68
+ const pngSizeText = size ? ` (${size}x${size})` : '';
69
+ console.log(`圆角 PNG 生成完成: ${pngPath}${pngSizeText} (${pngKb} KB)`);
70
+ }
48
71
  }
@@ -0,0 +1 @@
1
+ export default function (...args: any[]): Promise<void>;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = default_1;
4
+ const path_1 = require("path");
5
+ const fs_1 = require("fs");
6
+ const fs_extra_1 = require("fs-extra");
7
+ const utils_1 = require("../utils");
8
+ async function default_1(...args) {
9
+ const opts = args[0];
10
+ try {
11
+ if (!opts.source) {
12
+ throw new Error('请使用 -s/--source 指定品牌 LOGO 图片路径');
13
+ }
14
+ const sourcePath = (0, path_1.resolve)(utils_1.COMMAND_PATH, opts.source);
15
+ if (!(0, fs_1.existsSync)(sourcePath)) {
16
+ throw new Error(`LOGO 图片不存在: ${sourcePath}`);
17
+ }
18
+ if ((0, fs_1.statSync)(sourcePath).isDirectory()) {
19
+ throw new Error(`-s/--source 需要是图片文件: ${sourcePath}`);
20
+ }
21
+ const { fileName, ninePatch } = (0, utils_1.normalizeSplashName)(opts.name ?? 'splash.9');
22
+ if (opts.dest && /\.png$/i.test(opts.dest.trim())) {
23
+ throw new Error('--dest 需要是输出目录(文件名请用 --name 指定)');
24
+ }
25
+ const destDir = (0, path_1.resolve)(utils_1.COMMAND_PATH, opts.dest ?? (0, path_1.dirname)(opts.source));
26
+ if ((0, fs_1.existsSync)(destDir) && (0, fs_1.statSync)(destDir).isFile()) {
27
+ throw new Error(`--dest 需要是目录: ${destDir}`);
28
+ }
29
+ (0, fs_extra_1.ensureDirSync)(destDir);
30
+ const destPath = (0, path_1.join)(destDir, fileName);
31
+ let referenceSize = null;
32
+ const referencePath = (0, utils_1.findReferenceSplash)(destDir, fileName);
33
+ if (referencePath) {
34
+ try {
35
+ referenceSize = await (0, utils_1.readImageSize)(referencePath);
36
+ console.log(`参考启动图:${referencePath}(${referenceSize.width}×${referenceSize.height})`);
37
+ }
38
+ catch (err) {
39
+ utils_1.log.warning(`读取参考启动图失败,使用默认尺寸:${err.message}`);
40
+ }
41
+ }
42
+ else {
43
+ console.log(`参考启动图:未找到,使用默认尺寸 ${utils_1.DEFAULT_SPLASH_WIDTH}×${utils_1.DEFAULT_SPLASH_HEIGHT}`);
44
+ }
45
+ const width = opts.width ? (0, utils_1.parsePositiveInt)(opts.width, '--width') : (referenceSize?.width ?? utils_1.DEFAULT_SPLASH_WIDTH);
46
+ const height = opts.height
47
+ ? (0, utils_1.parsePositiveInt)(opts.height, '--height')
48
+ : (referenceSize?.height ?? utils_1.DEFAULT_SPLASH_HEIGHT);
49
+ const left = opts.left ? (0, utils_1.parseLength)(opts.left, width, '--left') : (0, utils_1.defaultSplashLeft)(width);
50
+ const top = opts.top ? (0, utils_1.parseLength)(opts.top, height, '--top') : (0, utils_1.defaultSplashTop)(height);
51
+ const background = opts.bg ? (0, utils_1.parseHexColor)(opts.bg) : { r: 255, g: 255, b: 255, a: 255 };
52
+ const { buffer, info } = await (0, utils_1.generateSplash)({
53
+ logo: (0, fs_1.readFileSync)(sourcePath),
54
+ width,
55
+ height,
56
+ left,
57
+ top,
58
+ background,
59
+ ninePatch,
60
+ });
61
+ (0, fs_1.writeFileSync)(destPath, buffer);
62
+ console.log(`画布:${info.width}×${info.height},背景 ${info.background}`);
63
+ console.log(`LOGO:可见区 ${info.logoBox.width}×${info.logoBox.height},left=${info.logoBox.x}, top=${info.logoBox.y}`);
64
+ console.log(`输出:${destPath}(${(buffer.length / 1024).toFixed(1)} KB,${ninePatch ? '9-patch' : '普通 PNG'})`);
65
+ if (info.logoBackground && info.logoBackground.toLowerCase() !== info.background.toLowerCase()) {
66
+ utils_1.log.warning(`LOGO 自带不透明背景色 ${info.logoBackground},与画布背景 ${info.background} 不一致,输出会保留 LOGO 自身背景`);
67
+ }
68
+ utils_1.log.success('启动图生成完成');
69
+ }
70
+ catch (err) {
71
+ utils_1.log.error(err?.message || String(err));
72
+ }
73
+ }
package/lib/bin.js CHANGED
@@ -13,6 +13,7 @@ const publish_1 = __importDefault(require("./actions/publish"));
13
13
  const config_1 = __importDefault(require("./utils/config"));
14
14
  const copy_1 = __importDefault(require("./actions/copy"));
15
15
  const icon_1 = __importDefault(require("./actions/icon"));
16
+ const splash_1 = __importDefault(require("./actions/splash"));
16
17
  const skill_1 = require("./actions/skill");
17
18
  const commands_1 = require("./commands");
18
19
  const utils_1 = require("./utils");
@@ -104,9 +105,22 @@ program
104
105
  .option(...commands_1.source)
105
106
  .option(...commands_1.radius)
106
107
  .option(...commands_1.size)
108
+ .option(...commands_1.sizes)
107
109
  .option(...commands_1.ico)
108
110
  .option(...commands_1.dest)
109
111
  .action(icon_1.default);
112
+ program
113
+ .command('splash')
114
+ .description('按 LOGO 生成启动图(白底 + 可选 9-patch 边框)')
115
+ .option(...commands_1.source)
116
+ .option(...commands_1.dest)
117
+ .option(...commands_1.name)
118
+ .option(...commands_1.width)
119
+ .option(...commands_1.height)
120
+ .option(...commands_1.left)
121
+ .option(...commands_1.top)
122
+ .option(...commands_1.bg)
123
+ .action(splash_1.default);
110
124
  const skillCmd = program.command('skill').description('技能管理');
111
125
  skillCmd
112
126
  .command('sync')
@@ -28,8 +28,14 @@ export declare const latest: readonly ["--latest", "是否获取最新版本数
28
28
  export declare const restore: readonly ["--restore", "构建完成后是否还原远程写入的主项目文件"];
29
29
  export declare const nobuild: readonly ["--nobuild", "跳过构建步骤"];
30
30
  export declare const radius: readonly ["-r, --radius <number>", "圆角半径,默认16", "16"];
31
- export declare const size: readonly ["--size <number>", "输出PNG尺寸(宽高相同),默认原尺寸"];
31
+ export declare const size: readonly ["--size <number>", "输出PNG尺寸(宽高相同),覆盖原图,默认原尺寸"];
32
+ export declare const sizes: readonly ["--sizes <numbers>", "输出PNG副本(宽高相同),可传一个或多个用英文逗号分隔,文件名为 原文件名_size"];
32
33
  export declare const ico: readonly ["--ico", "是否输出ICO文件"];
34
+ export declare const width: readonly ["--width <number>", "宽度(px)"];
35
+ export declare const height: readonly ["--height <number>", "高度(px)"];
36
+ export declare const left: readonly ["--left <value>", "左边距,支持 px 或百分比(如 421、38.9%)"];
37
+ export declare const top: readonly ["--top <value>", "顶部距离,支持 px 或百分比(如 451、23.9%)"];
38
+ export declare const bg: readonly ["--bg <color>", "背景色(如 #ffffff)"];
33
39
  export declare const hbuilderxCli: readonly ["--cli.hbuilderx <cli-path>", "HbuilderX Cli路径"];
34
40
  export declare const weixinCli: readonly ["--cli.weixin <cli-path>", "微信开发者工具 Cli路径"];
35
41
  export declare const alipayCli: readonly ["--cli.alipay <cli-path>", "支付宝开发者工具 Cli路径"];
@@ -67,6 +73,12 @@ export interface Options {
67
73
  nobuild?: boolean;
68
74
  radius?: string;
69
75
  size?: string;
76
+ sizes?: string;
77
+ width?: string;
78
+ height?: string;
79
+ left?: string;
80
+ top?: string;
81
+ bg?: string;
70
82
  ico?: boolean;
71
83
  'skill.repo'?: string;
72
84
  'template.repo'?: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.templateRepo = exports.skillRepo = exports.cosSecretkey = exports.cosSecretid = exports.alipayCli = exports.weixinCli = exports.hbuilderxCli = exports.ico = exports.size = exports.radius = exports.nobuild = exports.restore = exports.latest = exports.pull = exports.skip = exports.apk = exports.res = exports.wgt = exports.pack = exports.custom = exports.zip = exports.linux = exports.mac = exports.win = exports.link = exports.path = exports.name = exports.clean = exports.ignore = exports.dest = exports.source = exports.branch = exports.env = exports.compiler = exports.project = exports.platform = exports.template = exports.version = void 0;
3
+ exports.templateRepo = exports.skillRepo = exports.cosSecretkey = exports.cosSecretid = exports.alipayCli = exports.weixinCli = exports.hbuilderxCli = exports.bg = exports.top = exports.left = exports.height = exports.width = exports.ico = exports.sizes = exports.size = exports.radius = exports.nobuild = exports.restore = exports.latest = exports.pull = exports.skip = exports.apk = exports.res = exports.wgt = exports.pack = exports.custom = exports.zip = exports.linux = exports.mac = exports.win = exports.link = exports.path = exports.name = exports.clean = exports.ignore = exports.dest = exports.source = exports.branch = exports.env = exports.compiler = exports.project = exports.platform = exports.template = exports.version = void 0;
4
4
  exports.version = ['-v, --version', '输出版本号'];
5
5
  exports.template = ['-t, --template <template-name>', '模版仓库名称'];
6
6
  exports.platform = ['-p, --platform <platform-name>', '编译平台,默认h5', 'h5'];
@@ -30,8 +30,17 @@ exports.latest = ['--latest', '是否获取最新版本数据'];
30
30
  exports.restore = ['--restore', '构建完成后是否还原远程写入的主项目文件'];
31
31
  exports.nobuild = ['--nobuild', '跳过构建步骤'];
32
32
  exports.radius = ['-r, --radius <number>', '圆角半径,默认16', '16'];
33
- exports.size = ['--size <number>', '输出PNG尺寸(宽高相同),默认原尺寸'];
33
+ exports.size = ['--size <number>', '输出PNG尺寸(宽高相同),覆盖原图,默认原尺寸'];
34
+ exports.sizes = [
35
+ '--sizes <numbers>',
36
+ '输出PNG副本(宽高相同),可传一个或多个用英文逗号分隔,文件名为 原文件名_size',
37
+ ];
34
38
  exports.ico = ['--ico', '是否输出ICO文件'];
39
+ exports.width = ['--width <number>', '宽度(px)'];
40
+ exports.height = ['--height <number>', '高度(px)'];
41
+ exports.left = ['--left <value>', '左边距,支持 px 或百分比(如 421、38.9%)'];
42
+ exports.top = ['--top <value>', '顶部距离,支持 px 或百分比(如 451、23.9%)'];
43
+ exports.bg = ['--bg <color>', '背景色(如 #ffffff)'];
35
44
  exports.hbuilderxCli = ['--cli.hbuilderx <cli-path>', 'HbuilderX Cli路径'];
36
45
  exports.weixinCli = ['--cli.weixin <cli-path>', '微信开发者工具 Cli路径'];
37
46
  exports.alipayCli = ['--cli.alipay <cli-path>', '支付宝开发者工具 Cli路径'];
@@ -2,4 +2,5 @@ export * from './const';
2
2
  export * from './function';
3
3
  export * from './config';
4
4
  export * from './png2ico';
5
+ export * from './splash';
5
6
  export * as log from './log';
@@ -31,4 +31,5 @@ __exportStar(require("./const"), exports);
31
31
  __exportStar(require("./function"), exports);
32
32
  __exportStar(require("./config"), exports);
33
33
  __exportStar(require("./png2ico"), exports);
34
+ __exportStar(require("./splash"), exports);
34
35
  exports.log = __importStar(require("./log"));
@@ -0,0 +1,99 @@
1
+ /** 启动图背景色(RGBA,0~255) */
2
+ export interface SplashRgba {
3
+ r: number;
4
+ g: number;
5
+ b: number;
6
+ a: number;
7
+ }
8
+ /** 像素矩形区域 */
9
+ export interface SplashBox {
10
+ x: number;
11
+ y: number;
12
+ width: number;
13
+ height: number;
14
+ }
15
+ export interface SplashGenerateOptions {
16
+ /** 品牌 LOGO 图片 Buffer */
17
+ logo: Buffer;
18
+ /** 画布宽度(含 .9 的 1px 边框) */
19
+ width: number;
20
+ /** 画布高度(含 .9 的 1px 边框) */
21
+ height: number;
22
+ /** LOGO 左右边距(右 = 左,水平居中) */
23
+ left: number;
24
+ /** LOGO 可见内容区距画布顶部距离 */
25
+ top: number;
26
+ /** 背景色 */
27
+ background: SplashRgba;
28
+ /** 是否生成 9-patch 边框 */
29
+ ninePatch: boolean;
30
+ }
31
+ export interface SplashGenerateInfo {
32
+ width: number;
33
+ height: number;
34
+ /** 背景色,如 #ffffff */
35
+ background: string;
36
+ ninePatch: boolean;
37
+ /** LOGO 可见内容区在输出图中的位置 */
38
+ logoBox: SplashBox;
39
+ /** LOGO 自身不透明背景色(透明底为 null) */
40
+ logoBackground: string | null;
41
+ logoScale: number;
42
+ /** LOGO 源图可见内容区 */
43
+ logoSource: SplashBox;
44
+ }
45
+ /** 默认画布尺寸,与现有启动图一致 */
46
+ export declare const DEFAULT_SPLASH_WIDTH = 1082;
47
+ export declare const DEFAULT_SPLASH_HEIGHT = 1884;
48
+ /** 现有设计稿(1082×1884)中 LOGO 的左右边距与顶部距离 */
49
+ export declare const DEFAULT_SPLASH_LEFT = 421;
50
+ export declare const DEFAULT_SPLASH_TOP = 451;
51
+ /** 解析正整数选项 */
52
+ export declare function parsePositiveInt(value: string, label: string): number;
53
+ /**
54
+ * 解析长度选项:支持像素整数与百分比(相对 base 换算)
55
+ * 例如:parseLength('421', 1082) => 421;parseLength('40%', 1082) => 433
56
+ */
57
+ export declare function parseLength(value: string, base: number, label: string): number;
58
+ /** 解析 #rgb / #rrggbb 颜色 */
59
+ export declare function parseHexColor(value: string): SplashRgba;
60
+ /** 颜色转 #rrggbb */
61
+ export declare function colorToHex(color: SplashRgba): string;
62
+ /**
63
+ * 解析输出文件名:
64
+ * - 默认 splash.9 => splash.9.png(9-patch)
65
+ * - splash => splash.png(普通 PNG)
66
+ * - 兼容传入 splash.9.png
67
+ */
68
+ export declare function normalizeSplashName(value?: string): {
69
+ fileName: string;
70
+ ninePatch: boolean;
71
+ };
72
+ /** 按现有设计稿比例换算默认左右边距 */
73
+ export declare function defaultSplashLeft(width: number): number;
74
+ /** 按现有设计稿比例换算默认顶部距离 */
75
+ export declare function defaultSplashTop(height: number): number;
76
+ /** 查找输出目录中已有的启动图(依次:目标文件、splash.9.png、splash.png) */
77
+ export declare function findReferenceSplash(destDir: string, fileName: string): string | null;
78
+ /** 读取图片尺寸 */
79
+ export declare function readImageSize(input: string | Buffer): Promise<{
80
+ width: number;
81
+ height: number;
82
+ }>;
83
+ /**
84
+ * 从 RGBA 原始像素中识别可见内容区(排除透明与白底留白)
85
+ * @returns 内容包围盒;无内容返回 null
86
+ */
87
+ export declare function detectContentBox(data: Buffer, width: number, height: number): SplashBox | null;
88
+ /** 识别 LOGO 可见内容区 */
89
+ export declare function detectLogoContentBox(logo: Buffer): Promise<SplashBox>;
90
+ /**
91
+ * 按 LOGO 生成启动图
92
+ * - LOGO 可见宽度 = 画布宽 - 2 × left,高度按 LOGO 内容宽高比等比计算
93
+ * - 水平居中(右间距 = 左间距),顶部距离为 top
94
+ * - ninePatch 为 true 时生成与现有 splash.9.png 一致的边框与拉伸标记
95
+ */
96
+ export declare function generateSplash(options: SplashGenerateOptions): Promise<{
97
+ buffer: Buffer;
98
+ info: SplashGenerateInfo;
99
+ }>;
@@ -0,0 +1,522 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DEFAULT_SPLASH_TOP = exports.DEFAULT_SPLASH_LEFT = exports.DEFAULT_SPLASH_HEIGHT = exports.DEFAULT_SPLASH_WIDTH = void 0;
7
+ exports.parsePositiveInt = parsePositiveInt;
8
+ exports.parseLength = parseLength;
9
+ exports.parseHexColor = parseHexColor;
10
+ exports.colorToHex = colorToHex;
11
+ exports.normalizeSplashName = normalizeSplashName;
12
+ exports.defaultSplashLeft = defaultSplashLeft;
13
+ exports.defaultSplashTop = defaultSplashTop;
14
+ exports.findReferenceSplash = findReferenceSplash;
15
+ exports.readImageSize = readImageSize;
16
+ exports.detectContentBox = detectContentBox;
17
+ exports.detectLogoContentBox = detectLogoContentBox;
18
+ exports.generateSplash = generateSplash;
19
+ const fs_1 = require("fs");
20
+ const path_1 = require("path");
21
+ const sharp_1 = __importDefault(require("sharp"));
22
+ /** 默认画布尺寸,与现有启动图一致 */
23
+ exports.DEFAULT_SPLASH_WIDTH = 1082;
24
+ exports.DEFAULT_SPLASH_HEIGHT = 1884;
25
+ /** 现有设计稿(1082×1884)中 LOGO 的左右边距与顶部距离 */
26
+ exports.DEFAULT_SPLASH_LEFT = 421;
27
+ exports.DEFAULT_SPLASH_TOP = 451;
28
+ const ALPHA_OPAQUE = 200;
29
+ const INK_DISTANCE = 10;
30
+ const MIN_NINE_PATCH_SIZE = 8;
31
+ const VALIDATE_TOLERANCE = 2;
32
+ const PNG_COMPRESSION_LEVEL = 9;
33
+ /** 解析正整数选项 */
34
+ function parsePositiveInt(value, label) {
35
+ const text = String(value).trim();
36
+ if (!/^\d+$/.test(text)) {
37
+ throw new Error(`${label} 必须为正整数`);
38
+ }
39
+ const num = parseInt(text, 10);
40
+ if (!Number.isFinite(num) || num <= 0) {
41
+ throw new Error(`${label} 必须为正整数`);
42
+ }
43
+ return num;
44
+ }
45
+ /**
46
+ * 解析长度选项:支持像素整数与百分比(相对 base 换算)
47
+ * 例如:parseLength('421', 1082) => 421;parseLength('40%', 1082) => 433
48
+ */
49
+ function parseLength(value, base, label) {
50
+ const text = String(value).trim();
51
+ const percentMatch = text.match(/^(\d+(?:\.\d+)?)%$/);
52
+ if (percentMatch) {
53
+ const percent = parseFloat(percentMatch[1]);
54
+ if (!(percent >= 0) || percent >= 100) {
55
+ throw new Error(`${label} 百分比需在 0% ~ 100% 之间`);
56
+ }
57
+ return Math.round((base * percent) / 100);
58
+ }
59
+ if (/^\d+$/.test(text)) {
60
+ return parseInt(text, 10);
61
+ }
62
+ throw new Error(`${label} 格式无效,请使用像素整数(如 421)或百分比(如 38.9%)`);
63
+ }
64
+ /** 解析 #rgb / #rrggbb 颜色 */
65
+ function parseHexColor(value) {
66
+ const text = String(value).trim().replace(/^#/, '');
67
+ let hex = text;
68
+ if (/^[0-9a-f]{3}$/i.test(hex)) {
69
+ hex = hex
70
+ .split('')
71
+ .map((char) => char + char)
72
+ .join('');
73
+ }
74
+ if (!/^[0-9a-f]{6}$/i.test(hex)) {
75
+ throw new Error(`--bg 颜色格式无效:${value},请使用 #rgb 或 #rrggbb`);
76
+ }
77
+ const num = parseInt(hex, 16);
78
+ return {
79
+ r: (num >> 16) & 0xff,
80
+ g: (num >> 8) & 0xff,
81
+ b: num & 0xff,
82
+ a: 255,
83
+ };
84
+ }
85
+ /** 颜色转 #rrggbb */
86
+ function colorToHex(color) {
87
+ const hex = [color.r, color.g, color.b]
88
+ .map((channel) => Math.max(0, Math.min(255, Math.round(channel)))
89
+ .toString(16)
90
+ .padStart(2, '0'))
91
+ .join('');
92
+ return `#${hex}`;
93
+ }
94
+ /**
95
+ * 解析输出文件名:
96
+ * - 默认 splash.9 => splash.9.png(9-patch)
97
+ * - splash => splash.png(普通 PNG)
98
+ * - 兼容传入 splash.9.png
99
+ */
100
+ function normalizeSplashName(value = 'splash.9') {
101
+ let name = String(value ?? '').trim();
102
+ if (/\.png$/i.test(name)) {
103
+ name = name.slice(0, -4);
104
+ }
105
+ if (!name) {
106
+ throw new Error('--name 不能为空');
107
+ }
108
+ if (/[\\/:*?"<>|]/.test(name)) {
109
+ throw new Error('--name 不能包含路径分隔符或非法字符');
110
+ }
111
+ const ninePatch = name.toLowerCase().endsWith('.9');
112
+ const base = ninePatch ? name.slice(0, -2) : name;
113
+ if (!base) {
114
+ throw new Error('--name 不能为空');
115
+ }
116
+ return { fileName: `${name}.png`, ninePatch };
117
+ }
118
+ /** 按现有设计稿比例换算默认左右边距 */
119
+ function defaultSplashLeft(width) {
120
+ return Math.round((width * exports.DEFAULT_SPLASH_LEFT) / exports.DEFAULT_SPLASH_WIDTH);
121
+ }
122
+ /** 按现有设计稿比例换算默认顶部距离 */
123
+ function defaultSplashTop(height) {
124
+ return Math.round((height * exports.DEFAULT_SPLASH_TOP) / exports.DEFAULT_SPLASH_HEIGHT);
125
+ }
126
+ /** 查找输出目录中已有的启动图(依次:目标文件、splash.9.png、splash.png) */
127
+ function findReferenceSplash(destDir, fileName) {
128
+ const candidates = [(0, path_1.join)(destDir, fileName), (0, path_1.join)(destDir, 'splash.9.png'), (0, path_1.join)(destDir, 'splash.png')];
129
+ const visited = new Set();
130
+ for (const candidate of candidates) {
131
+ if (visited.has(candidate))
132
+ continue;
133
+ visited.add(candidate);
134
+ if ((0, fs_1.existsSync)(candidate))
135
+ return candidate;
136
+ }
137
+ return null;
138
+ }
139
+ async function readRaw(input) {
140
+ const { data, info } = await (0, sharp_1.default)(input).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
141
+ return { data: Buffer.from(data), width: info.width, height: info.height };
142
+ }
143
+ /** 读取图片尺寸 */
144
+ async function readImageSize(input) {
145
+ const meta = await (0, sharp_1.default)(input).metadata();
146
+ if (!meta.width || !meta.height) {
147
+ throw new Error('无法读取图片尺寸');
148
+ }
149
+ return { width: meta.width, height: meta.height };
150
+ }
151
+ /**
152
+ * 从 RGBA 原始像素中识别可见内容区(排除透明与白底留白)
153
+ * @returns 内容包围盒;无内容返回 null
154
+ */
155
+ function detectContentBox(data, width, height) {
156
+ let minX = width;
157
+ let minY = height;
158
+ let maxX = -1;
159
+ let maxY = -1;
160
+ for (let y = 0; y < height; y++) {
161
+ for (let x = 0; x < width; x++) {
162
+ const idx = (y * width + x) * 4;
163
+ if (data[idx + 3] <= ALPHA_OPAQUE)
164
+ continue;
165
+ const distance = Math.abs(data[idx] - 255) + Math.abs(data[idx + 1] - 255) + Math.abs(data[idx + 2] - 255);
166
+ if (distance <= INK_DISTANCE)
167
+ continue;
168
+ if (x < minX)
169
+ minX = x;
170
+ if (x > maxX)
171
+ maxX = x;
172
+ if (y < minY)
173
+ minY = y;
174
+ if (y > maxY)
175
+ maxY = y;
176
+ }
177
+ }
178
+ if (maxX < 0 || maxY < 0)
179
+ return null;
180
+ return { x: minX, y: minY, width: maxX - minX + 1, height: maxY - minY + 1 };
181
+ }
182
+ /** 识别 LOGO 可见内容区 */
183
+ async function detectLogoContentBox(logo) {
184
+ const raw = await readRaw(logo);
185
+ const box = detectContentBox(raw.data, raw.width, raw.height);
186
+ if (!box) {
187
+ throw new Error('无法识别 LOGO 内容区域,请确认图片包含有效图形(非全透明或纯白)');
188
+ }
189
+ return box;
190
+ }
191
+ function compositeRaw(canvas, width, height, source, sourceWidth, sourceHeight, options) {
192
+ const { pasteX, pasteY, clipMinX, clipMinY, clipMaxX, clipMaxY } = options;
193
+ for (let y = 0; y < sourceHeight; y++) {
194
+ const targetY = pasteY + y;
195
+ if (targetY < clipMinY || targetY > clipMaxY)
196
+ continue;
197
+ for (let x = 0; x < sourceWidth; x++) {
198
+ const targetX = pasteX + x;
199
+ if (targetX < clipMinX || targetX > clipMaxX)
200
+ continue;
201
+ const sourceIndex = (y * sourceWidth + x) * 4;
202
+ const alpha = source[sourceIndex + 3];
203
+ if (alpha === 0)
204
+ continue;
205
+ const targetIndex = (targetY * width + targetX) * 4;
206
+ if (alpha === 255) {
207
+ canvas[targetIndex] = source[sourceIndex];
208
+ canvas[targetIndex + 1] = source[sourceIndex + 1];
209
+ canvas[targetIndex + 2] = source[sourceIndex + 2];
210
+ canvas[targetIndex + 3] = 255;
211
+ continue;
212
+ }
213
+ const ratio = alpha / 255;
214
+ canvas[targetIndex] = Math.round(source[sourceIndex] * ratio + canvas[targetIndex] * (1 - ratio));
215
+ canvas[targetIndex + 1] = Math.round(source[sourceIndex + 1] * ratio + canvas[targetIndex + 1] * (1 - ratio));
216
+ canvas[targetIndex + 2] = Math.round(source[sourceIndex + 2] * ratio + canvas[targetIndex + 2] * (1 - ratio));
217
+ canvas[targetIndex + 3] = 255;
218
+ }
219
+ }
220
+ }
221
+ /** 按现有 splash.9.png 约定绘制 9-patch 边框标记 */
222
+ function drawNinePatchBorder(canvas, width, height) {
223
+ const setPixel = (x, y, r, g, b, a) => {
224
+ const idx = (y * width + x) * 4;
225
+ canvas[idx] = r;
226
+ canvas[idx + 1] = g;
227
+ canvas[idx + 2] = b;
228
+ canvas[idx + 3] = a;
229
+ };
230
+ for (let x = 0; x < width; x++) {
231
+ setPixel(x, 0, 0, 0, 0, 0);
232
+ setPixel(x, height - 1, 0, 0, 0, 0);
233
+ }
234
+ for (let y = 0; y < height; y++) {
235
+ setPixel(0, y, 0, 0, 0, 0);
236
+ setPixel(width - 1, y, 0, 0, 0, 0);
237
+ }
238
+ setPixel(1, 0, 0, 0, 0, 255);
239
+ setPixel(width - 2, 0, 0, 0, 0, 255);
240
+ setPixel(0, 1, 0, 0, 0, 255);
241
+ setPixel(width - 1, height - 2, 0, 0, 0, 255);
242
+ }
243
+ function boxDistance(a, b) {
244
+ return Math.max(Math.abs(a.x - b.x), Math.abs(a.y - b.y), Math.abs(a.width - b.width), Math.abs(a.height - b.height));
245
+ }
246
+ function colorDistance(a, b) {
247
+ return Math.abs(a.r - b.r) + Math.abs(a.g - b.g) + Math.abs(a.b - b.b);
248
+ }
249
+ /**
250
+ * 识别 LOGO 自身的不透明背景色(内容区以外的众数颜色)
251
+ * - 内容区以外几乎全透明 => 视为透明底,返回 null
252
+ * - 内容区覆盖整张图 => 返回 null
253
+ */
254
+ function detectLogoBackgroundColor(data, width, height, content) {
255
+ const counts = new Map();
256
+ let outsideCount = 0;
257
+ let opaqueCount = 0;
258
+ for (let y = 0; y < height; y++) {
259
+ for (let x = 0; x < width; x++) {
260
+ const inContent = x >= content.x && x < content.x + content.width && y >= content.y && y < content.y + content.height;
261
+ if (inContent)
262
+ continue;
263
+ outsideCount += 1;
264
+ const idx = (y * width + x) * 4;
265
+ if (data[idx + 3] <= ALPHA_OPAQUE)
266
+ continue;
267
+ opaqueCount += 1;
268
+ const key = `${data[idx]},${data[idx + 1]},${data[idx + 2]}`;
269
+ const entry = counts.get(key);
270
+ if (entry) {
271
+ entry.count += 1;
272
+ }
273
+ else {
274
+ counts.set(key, { color: { r: data[idx], g: data[idx + 1], b: data[idx + 2], a: 255 }, count: 1 });
275
+ }
276
+ }
277
+ }
278
+ if (outsideCount === 0 || opaqueCount === 0)
279
+ return null;
280
+ // 内容区以外不透明像素占比很低时,视为透明底(仅边缘抗锯齿)
281
+ if (opaqueCount < outsideCount * 0.1)
282
+ return null;
283
+ let best = null;
284
+ for (const entry of counts.values()) {
285
+ if (!best || entry.count > best.count)
286
+ best = entry;
287
+ }
288
+ return best ? best.color : null;
289
+ }
290
+ function assertNinePatchBorder(data, width, height) {
291
+ const pixel = (x, y) => {
292
+ const idx = (y * width + x) * 4;
293
+ return [data[idx], data[idx + 1], data[idx + 2], data[idx + 3]];
294
+ };
295
+ const markers = [
296
+ [1, 0],
297
+ [width - 2, 0],
298
+ [0, 1],
299
+ [width - 1, height - 2],
300
+ ];
301
+ const markerKeys = new Set(markers.map(([x, y]) => `${x},${y}`));
302
+ let opaqueBorderCount = 0;
303
+ for (let x = 0; x < width; x++) {
304
+ for (const y of [0, height - 1]) {
305
+ const [r, g, b, a] = pixel(x, y);
306
+ if (a === 0 && r === 0 && g === 0 && b === 0)
307
+ continue;
308
+ if (a === 255 && r === 0 && g === 0 && b === 0 && markerKeys.has(`${x},${y}`))
309
+ continue;
310
+ throw new Error(`启动图生成结果校验失败:9-patch 边框像素异常 (${x}, ${y})`);
311
+ }
312
+ }
313
+ for (let y = 0; y < height; y++) {
314
+ for (const x of [0, width - 1]) {
315
+ const [r, g, b, a] = pixel(x, y);
316
+ if (a === 0 && r === 0 && g === 0 && b === 0)
317
+ continue;
318
+ if (a === 255 && r === 0 && g === 0 && b === 0 && markerKeys.has(`${x},${y}`))
319
+ continue;
320
+ throw new Error(`启动图生成结果校验失败:9-patch 边框像素异常 (${x}, ${y})`);
321
+ }
322
+ }
323
+ for (const [x, y] of markers) {
324
+ const [r, g, b, a] = pixel(x, y);
325
+ if (a !== 255 || r !== 0 || g !== 0 || b !== 0) {
326
+ throw new Error(`启动图生成结果校验失败:9-patch 拉伸标记缺失 (${x}, ${y})`);
327
+ }
328
+ opaqueBorderCount += 1;
329
+ }
330
+ if (opaqueBorderCount !== markers.length) {
331
+ throw new Error('启动图生成结果校验失败:9-patch 拉伸标记数量异常');
332
+ }
333
+ }
334
+ function findBoxByBackground(data, width, height, background, border) {
335
+ let minX = width;
336
+ let minY = height;
337
+ let maxX = -1;
338
+ let maxY = -1;
339
+ for (let y = border; y < height - border; y++) {
340
+ for (let x = border; x < width - border; x++) {
341
+ const idx = (y * width + x) * 4;
342
+ if (data[idx + 3] <= ALPHA_OPAQUE)
343
+ continue;
344
+ const distance = Math.abs(data[idx] - background.r) +
345
+ Math.abs(data[idx + 1] - background.g) +
346
+ Math.abs(data[idx + 2] - background.b);
347
+ if (distance <= INK_DISTANCE)
348
+ continue;
349
+ if (x < minX)
350
+ minX = x;
351
+ if (x > maxX)
352
+ maxX = x;
353
+ if (y < minY)
354
+ minY = y;
355
+ if (y > maxY)
356
+ maxY = y;
357
+ }
358
+ }
359
+ if (maxX < 0 || maxY < 0)
360
+ return null;
361
+ return { x: minX, y: minY, width: maxX - minX + 1, height: maxY - minY + 1 };
362
+ }
363
+ async function validateOutput(buffer, options, expected, tolerance, strictBox, pasteBox) {
364
+ const raw = await readRaw(buffer);
365
+ if (raw.width !== options.width || raw.height !== options.height) {
366
+ throw new Error('启动图生成结果校验失败:输出尺寸异常');
367
+ }
368
+ const border = options.ninePatch ? 1 : 0;
369
+ if (options.ninePatch) {
370
+ assertNinePatchBorder(raw.data, raw.width, raw.height);
371
+ }
372
+ // 期望区域内必须存在 LOGO 像素
373
+ let hasContent = false;
374
+ for (let y = expected.y; y < expected.y + expected.height && !hasContent; y++) {
375
+ for (let x = expected.x; x < expected.x + expected.width; x++) {
376
+ const idx = (y * raw.width + x) * 4;
377
+ if (raw.data[idx + 3] <= ALPHA_OPAQUE)
378
+ continue;
379
+ const distance = Math.abs(raw.data[idx] - options.background.r) +
380
+ Math.abs(raw.data[idx + 1] - options.background.g) +
381
+ Math.abs(raw.data[idx + 2] - options.background.b);
382
+ if (distance > INK_DISTANCE) {
383
+ hasContent = true;
384
+ break;
385
+ }
386
+ }
387
+ }
388
+ if (!hasContent) {
389
+ throw new Error('启动图生成结果校验失败:未找到 LOGO 内容');
390
+ }
391
+ // LOGO 背景可融入画布背景时,做严格的包围盒校验
392
+ if (strictBox) {
393
+ const box = findBoxByBackground(raw.data, raw.width, raw.height, options.background, border);
394
+ if (!box) {
395
+ throw new Error('启动图生成结果校验失败:未找到 LOGO 内容');
396
+ }
397
+ if (boxDistance(box, expected) > tolerance) {
398
+ throw new Error('启动图生成结果校验失败:LOGO 位置或尺寸异常');
399
+ }
400
+ }
401
+ // 未被 LOGO 画布覆盖的角落必须是背景色
402
+ const samplePoints = [
403
+ [border, border],
404
+ [options.width - 1 - border, border],
405
+ [border, options.height - 1 - border],
406
+ [options.width - 1 - border, options.height - 1 - border],
407
+ ];
408
+ for (const [x, y] of samplePoints) {
409
+ const inPaste = x >= pasteBox.x && x < pasteBox.x + pasteBox.width && y >= pasteBox.y && y < pasteBox.y + pasteBox.height;
410
+ if (inPaste)
411
+ continue;
412
+ const idx = (y * raw.width + x) * 4;
413
+ const isBackground = raw.data[idx] === options.background.r &&
414
+ raw.data[idx + 1] === options.background.g &&
415
+ raw.data[idx + 2] === options.background.b &&
416
+ raw.data[idx + 3] === 255;
417
+ if (!isBackground) {
418
+ throw new Error('启动图生成结果校验失败:背景色异常');
419
+ }
420
+ }
421
+ }
422
+ /**
423
+ * 按 LOGO 生成启动图
424
+ * - LOGO 可见宽度 = 画布宽 - 2 × left,高度按 LOGO 内容宽高比等比计算
425
+ * - 水平居中(右间距 = 左间距),顶部距离为 top
426
+ * - ninePatch 为 true 时生成与现有 splash.9.png 一致的边框与拉伸标记
427
+ */
428
+ async function generateSplash(options) {
429
+ const { logo, width, height, left, top, background, ninePatch } = options;
430
+ if (!Number.isInteger(width) || width <= 0) {
431
+ throw new Error('画布宽度必须为正整数');
432
+ }
433
+ if (!Number.isInteger(height) || height <= 0) {
434
+ throw new Error('画布高度必须为正整数');
435
+ }
436
+ if (!Number.isInteger(left) || left < 0) {
437
+ throw new Error('--left 必须为非负整数');
438
+ }
439
+ if (!Number.isInteger(top) || top < 0) {
440
+ throw new Error('--top 必须为非负整数');
441
+ }
442
+ if (ninePatch) {
443
+ if (width < MIN_NINE_PATCH_SIZE || height < MIN_NINE_PATCH_SIZE) {
444
+ throw new Error(`9-patch 启动图尺寸不能小于 ${MIN_NINE_PATCH_SIZE}×${MIN_NINE_PATCH_SIZE}`);
445
+ }
446
+ if (left < 1) {
447
+ throw new Error('9-patch 启动图的 --left 至少为 1px(需保留 1px 边框)');
448
+ }
449
+ if (top < 1) {
450
+ throw new Error('9-patch 启动图的 --top 至少为 1px(需保留 1px 边框)');
451
+ }
452
+ }
453
+ const logoWidth = width - 2 * left;
454
+ if (logoWidth < 1) {
455
+ throw new Error(`--left 过大:LOGO 宽度计算为 ${logoWidth}px,请减小 --left 或增大 --width`);
456
+ }
457
+ const logoSource = await detectLogoContentBox(logo);
458
+ const logoRaw = await readRaw(logo);
459
+ const logoBackground = detectLogoBackgroundColor(logoRaw.data, logoRaw.width, logoRaw.height, logoSource);
460
+ const logoScale = logoWidth / logoSource.width;
461
+ const logoHeight = Math.max(1, Math.round(logoSource.height * logoScale));
462
+ const border = ninePatch ? 1 : 0;
463
+ if (top + logoHeight > height - border) {
464
+ throw new Error(`LOGO 超出画布:top(${top}) + LOGO 高度(${logoHeight}) 超过画布可用高度(${height - border}),` +
465
+ '请调整 --top/--left/--height');
466
+ }
467
+ const canvas = Buffer.alloc(width * height * 4);
468
+ for (let i = 0; i < width * height; i++) {
469
+ const idx = i * 4;
470
+ canvas[idx] = background.r;
471
+ canvas[idx + 1] = background.g;
472
+ canvas[idx + 2] = background.b;
473
+ canvas[idx + 3] = 255;
474
+ }
475
+ const scaledWidth = Math.max(1, Math.round(logoRaw.width * logoScale));
476
+ const scaledHeight = Math.max(1, Math.round(logoRaw.height * logoScale));
477
+ const resized = await (0, sharp_1.default)(logo)
478
+ .ensureAlpha()
479
+ .resize(scaledWidth, scaledHeight, { fit: 'fill', kernel: 'lanczos3' })
480
+ .raw()
481
+ .toBuffer({ resolveWithObject: true });
482
+ const pasteX = Math.round(left - logoSource.x * logoScale);
483
+ const pasteY = Math.round(top - logoSource.y * logoScale);
484
+ compositeRaw(canvas, width, height, Buffer.from(resized.data), scaledWidth, scaledHeight, {
485
+ pasteX,
486
+ pasteY,
487
+ clipMinX: border,
488
+ clipMinY: border,
489
+ clipMaxX: width - 1 - border,
490
+ clipMaxY: height - 1 - border,
491
+ });
492
+ if (ninePatch) {
493
+ drawNinePatchBorder(canvas, width, height);
494
+ }
495
+ const buffer = await (0, sharp_1.default)(canvas, { raw: { width, height, channels: 4 } })
496
+ .png({ compressionLevel: PNG_COMPRESSION_LEVEL })
497
+ .toBuffer();
498
+ const expectedBox = { x: left, y: top, width: logoWidth, height: logoHeight };
499
+ // 缩放重采样会在内容边缘产生 1~2px 抗锯齿光晕,按缩放比例放宽容差
500
+ const validationTolerance = Math.max(VALIDATE_TOLERANCE, Math.ceil(logoScale * 2) + 1);
501
+ // LOGO 自身背景与画布背景接近(或为透明底)时,LOGO 背景不可见,可做严格包围盒校验
502
+ const strictBox = !logoBackground || colorDistance(logoBackground, background) <= INK_DISTANCE;
503
+ await validateOutput(buffer, options, expectedBox, validationTolerance, strictBox, {
504
+ x: pasteX,
505
+ y: pasteY,
506
+ width: scaledWidth,
507
+ height: scaledHeight,
508
+ });
509
+ return {
510
+ buffer,
511
+ info: {
512
+ width,
513
+ height,
514
+ background: colorToHex(background),
515
+ ninePatch,
516
+ logoBox: expectedBox,
517
+ logoBackground: logoBackground ? colorToHex(logoBackground) : null,
518
+ logoScale,
519
+ logoSource,
520
+ },
521
+ };
522
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gmcb/cli",
3
- "version": "0.8.12",
3
+ "version": "0.9.0",
4
4
  "description": "工茂草本前端命令行工具",
5
5
  "author": "yinjiazeng@163.com",
6
6
  "license": "MIT",
@@ -58,6 +58,5 @@
58
58
  "@types/inquirer": "^9.0.7",
59
59
  "@types/rimraf": "^4.0.5",
60
60
  "@types/user-home": "^2.0.2"
61
- },
62
- "gitHead": "26edf845af9daec654c55c8d793213862f3b4bef"
61
+ }
63
62
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 aniu
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.