faceless-cli 1.1.7 → 1.1.9

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/src/output.mjs CHANGED
@@ -1,109 +1,106 @@
1
1
  const PREFERRED_COLUMNS = [
2
- "id",
3
- "name",
4
- "platform",
5
- "channelName",
6
- "username",
7
- "status",
8
- "renderStatus",
9
- "model",
10
- "source",
11
- "niche",
12
- "type",
13
- "credits",
14
- "paused",
15
- "scheduledTime",
16
- "postedAt",
17
- "createdAt",
18
- "value",
19
- "label",
2
+ "id",
3
+ "name",
4
+ "platform",
5
+ "channelName",
6
+ "username",
7
+ "status",
8
+ "renderStatus",
9
+ "model",
10
+ "source",
11
+ "niche",
12
+ "type",
13
+ "credits",
14
+ "paused",
15
+ "scheduledTime",
16
+ "postedAt",
17
+ "createdAt",
18
+ "value",
19
+ "label",
20
20
  ];
21
21
 
22
22
  const MAX_COLUMNS = 6;
23
23
  const MAX_CELL = 40;
24
24
 
25
25
  function cell(value) {
26
- if (value === undefined || value === null) return "";
27
- const text = typeof value === "object" ? JSON.stringify(value) : String(value);
28
- const flat = text.replace(/\s+/g, " ");
29
- return flat.length > MAX_CELL ? flat.slice(0, MAX_CELL - 3) + "..." : flat;
26
+ if (value === undefined || value === null) return "";
27
+ const text = typeof value === "object" ? JSON.stringify(value) : String(value);
28
+ const flat = text.replace(/\s+/g, " ");
29
+ return flat.length > MAX_CELL ? flat.slice(0, MAX_CELL - 3) + "..." : flat;
30
30
  }
31
31
 
32
32
  function pickColumns(rows) {
33
- const keys = [];
34
- for (const row of rows) {
35
- for (const key of Object.keys(row)) {
36
- if (!keys.includes(key)) keys.push(key);
37
- }
38
- }
39
- const preferred = PREFERRED_COLUMNS.filter((key) => keys.includes(key));
40
- const rest = keys.filter((key) => !preferred.includes(key));
41
- return [...preferred, ...rest].slice(0, MAX_COLUMNS);
33
+ const keys = [];
34
+ for (const row of rows) {
35
+ for (const key of Object.keys(row)) {
36
+ if (!keys.includes(key)) keys.push(key);
37
+ }
38
+ }
39
+ const preferred = PREFERRED_COLUMNS.filter((key) => keys.includes(key));
40
+ const rest = keys.filter((key) => !preferred.includes(key));
41
+ return [...preferred, ...rest].slice(0, MAX_COLUMNS);
42
42
  }
43
43
 
44
44
  function printTable(rows, indent = "") {
45
- if (!rows.length) {
46
- process.stdout.write(indent + "(no results)\n");
47
- return;
48
- }
49
- if (rows.some((row) => !row || typeof row !== "object")) {
50
- for (const row of rows) process.stdout.write(indent + cell(row) + "\n");
51
- return;
52
- }
53
- const columns = pickColumns(rows);
54
- const widths = columns.map((col) =>
55
- Math.max(col.length, ...rows.map((row) => cell(row[col]).length))
56
- );
57
- const line = (values) =>
58
- indent + values.map((value, i) => String(value).padEnd(widths[i])).join(" ") + "\n";
59
- process.stdout.write(line(columns));
60
- process.stdout.write(line(widths.map((w) => "-".repeat(w))));
61
- for (const row of rows) {
62
- process.stdout.write(line(columns.map((col) => cell(row[col]))));
63
- }
45
+ if (!rows.length) {
46
+ process.stdout.write(indent + "(no results)\n");
47
+ return;
48
+ }
49
+ if (rows.some((row) => !row || typeof row !== "object")) {
50
+ for (const row of rows) process.stdout.write(indent + cell(row) + "\n");
51
+ return;
52
+ }
53
+ const columns = pickColumns(rows);
54
+ const widths = columns.map((col) => Math.max(col.length, ...rows.map((row) => cell(row[col]).length)));
55
+ const line = (values) => indent + values.map((value, i) => String(value).padEnd(widths[i])).join(" ") + "\n";
56
+ process.stdout.write(line(columns));
57
+ process.stdout.write(line(widths.map((w) => "-".repeat(w))));
58
+ for (const row of rows) {
59
+ process.stdout.write(line(columns.map((col) => cell(row[col]))));
60
+ }
64
61
  }
