@kaynooo/cli-dev 0.5.1 → 0.5.2
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/cli.js +91 -32
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process12 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
7
|
import process6 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
@@ -109,6 +109,23 @@ var STATUS_WORDS = {
|
|
|
109
109
|
var STATUS_OF = new Map(Object.entries(STATUS_WORDS).flatMap(([status, words]) => words.map((word) => [word, status])));
|
|
110
110
|
|
|
111
111
|
// ../kit/src/format.ts
|
|
112
|
+
var UNITS = [["d", 86400], ["h", 3600], ["m", 60], ["s", 1]];
|
|
113
|
+
function formatSeconds(total) {
|
|
114
|
+
let rest = Math.max(0, Math.round(total));
|
|
115
|
+
const amounts = UNITS.map(([unit, size]) => {
|
|
116
|
+
const amount = Math.floor(rest / size);
|
|
117
|
+
rest -= amount * size;
|
|
118
|
+
return `${amount}${unit}`;
|
|
119
|
+
});
|
|
120
|
+
const first = amounts.findIndex((amount) => !amount.startsWith("0"));
|
|
121
|
+
if (first === -1)
|
|
122
|
+
return "0s";
|
|
123
|
+
const next = amounts[first + 1];
|
|
124
|
+
return next === undefined || next.startsWith("0") ? amounts[first] ?? "0s" : `${amounts[first]} ${next}`;
|
|
125
|
+
}
|
|
126
|
+
function formatDuration(ms) {
|
|
127
|
+
return ms < 60000 ? `${(Math.max(0, ms) / 1000).toFixed(1)}s` : formatSeconds(ms / 1000);
|
|
128
|
+
}
|
|
112
129
|
function upTo(text, separator) {
|
|
113
130
|
const end = text.indexOf(separator);
|
|
114
131
|
return end === -1 ? text : text.slice(0, end);
|
|
@@ -123,7 +140,7 @@ function printJson(value) {
|
|
|
123
140
|
|
|
124
141
|
// ../kit/src/help.ts
|
|
125
142
|
import process5 from "process";
|
|
126
|
-
var OPTIONAL =
|
|
143
|
+
var OPTIONAL = /(?<!\x1B)\[[^\]]+\]/g;
|
|
127
144
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
128
145
|
var FULL_STOP = /\.$/;
|
|
129
146
|
function usageLine(usage, colors = paint(process5.stdout)) {
|
|
@@ -162,29 +179,40 @@ function exampleRows(examples, colors = paint(process5.stdout)) {
|
|
|
162
179
|
return [];
|
|
163
180
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
164
181
|
}
|
|
182
|
+
var MAX_INLINE_CHOICES = 24;
|
|
165
183
|
function optionRows(options, colors = paint(process5.stdout)) {
|
|
166
184
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
167
185
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
168
|
-
const
|
|
186
|
+
const choices = option.choices?.join("|");
|
|
187
|
+
const inline = choices !== undefined && choices.length <= MAX_INLINE_CHOICES;
|
|
188
|
+
const named = inline ? `<${choices}>` : "<value>";
|
|
169
189
|
const placeholder = option.optional === undefined ? named : `[${named}]`;
|
|
170
190
|
const value = option.type === "string" ? ` ${colors.dim(placeholder)}` : "";
|
|
171
|
-
|
|
191
|
+
const described = choices === undefined || inline ? option.description ?? "" : [option.description, `one of ${option.choices.join(", ")}`].filter(Boolean).join("; ");
|
|
192
|
+
return { flag: `${short}${colors.cyan(`--${name}`)}${value}`, width: `-x, --${name}`.length + (option.type === "string" ? placeholder.length + 1 : 0), description: described };
|
|
172
193
|
});
|
|
173
194
|
const width = Math.max(0, ...rows.map((row) => row.width));
|
|
174
195
|
return rows.map((row) => row.description === "" ? ` ${row.flag}` : ` ${row.flag}${" ".repeat(width - row.width)} ${colors.dim(row.description)}`);
|
|
175
196
|
}
|
|
176
197
|
|
|
177
198
|
// ../kit/src/text.ts
|
|
178
|
-
var ANSI = new RegExp(`${String.fromCharCode(27)}\\[[\\d;?]*[a-z]`, "gi");
|
|
179
199
|
function distance(a, b) {
|
|
180
|
-
|
|
181
|
-
|
|
200
|
+
const left = [...a];
|
|
201
|
+
const right = [...b];
|
|
202
|
+
let before = [];
|
|
203
|
+
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
204
|
+
for (const [i, x] of left.entries()) {
|
|
182
205
|
const current = [i + 1];
|
|
183
|
-
for (const [j,
|
|
184
|
-
|
|
206
|
+
for (const [j, y] of right.entries()) {
|
|
207
|
+
let cost = Math.min((previous[j + 1] ?? 0) + 1, (current[j] ?? 0) + 1, (previous[j] ?? 0) + (x === y ? 0 : 1));
|
|
208
|
+
if (i > 0 && j > 0 && x === right[j - 1] && left[i - 1] === y)
|
|
209
|
+
cost = Math.min(cost, (before[j - 1] ?? 0) + 1);
|
|
210
|
+
current.push(cost);
|
|
211
|
+
}
|
|
212
|
+
before = previous;
|
|
185
213
|
previous = current;
|
|
186
214
|
}
|
|
187
|
-
return previous[
|
|
215
|
+
return previous[right.length] ?? 0;
|
|
188
216
|
}
|
|
189
217
|
function closest(name, names) {
|
|
190
218
|
const started = names.find((known) => known.startsWith(name));
|
|
@@ -477,13 +505,31 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
477
505
|
// ../kit/src/cli.ts
|
|
478
506
|
import { readFileSync as readFileSync2, realpathSync as realpathSync2 } from "fs";
|
|
479
507
|
import { dirname as dirname2, join as join2 } from "path";
|
|
480
|
-
import
|
|
508
|
+
import process10 from "process";
|
|
509
|
+
|
|
510
|
+
// ../kit/src/notify.ts
|
|
511
|
+
import process8 from "process";
|
|
512
|
+
var NOTIFY_AFTER_MS = 1e4;
|
|
513
|
+
var CANCELLED = 130;
|
|
514
|
+
function base64(text) {
|
|
515
|
+
return Buffer.from(text).toString("base64");
|
|
516
|
+
}
|
|
517
|
+
function notification(title, body, env = process8.env) {
|
|
518
|
+
const id = `kit${process8.pid}`;
|
|
519
|
+
const codes = [`\x1B]99;i=${id}:d=0:o=unfocused:e=1;${base64(title)}\x1B\\`, `\x1B]99;i=${id}:p=body:e=1;${base64(body)}\x1B\\`];
|
|
520
|
+
return codes.map((code) => env.TMUX === undefined || env.TMUX === "" ? code : `\x1BPtmux;${code.replaceAll("\x1B", "\x1B\x1B")}\x1B\\`).join("");
|
|
521
|
+
}
|
|
522
|
+
function notifyEnd(command, code, ms, { stream = process8.stderr, env = process8.env } = {}) {
|
|
523
|
+
if (ms < NOTIFY_AFTER_MS || code === CANCELLED || outputMode(stream, env) !== "terminal")
|
|
524
|
+
return;
|
|
525
|
+
stream.write(notification(command, code === 0 ? `done in ${formatDuration(ms)}` : `failed with exit ${code} after ${formatDuration(ms)}`, env));
|
|
526
|
+
}
|
|
481
527
|
|
|
482
528
|
// ../kit/src/usage.ts
|
|
483
529
|
import { appendFileSync, mkdirSync } from "fs";
|
|
484
530
|
import { homedir } from "os";
|
|
485
531
|
import { dirname, isAbsolute, join } from "path";
|
|
486
|
-
import
|
|
532
|
+
import process9 from "process";
|
|
487
533
|
|
|
488
534
|
// ../kit/src/logs.ts
|
|
489
535
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -497,14 +543,14 @@ var usageError;
|
|
|
497
543
|
function noteUsageError(message) {
|
|
498
544
|
usageError = message;
|
|
499
545
|
}
|
|
500
|
-
function stateHome(env =
|
|
546
|
+
function stateHome(env = process9.env) {
|
|
501
547
|
const dir = env.XDG_STATE_HOME;
|
|
502
548
|
return dir !== undefined && isAbsolute(dir) ? dir : join(homedir(), ".local", "state");
|
|
503
549
|
}
|
|
504
|
-
function usagePath(env =
|
|
550
|
+
function usagePath(env = process9.env) {
|
|
505
551
|
return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
506
552
|
}
|
|
507
|
-
function usageRecorded(env =
|
|
553
|
+
function usageRecorded(env = process9.env) {
|
|
508
554
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
509
555
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
510
556
|
}
|
|
@@ -518,7 +564,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
518
564
|
usageError = undefined;
|
|
519
565
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
520
566
|
}
|
|
521
|
-
function recordUsage(entry, env =
|
|
567
|
+
function recordUsage(entry, env = process9.env) {
|
|
522
568
|
if (!usageRecorded(env))
|
|
523
569
|
return;
|
|
524
570
|
try {
|
|
@@ -608,8 +654,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
608
654
|
`);
|
|
609
655
|
}
|
|
610
656
|
function printError(prefix, message, hint, cause) {
|
|
611
|
-
const colors = paint(
|
|
612
|
-
const styled = takesControlCodes(
|
|
657
|
+
const colors = paint(process10.stderr);
|
|
658
|
+
const styled = takesControlCodes(process10.stderr);
|
|
613
659
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
614
660
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
615
661
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -652,11 +698,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
652
698
|
}
|
|
653
699
|
}
|
|
654
700
|
if (name === undefined && level.path.length > 1) {
|
|
655
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
701
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process10.stderr));
|
|
656
702
|
return 2;
|
|
657
703
|
}
|
|
658
704
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
659
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
705
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process10.stdout));
|
|
660
706
|
return 0;
|
|
661
707
|
}
|
|
662
708
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -791,10 +837,13 @@ async function runCli(cli, argv = scriptArgs()) {
|
|
|
791
837
|
const version = cli.version ?? packageVersion();
|
|
792
838
|
if (argv[0] === COMPLETE)
|
|
793
839
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
840
|
+
const started = performance.now();
|
|
794
841
|
const code = await dispatch({ path: [cli.name], header: (colors, about) => `${colors.bold(cli.name)} ${colors.dim(version)} ${about}`, summary: cli.about, ...scopeOf(cli.commands, cli.extraCommands, cli.aliases), defaultCommand: cli.defaultCommand, runBare: cli.runBare === true, examples: cli.examples, replaces: cli.replaces }, argv, { version, redact: cli.redact });
|
|
842
|
+
const command = commandPath(scopeOf(cli.commands, cli.extraCommands, cli.aliases), argv);
|
|
843
|
+
notifyEnd(command === undefined ? cli.name : `${cli.name} ${command}`, code, performance.now() - started);
|
|
795
844
|
if (cli.usage !== false) {
|
|
796
845
|
try {
|
|
797
|
-
recordUsage(usageEntry(cli.name,
|
|
846
|
+
recordUsage(usageEntry(cli.name, command, argv, code));
|
|
798
847
|
} catch {}
|
|
799
848
|
}
|
|
800
849
|
return code;
|
|
@@ -841,6 +890,15 @@ var ALGORITHMS = ["sha256", "sha1", "md5", "sha512"];
|
|
|
841
890
|
function hashHex(algorithm, bytes) {
|
|
842
891
|
return new Bun.CryptoHasher(algorithm).update(bytes).digest("hex");
|
|
843
892
|
}
|
|
893
|
+
async function hashStream(algorithm, stream) {
|
|
894
|
+
const hasher = new Bun.CryptoHasher(algorithm);
|
|
895
|
+
let bytes = 0;
|
|
896
|
+
for await (const chunk of stream) {
|
|
897
|
+
hasher.update(chunk);
|
|
898
|
+
bytes += chunk.length;
|
|
899
|
+
}
|
|
900
|
+
return { digest: hasher.digest("hex"), bytes };
|
|
901
|
+
}
|
|
844
902
|
|
|
845
903
|
// src/ids.ts
|
|
846
904
|
var CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
@@ -881,7 +939,7 @@ async function publicIp(family, service = IP_SERVICE) {
|
|
|
881
939
|
}
|
|
882
940
|
|
|
883
941
|
// src/time.ts
|
|
884
|
-
var
|
|
942
|
+
var UNITS2 = [["d", 86400000], ["h", 3600000], ["m", 60000], ["s", 1000]];
|
|
885
943
|
var DURATION_UNIT_MS = { ms: 1, s: 1000, m: 60000, h: 3600000, d: 86400000 };
|
|
886
944
|
function parts(ms, timeZone) {
|
|
887
945
|
const formatter = new Intl.DateTimeFormat("en-US", { timeZone, hourCycle: "h23", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", timeZoneName: "longOffset" });
|
|
@@ -902,7 +960,7 @@ function offsetAt(ms, timeZone) {
|
|
|
902
960
|
function humanDuration(ms) {
|
|
903
961
|
let rest = Math.floor(Math.abs(ms) / 1000) * 1000;
|
|
904
962
|
const out = [];
|
|
905
|
-
for (const [name, size] of
|
|
963
|
+
for (const [name, size] of UNITS2) {
|
|
906
964
|
const count = Math.floor(rest / size);
|
|
907
965
|
if (count > 0) {
|
|
908
966
|
out.push(`${count}${name}`);
|
|
@@ -1075,7 +1133,7 @@ async function bytesInput(argument, what) {
|
|
|
1075
1133
|
return new Uint8Array(await Bun.stdin.arrayBuffer());
|
|
1076
1134
|
}
|
|
1077
1135
|
function printRows(rows) {
|
|
1078
|
-
const { cyan } = paint(
|
|
1136
|
+
const { cyan } = paint(process12.stdout);
|
|
1079
1137
|
const width = Math.max(...rows.map(([label]) => label.length));
|
|
1080
1138
|
for (const [label, value] of rows)
|
|
1081
1139
|
console.log(`${cyan(label.padEnd(width))} ${value}`);
|
|
@@ -1111,7 +1169,7 @@ async function password(args) {
|
|
|
1111
1169
|
return 0;
|
|
1112
1170
|
}
|
|
1113
1171
|
if (copied)
|
|
1114
|
-
console.error(paint(
|
|
1172
|
+
console.error(paint(process12.stderr).dim("copied to clipboard"));
|
|
1115
1173
|
if (hash !== undefined && given === undefined)
|
|
1116
1174
|
console.error(secret);
|
|
1117
1175
|
console.log(hash ?? secret);
|
|
@@ -1140,7 +1198,7 @@ async function jwt(args) {
|
|
|
1140
1198
|
printJson({ header: decoded.header, payload: decoded.payload, claims, signatureBytes: decoded.signatureBytes });
|
|
1141
1199
|
return 0;
|
|
1142
1200
|
}
|
|
1143
|
-
const { bold } = paint(
|
|
1201
|
+
const { bold } = paint(process12.stdout);
|
|
1144
1202
|
console.log(`${bold("header")}
|
|
1145
1203
|
${JSON.stringify(decoded.header, null, 2)}
|
|
1146
1204
|
|
|
@@ -1280,18 +1338,19 @@ async function hash(args) {
|
|
|
1280
1338
|
});
|
|
1281
1339
|
const algorithm = oneOf(positionals[0] ?? "", "<algorithm>", ALGORITHMS);
|
|
1282
1340
|
const file = positionals[1];
|
|
1283
|
-
let
|
|
1341
|
+
let result;
|
|
1284
1342
|
if (file === undefined) {
|
|
1285
|
-
|
|
1343
|
+
const input = await bytesInput(undefined, "a file");
|
|
1344
|
+
result = { digest: hashHex(algorithm, input), bytes: input.length };
|
|
1286
1345
|
} else {
|
|
1287
1346
|
const source = Bun.file(file);
|
|
1288
1347
|
if (!await source.exists())
|
|
1289
1348
|
return fail(`no file ${file}`, 1);
|
|
1290
|
-
|
|
1349
|
+
result = await hashStream(algorithm, source.stream());
|
|
1291
1350
|
}
|
|
1292
|
-
const digest =
|
|
1351
|
+
const digest = result.digest;
|
|
1293
1352
|
if (values.json === true)
|
|
1294
|
-
printJson({ algorithm, hash: digest, bytes: bytes
|
|
1353
|
+
printJson({ algorithm, hash: digest, bytes: result.bytes });
|
|
1295
1354
|
else
|
|
1296
1355
|
console.log(digest);
|
|
1297
1356
|
return 0;
|
|
@@ -1325,7 +1384,7 @@ async function ip(args) {
|
|
|
1325
1384
|
}
|
|
1326
1385
|
}
|
|
1327
1386
|
if (import.meta.main) {
|
|
1328
|
-
|
|
1387
|
+
process12.exitCode = await runCli({
|
|
1329
1388
|
name: "dev",
|
|
1330
1389
|
about: "small developer utilities: passwords and bcrypt hashes, JWTs, timestamps, UUIDs, base64, URLs and file hashes",
|
|
1331
1390
|
replaces: "openssl rand, htpasswd, jwt.io, date -d, uuidgen, base64, sha256sum and one-off scripts for each",
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-dev",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.2",
|
|
5
5
|
"description": "dev: small developer utilities in one bin: random passwords and bcrypt hashes, JWT decoding, timestamps, UUIDs and ULIDs, base64, URL parts and file hashes.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -36,6 +36,6 @@
|
|
|
36
36
|
"dev": "$npm_execpath src/cli.ts"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@kaynooo/cli-kit": "0.
|
|
39
|
+
"@kaynooo/cli-kit": "0.39.0"
|
|
40
40
|
}
|
|
41
41
|
}
|