@pmate/cli 0.21.19 → 0.23.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/dist/index.d.ts CHANGED
@@ -738,6 +738,7 @@ type AppCommandArgs = {
738
738
  type: Command.App;
739
739
  action: "icon";
740
740
  id: string;
741
+ subAction?: "export";
741
742
  preset?: "android" | "ios";
742
743
  width?: number;
743
744
  height?: number;
@@ -746,6 +747,19 @@ type AppCommandArgs = {
746
747
  outDir?: string;
747
748
  baseUrl?: string;
748
749
  json: boolean;
750
+ } | {
751
+ type: Command.App;
752
+ action: "icon-add";
753
+ id: string;
754
+ name: string;
755
+ filePath?: string;
756
+ width?: number;
757
+ height?: number;
758
+ format?: "png" | "webp" | "svg" | "ico";
759
+ output?: string;
760
+ force: boolean;
761
+ baseUrl?: string;
762
+ json: boolean;
749
763
  };
750
764
  type ServiceCommandArgs = {
751
765
  type: Command.Service;
package/dist/index.js CHANGED
@@ -17252,6 +17252,9 @@ var IOS_ICON_SPECS = [
17252
17252
  { width: 1024, height: 1024, name: "app-store" }
17253
17253
  ];
17254
17254
  var pngToIco = require("png-to-ico").default;
17255
+ var APP_ICON_VARIANT_KEY_PATTERN = /^[a-z0-9]+$/;
17256
+ var DATA_URL_PATTERN = /^data:([^;,]+);base64,([a-z0-9+/=]+)$/i;
17257
+ var DEFAULT_APP_ICON_VARIANT_NAME = "webp512x512";
17255
17258
  function normalizeAppIcon(icon, cwd) {
17256
17259
  if (typeof icon !== "string" || !icon.trim()) {
17257
17260
  throw new Error("App icon must be a non-empty http(s) URL.");
@@ -17268,7 +17271,7 @@ function normalizeAppIcon(icon, cwd) {
17268
17271
  }
17269
17272
  throw new Error("App icon must be an http(s) URL. Upload SVG files to OSS first and use the public link.");
17270
17273
  }
17271
- function prepareAppPayload(payload, cwd) {
17274
+ async function prepareAppPayload(payload, cwd) {
17272
17275
  if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
17273
17276
  throw new Error("App payload must be a JSON object.");
17274
17277
  }
@@ -17276,8 +17279,53 @@ function prepareAppPayload(payload, cwd) {
17276
17279
  if (normalized.icon !== void 0) {
17277
17280
  normalized.icon = normalizeAppIcon(normalized.icon, cwd);
17278
17281
  }
17282
+ if (normalized.icons !== void 0) {
17283
+ normalized.icons = normalizeAppIconVariants(normalized.icons);
17284
+ }
17285
+ if (typeof normalized.icon === "string" && shouldAutoGenerateDefaultVariant(normalized.icon)) {
17286
+ normalized.icons = await withDefaultSvgIconVariant(
17287
+ normalized.icon,
17288
+ normalized.icons,
17289
+ cwd
17290
+ );
17291
+ }
17292
+ return normalized;
17293
+ }
17294
+ function normalizeAppIconVariants(input) {
17295
+ if (!input || typeof input !== "object" || Array.isArray(input)) {
17296
+ throw new Error("App icons must be an object map of variant names to asset references.");
17297
+ }
17298
+ const entries = Object.entries(input);
17299
+ const normalized = Object.fromEntries(
17300
+ entries.map(([key, value]) => [validateAppIconVariantKey(key), normalizeAppIconVariantValue(value)])
17301
+ );
17279
17302
  return normalized;
17280
17303
  }
17304
+ function validateAppIconVariantKey(key) {
17305
+ const trimmed = key.trim();
17306
+ if (!trimmed) {
17307
+ throw new Error("App icon variant name is required.");
17308
+ }
17309
+ if (!APP_ICON_VARIANT_KEY_PATTERN.test(trimmed)) {
17310
+ throw new Error(
17311
+ `Invalid app icon variant name "${key}". Use lowercase letters and digits only, for example webp512x512.`
17312
+ );
17313
+ }
17314
+ return trimmed;
17315
+ }
17316
+ function normalizeAppIconVariantValue(value) {
17317
+ if (typeof value !== "string" || !value.trim()) {
17318
+ throw new Error("App icon variant value must be a non-empty URL or image data URL.");
17319
+ }
17320
+ const trimmed = value.trim();
17321
+ if (/^https?:\/\//i.test(trimmed)) {
17322
+ return trimmed;
17323
+ }
17324
+ if (isSupportedImageDataUrl(trimmed)) {
17325
+ return trimmed;
17326
+ }
17327
+ throw new Error("App icon variant value must be an http(s) URL or a supported image data URL.");
17328
+ }
17281
17329
  function createAppIconExportPlans(input) {
17282
17330
  const { namespace, objectId } = parseNamespacedRef(input.id, {
17283
17331
  entityLabel: "app ref"
@@ -17324,6 +17372,30 @@ async function exportAppIcon(icon, plans) {
17324
17372
  (0, import_node_fs15.writeFileSync)(plan.outputPath, bytes);
17325
17373
  }
17326
17374
  }
17375
+ async function renderAppIconVariant(input) {
17376
+ const svg = await resolveAppIconSvg(input.icon);
17377
+ const bytes = await renderAppIcon(svg, input.width, input.height, input.format);
17378
+ let outputPath;
17379
+ if (input.output) {
17380
+ outputPath = import_node_path20.default.resolve(input.cwd, input.output);
17381
+ (0, import_node_fs15.mkdirSync)(import_node_path20.default.dirname(outputPath), { recursive: true });
17382
+ (0, import_node_fs15.writeFileSync)(outputPath, bytes);
17383
+ }
17384
+ return {
17385
+ value: createImageDataUrl(bytes, input.format),
17386
+ ...outputPath ? { outputPath } : {}
17387
+ };
17388
+ }
17389
+ function loadAppIconVariantFile(filePath, cwd) {
17390
+ const absolutePath = import_node_path20.default.resolve(cwd, filePath);
17391
+ const format = inferVariantFormatFromPath(absolutePath);
17392
+ const bytes = (0, import_node_fs15.readFileSync)(absolutePath);
17393
+ return {
17394
+ value: createImageDataUrl(bytes, format),
17395
+ format,
17396
+ absolutePath
17397
+ };
17398
+ }
17327
17399
  async function resolveAppIconSvg(icon) {
17328
17400
  const trimmed = icon.trim();
17329
17401
  if (looksLikeSvgMarkup(trimmed)) {
@@ -17366,6 +17438,68 @@ async function renderAppIcon(svg, width, height, format) {
17366
17438
  function looksLikeSvgMarkup(value) {
17367
17439
  return /<svg[\s>]/i.test(value);
17368
17440
  }
17441
+ function looksLikeSvgUrl(value) {
17442
+ try {
17443
+ const url = new URL(value);
17444
+ return url.pathname.toLowerCase().endsWith(".svg");
17445
+ } catch {
17446
+ return false;
17447
+ }
17448
+ }
17449
+ function shouldAutoGenerateDefaultVariant(icon) {
17450
+ return looksLikeSvgMarkup(icon) || looksLikeSvgUrl(icon);
17451
+ }
17452
+ function inferVariantFormatFromPath(filePath) {
17453
+ const ext = import_node_path20.default.extname(filePath).toLowerCase();
17454
+ switch (ext) {
17455
+ case ".png":
17456
+ return "png";
17457
+ case ".webp":
17458
+ return "webp";
17459
+ case ".svg":
17460
+ return "svg";
17461
+ case ".ico":
17462
+ return "ico";
17463
+ default:
17464
+ throw new Error(`Unsupported app icon variant file extension "${ext || "(none)"}". Use .png, .webp, .svg, or .ico.`);
17465
+ }
17466
+ }
17467
+ function createImageDataUrl(bytes, format) {
17468
+ return `data:${mimeTypeForIconFormat(format)};base64,${bytes.toString("base64")}`;
17469
+ }
17470
+ function mimeTypeForIconFormat(format) {
17471
+ switch (format) {
17472
+ case "png":
17473
+ return "image/png";
17474
+ case "webp":
17475
+ return "image/webp";
17476
+ case "svg":
17477
+ return "image/svg+xml";
17478
+ case "ico":
17479
+ return "image/x-icon";
17480
+ }
17481
+ }
17482
+ function isSupportedImageDataUrl(value) {
17483
+ const match = value.match(DATA_URL_PATTERN);
17484
+ if (!match) {
17485
+ return false;
17486
+ }
17487
+ const mimeType = match[1]?.toLowerCase();
17488
+ return mimeType === "image/png" || mimeType === "image/webp" || mimeType === "image/svg+xml" || mimeType === "image/x-icon";
17489
+ }
17490
+ async function withDefaultSvgIconVariant(icon, icons, cwd) {
17491
+ const variant = await renderAppIconVariant({
17492
+ icon,
17493
+ width: 512,
17494
+ height: 512,
17495
+ format: "webp",
17496
+ cwd
17497
+ });
17498
+ return {
17499
+ ...icons || {},
17500
+ [DEFAULT_APP_ICON_VARIANT_NAME]: variant.value
17501
+ };
17502
+ }
17369
17503
 
17370
17504
  // src/commands/AppCommandHandler.ts
17371
17505
  init_namespacedRef();
@@ -17399,11 +17533,15 @@ var AppCommandHandler = class {
17399
17533
  "GET",
17400
17534
  pathname
17401
17535
  );
17402
- this.renderJson(record, true);
17536
+ if (args.action === "view") {
17537
+ this.renderView(record);
17538
+ } else {
17539
+ this.renderJson(record, true);
17540
+ }
17403
17541
  return;
17404
17542
  }
17405
17543
  case "create": {
17406
- const payload = this.preparePayload(args.filePath);
17544
+ const payload = await this.preparePayload(args.filePath);
17407
17545
  await this.requireNamespace(payload, baseUrl);
17408
17546
  const record = await this.request(
17409
17547
  baseUrl,
@@ -17417,7 +17555,7 @@ var AppCommandHandler = class {
17417
17555
  return;
17418
17556
  }
17419
17557
  case "update": {
17420
- const payload = this.preparePayload(args.filePath);
17558
+ const payload = await this.preparePayload(args.filePath);
17421
17559
  const pathname = this.buildAppPath(args.id);
17422
17560
  const record = await this.request(
17423
17561
  baseUrl,
@@ -17463,6 +17601,46 @@ var AppCommandHandler = class {
17463
17601
  );
17464
17602
  return;
17465
17603
  }
17604
+ case "icon-add": {
17605
+ const pathname = this.buildAppPath(args.id);
17606
+ const record = await this.request(
17607
+ baseUrl,
17608
+ token,
17609
+ "GET",
17610
+ pathname
17611
+ );
17612
+ const name = validateAppIconVariantKey(args.name);
17613
+ const existingIcons = record.icons || {};
17614
+ if (existingIcons[name] && !args.force) {
17615
+ throw new Error(`App icon variant "${name}" already exists for ${args.id}. Re-run with --force to overwrite it.`);
17616
+ }
17617
+ const source = args.filePath ? loadAppIconVariantFile(args.filePath, process.cwd()) : await this.renderIconVariantFromApp(record, args);
17618
+ const updatedIcons = {
17619
+ ...existingIcons,
17620
+ [name]: source.value
17621
+ };
17622
+ const updatedRecord = await this.request(
17623
+ baseUrl,
17624
+ token,
17625
+ "PUT",
17626
+ pathname,
17627
+ {
17628
+ icons: updatedIcons
17629
+ }
17630
+ );
17631
+ ui.success(`Updated icon variant ${name} for ${args.id}`);
17632
+ this.renderJson(
17633
+ {
17634
+ id: args.id,
17635
+ name,
17636
+ ..."absolutePath" in source ? { filePath: source.absolutePath } : {},
17637
+ ..."outputPath" in source && source.outputPath ? { outputPath: source.outputPath } : {},
17638
+ icons: updatedRecord.icons || updatedIcons
17639
+ },
17640
+ args.json
17641
+ );
17642
+ return;
17643
+ }
17466
17644
  case "delete": {
17467
17645
  const pathname = this.buildAppPath(args.id);
17468
17646
  const record = await this.request(
@@ -17533,7 +17711,7 @@ var AppCommandHandler = class {
17533
17711
  throw new Error(`Invalid JSON in ${absolutePath}: ${message}`);
17534
17712
  }
17535
17713
  }
17536
- preparePayload(filePath) {
17714
+ async preparePayload(filePath) {
17537
17715
  const absolutePath = this.resolveFilePath(filePath);
17538
17716
  return prepareAppPayload(this.readJsonFile(filePath), import_node_path21.default.dirname(absolutePath));
17539
17717
  }
@@ -17593,12 +17771,53 @@ var AppCommandHandler = class {
17593
17771
  }
17594
17772
  ui.plain(JSON.stringify(value, null, 2));
17595
17773
  }
17774
+ renderView(record) {
17775
+ const appId = formatNamespacedDisplayId(record.namespace, record.id);
17776
+ ui.plain(`App: ${appId}`);
17777
+ ui.plain(`Name: ${record.name}`);
17778
+ ui.plain(`Status: ${record.status}`);
17779
+ if (record.icon) {
17780
+ ui.plain(`Icon: ${record.icon}`);
17781
+ }
17782
+ if (record.icons && Object.keys(record.icons).length > 0) {
17783
+ ui.plain("Icons:");
17784
+ for (const [name, value] of Object.entries(record.icons).sort(
17785
+ ([left], [right]) => left.localeCompare(right)
17786
+ )) {
17787
+ ui.plain(` ${name}: ${value}`);
17788
+ }
17789
+ }
17790
+ if (record.deploys && Object.keys(record.deploys).length > 0) {
17791
+ ui.plain("Deploys:");
17792
+ for (const [platform, serviceRef] of Object.entries(record.deploys)) {
17793
+ if (serviceRef) {
17794
+ ui.plain(` ${platform}: ${serviceRef}`);
17795
+ }
17796
+ }
17797
+ }
17798
+ }
17596
17799
  buildAppPath(appId) {
17597
17800
  const { namespace, objectId } = parseNamespacedRef(appId, {
17598
17801
  entityLabel: "app ref"
17599
17802
  });
17600
17803
  return `/apps/${encodeURIComponent(namespace)}/${encodeURIComponent(objectId)}`;
17601
17804
  }
17805
+ async renderIconVariantFromApp(record, args) {
17806
+ if (!record.icon) {
17807
+ throw new Error(`App icon is missing for ${args.id}`);
17808
+ }
17809
+ if (!args.width || !args.height) {
17810
+ throw new Error("App icon add export requires an explicit size.");
17811
+ }
17812
+ return renderAppIconVariant({
17813
+ icon: record.icon,
17814
+ width: args.width,
17815
+ height: args.height,
17816
+ format: args.format || "png",
17817
+ output: args.output,
17818
+ cwd: process.cwd()
17819
+ });
17820
+ }
17602
17821
  };
17603
17822
  function readNamespaceFromPayload(payload) {
17604
17823
  if (!payload || typeof payload !== "object") {
@@ -20699,6 +20918,97 @@ function parseAppArgs(tokens) {
20699
20918
  return { type: "app" /* App */, action, id, baseUrl, json };
20700
20919
  }
20701
20920
  if (action === "icon") {
20921
+ const nextToken = rest[0];
20922
+ if (nextToken === "add") {
20923
+ rest.shift();
20924
+ const rawRef2 = rest.shift();
20925
+ const name = rest.shift();
20926
+ if (!rawRef2 || rawRef2.startsWith("-") || !name || name.startsWith("-")) {
20927
+ throw new Error(
20928
+ "Usage: app icon add <ns:objectId> <name> (--file <path> | (--size <w>x<h>|--<w>x<h>) [--format <png|webp|svg|ico>] [--output <path>]) [--force] [--base-url <url>] [--json]"
20929
+ );
20930
+ }
20931
+ const id2 = parseNamespacedRef(rawRef2, {
20932
+ entityLabel: "app ref"
20933
+ }).fullId;
20934
+ let width2;
20935
+ let height2;
20936
+ let format2;
20937
+ let output2;
20938
+ let filePath;
20939
+ let force = false;
20940
+ while (rest.length > 0) {
20941
+ const token = rest.shift();
20942
+ const inlineSize = parseInlineSizeToken(token);
20943
+ if (inlineSize) {
20944
+ if (filePath || width2 || height2) {
20945
+ throw new Error("App icon add source already set. Use either --file or one explicit size.");
20946
+ }
20947
+ width2 = inlineSize.width;
20948
+ height2 = inlineSize.height;
20949
+ continue;
20950
+ }
20951
+ switch (token) {
20952
+ case "--file":
20953
+ if (filePath || width2 || height2) {
20954
+ throw new Error("App icon add source already set. Use either --file or one explicit size.");
20955
+ }
20956
+ filePath = requireValue5(rest, token);
20957
+ break;
20958
+ case "--size": {
20959
+ if (filePath || width2 || height2) {
20960
+ throw new Error("App icon add source already set. Use either --file or one explicit size.");
20961
+ }
20962
+ const size = parseSizeValue(requireValue5(rest, token));
20963
+ width2 = size.width;
20964
+ height2 = size.height;
20965
+ break;
20966
+ }
20967
+ case "--format": {
20968
+ const value = requireValue5(rest, token);
20969
+ if (value !== "png" && value !== "webp" && value !== "svg" && value !== "ico") {
20970
+ throw new Error(`Invalid app icon format "${value}". Use png, webp, svg, or ico.`);
20971
+ }
20972
+ format2 = value;
20973
+ break;
20974
+ }
20975
+ case "--output":
20976
+ output2 = requireValue5(rest, token);
20977
+ break;
20978
+ case "--force":
20979
+ force = true;
20980
+ break;
20981
+ case "--base-url":
20982
+ baseUrl = requireValue5(rest, token);
20983
+ break;
20984
+ case "--json":
20985
+ json = true;
20986
+ break;
20987
+ default:
20988
+ throw new Error(`Unknown app icon add option: ${token}`);
20989
+ }
20990
+ }
20991
+ if (!filePath && (!width2 || !height2)) {
20992
+ throw new Error("Missing app icon add source. Use --file <path>, --size <w>x<h>, or --<w>x<h>.");
20993
+ }
20994
+ if (filePath && format2) {
20995
+ throw new Error("App icon add with --file infers format from the file extension. Remove --format.");
20996
+ }
20997
+ return {
20998
+ type: "app" /* App */,
20999
+ action: "icon-add",
21000
+ id: id2,
21001
+ name,
21002
+ filePath,
21003
+ width: width2,
21004
+ height: height2,
21005
+ format: format2,
21006
+ output: output2,
21007
+ force,
21008
+ baseUrl,
21009
+ json
21010
+ };
21011
+ }
20702
21012
  const rawRef = rest.shift();
20703
21013
  if (!rawRef || rawRef.startsWith("-")) {
20704
21014
  throw new Error(
@@ -20789,6 +21099,7 @@ function parseAppArgs(tokens) {
20789
21099
  return {
20790
21100
  type: "app" /* App */,
20791
21101
  action,
21102
+ subAction: "export",
20792
21103
  id,
20793
21104
  preset,
20794
21105
  width,
@@ -22475,6 +22786,8 @@ var HELP_TEXT = [
22475
22786
  ` ${import_chalk4.default.gray(" icon <ns:objectId> --android --out-dir <path> [--base-url <url>] [--json]")}`,
22476
22787
  ` ${import_chalk4.default.gray(" icon <ns:objectId> --ios --out-dir <path> [--base-url <url>] [--json]")}`,
22477
22788
  ` ${import_chalk4.default.gray(" icon <ns:objectId> (--size <w>x<h>|--<w>x<h>) --output <path> [--format <png|webp|svg|ico>] [--base-url <url>] [--json]")}`,
22789
+ ` ${import_chalk4.default.gray(" icon add <ns:objectId> <name> --file <path> [--force] [--base-url <url>] [--json]")}`,
22790
+ ` ${import_chalk4.default.gray(" icon add <ns:objectId> <name> (--size <w>x<h>|--<w>x<h>) [--format <png|webp|svg|ico>] [--output <path>] [--force] [--base-url <url>] [--json]")}`,
22478
22791
  ` ${import_chalk4.default.gray(" delete <ns:objectId> [--base-url <url>] [--json]")}`,
22479
22792
  ` ${import_chalk4.default.gray(" default base URL: https://apps-api.pmate.chat")}`,
22480
22793
  ` ${import_chalk4.default.cyan("service")} Manage service registry entries via apps-api`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pmate/cli",
3
- "version": "0.21.19",
3
+ "version": "0.23.0",
4
4
  "description": "CLI utilities for the pMate project",
5
5
  "license": "MIT",
6
6
  "bin": {