65
62
 
66
63
  function printObject(obj, indent = "") {
67
- for (const [key, value] of Object.entries(obj)) {
68
- if (value === undefined) continue;
69
- if (Array.isArray(value)) {
70
- if (value.length && value.every((v) => v && typeof v === "object")) {
71
- process.stdout.write(`${indent}${key}:\n`);
72
- printTable(value, indent + " ");
73
- } else {
74
- process.stdout.write(`${indent}${key}: ${value.map(cell).join(", ")}\n`);
75
- }
76
- } else if (value && typeof value === "object") {
77
- process.stdout.write(`${indent}${key}:\n`);
78
- printObject(value, indent + " ");
79
- } else {
80
- process.stdout.write(`${indent}${key}: ${value}\n`);
81
- }
82
- }
64
+ for (const [key, value] of Object.entries(obj)) {
65
+ if (value === undefined) continue;
66
+ if (Array.isArray(value)) {
67
+ if (value.length && value.every((v) => v && typeof v === "object")) {
68
+ process.stdout.write(`${indent}${key}:\n`);
69
+ printTable(value, indent + " ");
70
+ } else {
71
+ process.stdout.write(`${indent}${key}: ${value.map(cell).join(", ")}\n`);
72
+ }
73
+ } else if (value && typeof value === "object") {
74
+ process.stdout.write(`${indent}${key}:\n`);
75
+ printObject(value, indent + " ");
76
+ } else {
77
+ process.stdout.write(`${indent}${key}: ${value}\n`);
78
+ }
79
+ }
83
80
  }
84
81
 
85
82
  export function print(result, { json = false } = {}) {
86
- if (json || !process.stdout.isTTY) {
87
- process.stdout.write(JSON.stringify(result, null, 2) + "\n");
88
- return;
89
- }
90
- const data = result && typeof result === "object" && "data" in result ? result.data : result;
91
- if (Array.isArray(data)) {
92
- printTable(data);
93
- } else if (data && typeof data === "object") {
94
- printObject(data);
95
- } else {
96
- process.stdout.write(String(data) + "\n");
97
- }
98
- if (result && typeof result === "object" && result.pagination) {
99
- const p = result.pagination;
100
- const pages = p.pages || (p.limit ? Math.ceil((p.total || 0) / p.limit) : undefined);
101
- process.stdout.write(`page ${p.page}${pages ? ` of ${pages}` : ""} (${p.total} total)\n`);
102
- }
83
+ if (json || !process.stdout.isTTY) {
84
+ process.stdout.write(JSON.stringify(result, null, 2) + "\n");
85
+ return;
86
+ }
87
+ const data = result && typeof result === "object" && "data" in result ? result.data : result;
88
+ if (Array.isArray(data)) {
89
+ printTable(data);
90
+ } else if (data && typeof data === "object") {
91
+ printObject(data);
92
+ } else {
93
+ process.stdout.write(String(data) + "\n");
94
+ }
95
+ if (result && typeof result === "object" && result.pagination) {
96
+ const p = result.pagination;
97
+ const pages = p.pages || (p.limit ? Math.ceil((p.total || 0) / p.limit) : undefined);
98
+ process.stdout.write(`page ${p.page}${pages ? ` of ${pages}` : ""} (${p.total} total)\n`);
99
+ }
103
100
  }
104
101
 
105
102
  export function printError(err) {
106
- const type = (err && err.type) || "internal_error";
107
- const message = (err && err.message) || "Unknown error";
108
- process.stderr.write(`error (${type}): ${message}\n`);
103
+ const type = (err && err.type) || "internal_error";
104
+ const message = (err && err.message) || "Unknown error";
105
+ process.stderr.write(`error (${type}): ${message}\n`);
109
106
  }