@kaynooo/cli-dev 0.2.0 → 0.3.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/cli.js +92 -51
- package/package.json +2 -2
package/dist/cli.js
CHANGED
|
@@ -1,14 +1,35 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
1
|
+
#!/usr/bin/env -S bun --no-env-file
|
|
2
2
|
// @bun
|
|
3
3
|
|
|
4
4
|
// src/cli.ts
|
|
5
|
-
import
|
|
5
|
+
import process10 from "process";
|
|
6
6
|
// ../kit/src/args.ts
|
|
7
|
-
import
|
|
7
|
+
import process6 from "process";
|
|
8
8
|
import { parseArgs } from "util";
|
|
9
9
|
|
|
10
|
-
// ../kit/src/
|
|
10
|
+
// ../kit/src/format.ts
|
|
11
|
+
import process4 from "process";
|
|
12
|
+
|
|
13
|
+
// ../kit/src/mode.ts
|
|
11
14
|
import process2 from "process";
|
|
15
|
+
var MODES = new Set(["terminal", "ci", "agent", "pipe"]);
|
|
16
|
+
var AGENT_VARIABLES = ["CLAUDECODE", "AI_AGENT"];
|
|
17
|
+
function isSet(value) {
|
|
18
|
+
return value !== undefined && value !== "" && value !== "0" && value.toLowerCase() !== "false";
|
|
19
|
+
}
|
|
20
|
+
function outputMode(stream = process2.stdout, env = process2.env) {
|
|
21
|
+
const chosen = env.KIT_MODE;
|
|
22
|
+
if (chosen !== undefined && MODES.has(chosen))
|
|
23
|
+
return chosen;
|
|
24
|
+
if (AGENT_VARIABLES.some((name) => isSet(env[name])))
|
|
25
|
+
return "agent";
|
|
26
|
+
if (stream.isTTY === true)
|
|
27
|
+
return "terminal";
|
|
28
|
+
return isSet(env.CI) ? "ci" : "pipe";
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// ../kit/src/style.ts
|
|
32
|
+
import process3 from "process";
|
|
12
33
|
|
|
13
34
|
// ../../node_modules/.bun/@kaynooo+utils@2.31.2+d0769e28dc4dad16/node_modules/@kaynooo/utils/dist/chunk-mtetydvc.js
|
|
14
35
|
function x(s) {
|
|
@@ -271,21 +292,26 @@ function le(i = {}) {
|
|
|
271
292
|
}
|
|
272
293
|
var t = le();
|
|
273
294
|
|
|
295
|
+
// ../kit/src/theme.ts
|
|
296
|
+
var STYLES = new Set(["bold", "dim", "italic", "underline", "inverse", "strikethrough", "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "gray", "grey", "blackBright", "redBright", "greenBright", "yellowBright", "blueBright", "magentaBright", "cyanBright", "whiteBright"]);
|
|
297
|
+
var MAX_FILE_BYTES = 64 * 1024;
|
|
298
|
+
var warned = new Set;
|
|
299
|
+
|
|
274
300
|
// ../kit/src/style.ts
|
|
275
|
-
function takesControlCodes(stream, env =
|
|
276
|
-
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb";
|
|
301
|
+
function takesControlCodes(stream, env = process3.env) {
|
|
302
|
+
return stream.isTTY === true && env.NO_COLOR === undefined && env.NODE_DISABLE_COLORS === undefined && env.TERM !== "dumb" && outputMode(stream, env) === "terminal";
|
|
277
303
|
}
|
|
278
|
-
function paint(stream, env =
|
|
304
|
+
function paint(stream, env = process3.env) {
|
|
279
305
|
return le({ stream: { isTTY: takesControlCodes(stream, env) }, env });
|
|
280
306
|
}
|
|
281
|
-
var
|
|
282
|
-
passed:
|
|
283
|
-
failed:
|
|
284
|
-
running:
|
|
285
|
-
queued:
|
|
286
|
-
skipped:
|
|
307
|
+
var STATUS_WORDS = {
|
|
308
|
+
passed: ["passed", "success", "succeeded", "ok"],
|
|
309
|
+
failed: ["failed", "failure", "error", "errored"],
|
|
310
|
+
running: ["running", "in_progress", "started", "holding"],
|
|
311
|
+
queued: ["queued", "waiting", "pending", "created", "blocked", "scheduled"],
|
|
312
|
+
skipped: ["skipped", "cancelled", "canceled", "manual"]
|
|
287
313
|
};
|
|
288
|
-
var STATUS_OF = new Map(Object.entries(
|
|
314
|
+
var STATUS_OF = new Map(Object.entries(STATUS_WORDS).flatMap(([status, words]) => words.map((word) => [word, status])));
|
|
289
315
|
|
|
290
316
|
// ../kit/src/format.ts
|
|
291
317
|
function upTo(text, separator) {
|
|
@@ -293,32 +319,35 @@ function upTo(text, separator) {
|
|
|
293
319
|
return end === -1 ? text : text.slice(0, end);
|
|
294
320
|
}
|
|
295
321
|
var ESCAPE = String.fromCharCode(27);
|
|
322
|
+
function jsonText(value, replacer, { stream = process4.stdout, env = process4.env } = {}) {
|
|
323
|
+
return JSON.stringify(value, replacer, outputMode(stream, env) === "agent" ? undefined : 2);
|
|
324
|
+
}
|
|
296
325
|
function printJson(value) {
|
|
297
|
-
console.log(
|
|
326
|
+
console.log(jsonText(value));
|
|
298
327
|
}
|
|
299
328
|
|
|
300
329
|
// ../kit/src/help.ts
|
|
301
|
-
import
|
|
330
|
+
import process5 from "process";
|
|
302
331
|
var OPTIONAL = /\[[^\]]+\]/g;
|
|
303
332
|
var USAGE_PREFIX = /^usage:\s*/i;
|
|
304
333
|
var FULL_STOP = /\.$/;
|
|
305
|
-
function usageLine(usage, colors = paint(
|
|
334
|
+
function usageLine(usage, colors = paint(process5.stdout)) {
|
|
306
335
|
return `${colors.bold("Usage:")} ${usage.replace(USAGE_PREFIX, "").replaceAll(OPTIONAL, (part) => colors.dim(part))}`;
|
|
307
336
|
}
|
|
308
|
-
function helpText(usage, options, colors = paint(
|
|
337
|
+
function helpText(usage, options, colors = paint(process5.stdout), { examples = [], replaces, exits = [] } = {}) {
|
|
309
338
|
return [usageLine(usage, colors), ...replacesRows(replaces, colors), "", colors.bold("Options:"), ...optionRows(options, colors), ...exitRows(exits, colors), ...exampleRows(examples, colors)].join(`
|
|
310
339
|
`);
|
|
311
340
|
}
|
|
312
|
-
function exitRows(exits, colors = paint(
|
|
341
|
+
function exitRows(exits, colors = paint(process5.stdout)) {
|
|
313
342
|
if (exits.length === 0)
|
|
314
343
|
return [];
|
|
315
344
|
const width = Math.max(...exits.map(([code]) => String(code).length));
|
|
316
345
|
return ["", colors.bold("Exits:"), ...exits.map(([code, meaning]) => ` ${colors.cyan(String(code).padEnd(width))} ${meaning}`)];
|
|
317
346
|
}
|
|
318
|
-
function replacesRows(replaces, colors = paint(
|
|
319
|
-
return replaces === undefined ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
347
|
+
function replacesRows(replaces, colors = paint(process5.stdout)) {
|
|
348
|
+
return replaces === undefined || outputMode() === "agent" ? [] : ["", `${colors.bold("Instead of:")} ${replaces}`];
|
|
320
349
|
}
|
|
321
|
-
function wantsHelpJson(env =
|
|
350
|
+
function wantsHelpJson(env = process5.env) {
|
|
322
351
|
return env.KIT_HELP === "json";
|
|
323
352
|
}
|
|
324
353
|
function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
@@ -337,12 +366,12 @@ function description(usage, { examples = [], replaces, exits } = {}, options) {
|
|
|
337
366
|
...options === undefined ? {} : { options: Object.entries(options).map(([name, { short, description }]) => ({ name, ...short === undefined ? {} : { short }, ...description === undefined ? {} : { description } })) }
|
|
338
367
|
};
|
|
339
368
|
}
|
|
340
|
-
function exampleRows(examples, colors = paint(
|
|
341
|
-
if (examples.length === 0)
|
|
369
|
+
function exampleRows(examples, colors = paint(process5.stdout)) {
|
|
370
|
+
if (examples.length === 0 || outputMode() === "agent")
|
|
342
371
|
return [];
|
|
343
372
|
return ["", colors.bold("Examples:"), ...examples.map((example) => typeof example === "string" ? ` ${example}` : ` ${example[0]} ${colors.dim(`# ${example[1]}`)}`)];
|
|
344
373
|
}
|
|
345
|
-
function optionRows(options, colors = paint(
|
|
374
|
+
function optionRows(options, colors = paint(process5.stdout)) {
|
|
346
375
|
const rows = Object.entries(options).map(([name, option]) => {
|
|
347
376
|
const short = option.short === undefined ? " " : `${colors.cyan(`-${option.short}`)}, `;
|
|
348
377
|
const named = option.choices === undefined ? "<value>" : `<${option.choices.join("|")}>`;
|
|
@@ -394,7 +423,6 @@ var DASHES = /^-+/;
|
|
|
394
423
|
function jsonOption(holds) {
|
|
395
424
|
return { type: "boolean", description: holds === undefined ? "print JSON" : `print JSON: ${holds}` };
|
|
396
425
|
}
|
|
397
|
-
|
|
398
426
|
class Exit extends Error {
|
|
399
427
|
code;
|
|
400
428
|
hint;
|
|
@@ -438,7 +466,7 @@ var OUTPUT_OPTIONS = {
|
|
|
438
466
|
color: { type: "string", description: "auto (on a terminal alone), always or never" },
|
|
439
467
|
plain: { type: "boolean", description: "plain text, no colour or spinner: --color never" }
|
|
440
468
|
};
|
|
441
|
-
function applyOutput(color, plain, env =
|
|
469
|
+
function applyOutput(color, plain, env = process6.env) {
|
|
442
470
|
const when = plain === true ? "never" : color ?? "auto";
|
|
443
471
|
if (when === "auto")
|
|
444
472
|
return;
|
|
@@ -607,6 +635,25 @@ function nearestOption(message, options, allowNegative) {
|
|
|
607
635
|
const meant = closest(word, known.map((flag) => flag.replace(DASHES, "")));
|
|
608
636
|
return meant === undefined ? undefined : `did you mean ${known.find((flag) => flag.replace(DASHES, "") === meant)}?`;
|
|
609
637
|
}
|
|
638
|
+
// ../kit/src/process.ts
|
|
639
|
+
var CLIPBOARDS = [["wl-copy"], ["xclip", "-selection", "clipboard"], ["xsel", "--clipboard", "--input"], ["pbcopy"], ["clip.exe"]];
|
|
640
|
+
async function copyText(text, which = Bun.which) {
|
|
641
|
+
for (const [command, ...args] of CLIPBOARDS) {
|
|
642
|
+
const found = which(command);
|
|
643
|
+
if (found !== null && await pipeInto([found, ...args], text))
|
|
644
|
+
return true;
|
|
645
|
+
}
|
|
646
|
+
return false;
|
|
647
|
+
}
|
|
648
|
+
async function pipeInto(argv, text) {
|
|
649
|
+
try {
|
|
650
|
+
const copy = Bun.spawn(argv, { stdin: new Blob([text]), stdout: "ignore", stderr: "ignore" });
|
|
651
|
+
return await copy.exited === 0;
|
|
652
|
+
} catch {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
610
657
|
// ../kit/src/progress.ts
|
|
611
658
|
var ESC = String.fromCharCode(27);
|
|
612
659
|
var ESCAPE_CODE = new RegExp(`${ESC}(?:\\[[\\x30-\\x3F]*[\\x20-\\x2F]*[\\x40-\\x7E]|\\][^\\u0007${ESC}]*(?:\\u0007|${ESC}\\\\))`, "y");
|
|
@@ -615,13 +662,13 @@ var CLOSE_LINK = `${ESC}]8;;${ESC}\\`;
|
|
|
615
662
|
// ../kit/src/cli.ts
|
|
616
663
|
import { readFileSync, realpathSync } from "fs";
|
|
617
664
|
import { dirname as dirname2, join as join2 } from "path";
|
|
618
|
-
import
|
|
665
|
+
import process8 from "process";
|
|
619
666
|
|
|
620
667
|
// ../kit/src/usage.ts
|
|
621
668
|
import { appendFileSync, mkdirSync } from "fs";
|
|
622
669
|
import { homedir } from "os";
|
|
623
670
|
import { dirname, isAbsolute, join } from "path";
|
|
624
|
-
import
|
|
671
|
+
import process7 from "process";
|
|
625
672
|
|
|
626
673
|
// ../kit/src/logs.ts
|
|
627
674
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
@@ -635,14 +682,14 @@ var usageError;
|
|
|
635
682
|
function noteUsageError(message) {
|
|
636
683
|
usageError = message;
|
|
637
684
|
}
|
|
638
|
-
function stateHome(env =
|
|
685
|
+
function stateHome(env = process7.env) {
|
|
639
686
|
const dir = env.XDG_STATE_HOME;
|
|
640
687
|
return dir !== undefined && isAbsolute(dir) ? dir : join(homedir(), ".local", "state");
|
|
641
688
|
}
|
|
642
|
-
function usagePath(env =
|
|
689
|
+
function usagePath(env = process7.env) {
|
|
643
690
|
return join(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
644
691
|
}
|
|
645
|
-
function usageRecorded(env =
|
|
692
|
+
function usageRecorded(env = process7.env) {
|
|
646
693
|
const off = env.KAYNOOO_CLI_NO_USAGE;
|
|
647
694
|
return env.NODE_ENV !== "test" && (off === undefined || off === "" || off === "0");
|
|
648
695
|
}
|
|
@@ -656,7 +703,7 @@ function usageEntry(bin, command, argv, code, now = new Date) {
|
|
|
656
703
|
usageError = undefined;
|
|
657
704
|
return { at: now.toISOString(), bin, ...command === undefined ? {} : { command }, flags: flagNames(argv), code, ...unknown === undefined ? {} : { unknown } };
|
|
658
705
|
}
|
|
659
|
-
function recordUsage(entry, env =
|
|
706
|
+
function recordUsage(entry, env = process7.env) {
|
|
660
707
|
if (!usageRecorded(env))
|
|
661
708
|
return;
|
|
662
709
|
try {
|
|
@@ -747,8 +794,8 @@ async function help2({ path, header, summary: lazySummary, commands, defaultComm
|
|
|
747
794
|
`);
|
|
748
795
|
}
|
|
749
796
|
function printError(prefix, message, hint, cause) {
|
|
750
|
-
const colors = paint(
|
|
751
|
-
const styled = takesControlCodes(
|
|
797
|
+
const colors = paint(process8.stderr);
|
|
798
|
+
const styled = takesControlCodes(process8.stderr);
|
|
752
799
|
const head = styled ? `${colors.red.bold("error")} ${colors.dim(`${prefix}:`)}` : `error: ${prefix}:`;
|
|
753
800
|
const under = (label, text, tint) => text === undefined ? "" : `
|
|
754
801
|
${styled ? tint(label) : `${label}:`} ${text}`;
|
|
@@ -767,7 +814,7 @@ function reportThrown(prefix, error, redact = (text) => text, json = false) {
|
|
|
767
814
|
const help = error instanceof UsageError ? `run ${prefix} --help for its options` : undefined;
|
|
768
815
|
const named = error instanceof Error && !(error instanceof Exit) && error.name !== "Error" ? `${error.name}: ${error.message}` : undefined;
|
|
769
816
|
const cause = error instanceof Error && error.cause instanceof Error ? redact(error.cause.message) : undefined;
|
|
770
|
-
if (error instanceof
|
|
817
|
+
if (error instanceof Exit && error.code === 2)
|
|
771
818
|
noteUsageError(error.message);
|
|
772
819
|
const report = json ? printJsonError : (message, hint, causeText) => printError(prefix, message, hint, causeText);
|
|
773
820
|
report(redact(named ?? (error instanceof Error ? error.message : String(error))), error instanceof Exit ? error.hint ?? help : undefined, cause);
|
|
@@ -791,11 +838,11 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
791
838
|
}
|
|
792
839
|
}
|
|
793
840
|
if (name === undefined && level.path.length > 1) {
|
|
794
|
-
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
841
|
+
console.error(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stderr));
|
|
795
842
|
return 2;
|
|
796
843
|
}
|
|
797
844
|
if (name === undefined || name === "help" || name === "--help" || name === "-h") {
|
|
798
|
-
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]),
|
|
845
|
+
console.log(await help2(await expand(level, [...level.path.slice(1), ...argv]), process8.stdout));
|
|
799
846
|
return 0;
|
|
800
847
|
}
|
|
801
848
|
if (level.path.length === 1 && (name === "--version" || name === "-v")) {
|
|
@@ -922,7 +969,7 @@ function packageVersion(entry = Bun.main) {
|
|
|
922
969
|
return "0.0.0";
|
|
923
970
|
}
|
|
924
971
|
}
|
|
925
|
-
async function runCli(cli, argv =
|
|
972
|
+
async function runCli(cli, argv = process8.argv.slice(2)) {
|
|
926
973
|
const version = cli.version ?? packageVersion();
|
|
927
974
|
if (argv[0] === COMPLETE)
|
|
928
975
|
return printCandidates(candidates(scopeOf(cli.commands, cli.extraCommands, cli.aliases), cli.defaultCommand, argv.slice(1)));
|
|
@@ -1197,21 +1244,15 @@ async function bytesInput(argument, what) {
|
|
|
1197
1244
|
return fail(`give ${what} as an argument or pipe it on stdin`, 2);
|
|
1198
1245
|
return new Uint8Array(await Bun.stdin.arrayBuffer());
|
|
1199
1246
|
}
|
|
1200
|
-
async function copyToClipboard(text) {
|
|
1201
|
-
if (Bun.which("wl-copy") === null)
|
|
1202
|
-
return false;
|
|
1203
|
-
const copy = Bun.spawn(["wl-copy"], { stdin: new Blob([text]), stdout: "ignore", stderr: "ignore" });
|
|
1204
|
-
return await copy.exited === 0;
|
|
1205
|
-
}
|
|
1206
1247
|
function printRows(rows) {
|
|
1207
|
-
const { cyan } = paint(
|
|
1248
|
+
const { cyan } = paint(process10.stdout);
|
|
1208
1249
|
const width = Math.max(...rows.map(([label]) => label.length));
|
|
1209
1250
|
for (const [label, value] of rows)
|
|
1210
1251
|
console.log(`${cyan(label.padEnd(width))} ${value}`);
|
|
1211
1252
|
}
|
|
1212
1253
|
var PASSWORD_USAGE = `dev password [length] [format] [-b|--bcrypt [password]]: a random password of <length> bytes (default ${DEFAULT_LENGTH}) as hex, base64 (default) or base64url
|
|
1213
1254
|
|
|
1214
|
-
Printed on stdout and copied to the clipboard
|
|
1255
|
+
Printed on stdout and copied to the clipboard when a tool for it (wl-copy, xclip, xsel, pbcopy) exists (the copy is said on stderr). With --bcrypt it is the bcrypt hash (cost 10) that prints: of the password given, or of a generated one, which then prints on stderr and is the one copied. Bun writes bcrypt as $2b$, the hash prints with the $2y$ prefix htpasswd -B writes, the one Apache, nginx and traefik basic auth all accept.
|
|
1215
1256
|
Exits 0 when it printed the password or hash, 2 on a usage error.`;
|
|
1216
1257
|
var PASSWORD_OPTIONS = {
|
|
1217
1258
|
bcrypt: { type: "string", short: "b", optional: { bare: "" }, description: "print the bcrypt hash of the password given, or of a generated one" },
|
|
@@ -1233,13 +1274,13 @@ async function password(args) {
|
|
|
1233
1274
|
const given = values.bcrypt === undefined || values.bcrypt === "" ? undefined : values.bcrypt;
|
|
1234
1275
|
const secret = given ?? generatePassword(bytes, format);
|
|
1235
1276
|
const hash = values.bcrypt === undefined ? undefined : await bcryptHash(secret);
|
|
1236
|
-
const copied = values["no-copy"] === true ? false : await
|
|
1277
|
+
const copied = values["no-copy"] === true ? false : await copyText(given === undefined ? secret : hash ?? secret);
|
|
1237
1278
|
if (values.json === true) {
|
|
1238
1279
|
printJson({ ...hash === undefined ? {} : { hash }, ...given === undefined ? { password: secret } : {}, format, bytes, copied });
|
|
1239
1280
|
return 0;
|
|
1240
1281
|
}
|
|
1241
1282
|
if (copied)
|
|
1242
|
-
console.error(paint(
|
|
1283
|
+
console.error(paint(process10.stderr).dim("copied to clipboard"));
|
|
1243
1284
|
if (hash !== undefined && given === undefined)
|
|
1244
1285
|
console.error(secret);
|
|
1245
1286
|
console.log(hash ?? secret);
|
|
@@ -1267,7 +1308,7 @@ async function jwt(args) {
|
|
|
1267
1308
|
printJson({ header: decoded.header, payload: decoded.payload, claims, signatureBytes: decoded.signatureBytes });
|
|
1268
1309
|
return 0;
|
|
1269
1310
|
}
|
|
1270
|
-
const { bold } = paint(
|
|
1311
|
+
const { bold } = paint(process10.stdout);
|
|
1271
1312
|
console.log(`${bold("header")}
|
|
1272
1313
|
${JSON.stringify(decoded.header, null, 2)}
|
|
1273
1314
|
|
|
@@ -1421,7 +1462,7 @@ async function hash(args) {
|
|
|
1421
1462
|
return 0;
|
|
1422
1463
|
}
|
|
1423
1464
|
if (import.meta.main) {
|
|
1424
|
-
|
|
1465
|
+
process10.exitCode = await runCli({
|
|
1425
1466
|
name: "dev",
|
|
1426
1467
|
about: "small developer utilities: passwords and bcrypt hashes, JWTs, timestamps, UUIDs, base64, URLs and file hashes",
|
|
1427
1468
|
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.
|
|
4
|
+
"version": "0.3.0",
|
|
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.24.0"
|
|
40
40
|
}
|
|
41
41
|
}
|