@kaynooo/cli-stack 0.8.1 → 0.8.3
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/README.md +1 -1
- package/dist/cli.js +47 -30
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -12,4 +12,4 @@ bun install -g @kaynooo/cli-stack
|
|
|
12
12
|
| --- | --- |
|
|
13
13
|
| `stack port <name>` | prints the port the name always hashes to (32-bit FNV-1a, 1024-32767), so a service keeps the same port on every machine |
|
|
14
14
|
|
|
15
|
-
`stack port` takes `--min` and `--max` to change the range: the default stops below 32768, where Linux starts handing out ephemeral ports, `--min` refuses anything under 1024, and `--max` goes up to 65535. The hash used to lose precision past 2^53 and the range ran to 65535, so every name now maps to a different port than before; a port already written in a compose file keeps working, only a newly picked one comes out different. When something on this machine already listens on the port it says so on stderr; the number is still printed and the exit code stays 0. `--json` prints `{name, port, listening}`.
|
|
15
|
+
`stack port` takes `--min` and `--max` to change the range: the default stops below 32768, where Linux starts handing out ephemeral ports, `--min` refuses anything under 1024, and `--max` goes up to 65535; a `--min` at or past the default top, given alone, spreads up to 65535. The hash used to lose precision past 2^53 and the range ran to 65535, so every name now maps to a different port than before; a port already written in a compose file keeps working, only a newly picked one comes out different. When something on this machine already listens on the port it says so on stderr; the number is still printed and the exit code stays 0. `--json` prints `{name, port, listening}`.
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
#!/
|
|
2
|
-
// @bun
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
// @bun 2>/dev/null; exec bun --no-env-file --config=/dev/null "$0" "$@"
|
|
3
3
|
var __esm = (fn, res, err) => () => {
|
|
4
4
|
if (fn)
|
|
5
5
|
try {
|
|
@@ -116,11 +116,14 @@ var init_config = __esm(() => {
|
|
|
116
116
|
|
|
117
117
|
// ../kit/src/user-config.ts
|
|
118
118
|
import { homedir } from "os";
|
|
119
|
-
import { join as join2 } from "path";
|
|
119
|
+
import { isAbsolute, join as join2 } from "path";
|
|
120
120
|
import process4 from "process";
|
|
121
|
+
function xdgFolder(value, fallback) {
|
|
122
|
+
return value !== undefined && isAbsolute(value) ? value : fallback;
|
|
123
|
+
}
|
|
121
124
|
function configBase(where = {}) {
|
|
122
125
|
const xdg = where.xdg ?? (where.home === undefined ? process4.env.XDG_CONFIG_HOME : undefined);
|
|
123
|
-
return xdg
|
|
126
|
+
return xdgFolder(xdg, join2(where.home ?? homedir(), ".config"));
|
|
124
127
|
}
|
|
125
128
|
function userConfigPath(bin, where = {}) {
|
|
126
129
|
return join2(configBase(where), "kaynooo-cli", `${bin}.json`);
|
|
@@ -793,6 +796,11 @@ init_format();
|
|
|
793
796
|
|
|
794
797
|
// ../kit/src/progress.ts
|
|
795
798
|
init_format();
|
|
799
|
+
|
|
800
|
+
// ../kit/src/process.ts
|
|
801
|
+
var GROUP_SIGNALS = new Set(["SIGINT", "SIGHUP"]);
|
|
802
|
+
|
|
803
|
+
// ../kit/src/progress.ts
|
|
796
804
|
init_style();
|
|
797
805
|
init_theme();
|
|
798
806
|
var ESC = String.fromCharCode(27);
|
|
@@ -937,14 +945,16 @@ init_text();
|
|
|
937
945
|
// ../kit/src/usage.ts
|
|
938
946
|
import { appendFileSync, mkdirSync } from "fs";
|
|
939
947
|
import { homedir as homedir3 } from "os";
|
|
940
|
-
import { dirname as dirname2,
|
|
948
|
+
import { dirname as dirname2, join as join4 } from "path";
|
|
941
949
|
import process13 from "process";
|
|
942
950
|
|
|
943
951
|
// ../kit/src/logs.ts
|
|
952
|
+
init_user_config();
|
|
944
953
|
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
945
954
|
var CLI_FOLDER = "kaynooo-cli";
|
|
946
955
|
|
|
947
956
|
// ../kit/src/usage.ts
|
|
957
|
+
init_user_config();
|
|
948
958
|
var UNKNOWN_OPTION2 = /unknown option '(--[a-z][a-z0-9-]*)[=']/;
|
|
949
959
|
var FLAG_NAME = /^(?:--[a-z][a-z0-9-]*|-[a-zA-Z])$/;
|
|
950
960
|
var LONGEST_NAME = 40;
|
|
@@ -953,8 +963,7 @@ function noteUsageError(message) {
|
|
|
953
963
|
usageError = message;
|
|
954
964
|
}
|
|
955
965
|
function stateHome(env = process13.env) {
|
|
956
|
-
|
|
957
|
-
return dir !== undefined && isAbsolute(dir) ? dir : join4(homedir3(), ".local", "state");
|
|
966
|
+
return xdgFolder(env.XDG_STATE_HOME, join4(homedir3(), ".local", "state"));
|
|
958
967
|
}
|
|
959
968
|
function usagePath(env = process13.env) {
|
|
960
969
|
return join4(stateHome(env), CLI_FOLDER, "usage.jsonl");
|
|
@@ -1028,6 +1037,10 @@ function takeOutputFlags(argv) {
|
|
|
1028
1037
|
async function resolveSummary(summary) {
|
|
1029
1038
|
return typeof summary === "string" ? summary : summary();
|
|
1030
1039
|
}
|
|
1040
|
+
function lookup({ commands, aliases }, word) {
|
|
1041
|
+
const name = aliases !== undefined && Object.hasOwn(aliases, word) ? aliases[word] ?? word : word;
|
|
1042
|
+
return Object.hasOwn(commands, name) ? commands[name] : undefined;
|
|
1043
|
+
}
|
|
1031
1044
|
function scopeOf(commands, extra, aliases) {
|
|
1032
1045
|
return { commands: typeof commands === "function" ? {} : commands, lazy: [...typeof commands === "function" ? [commands] : [], ...extra === undefined ? [] : [extra]], aliases };
|
|
1033
1046
|
}
|
|
@@ -1122,12 +1135,12 @@ async function dispatch(level, typed, { version, redact }) {
|
|
|
1122
1135
|
console.log(version);
|
|
1123
1136
|
return 0;
|
|
1124
1137
|
}
|
|
1125
|
-
let command = level
|
|
1138
|
+
let command = lookup(level, name);
|
|
1126
1139
|
let rest = args;
|
|
1127
1140
|
let ran = name;
|
|
1128
1141
|
if (command === undefined && level.lazy.length > 0) {
|
|
1129
1142
|
level = await expand(level, [...level.path.slice(1), ...argv]);
|
|
1130
|
-
command = level
|
|
1143
|
+
command = lookup(level, name);
|
|
1131
1144
|
}
|
|
1132
1145
|
if (command === undefined && level.defaultCommand !== undefined) {
|
|
1133
1146
|
command = level.commands[level.defaultCommand];
|
|
@@ -1207,18 +1220,19 @@ async function leafCandidates(run, words) {
|
|
|
1207
1220
|
async function candidates(scope, defaultCommand, words, before = []) {
|
|
1208
1221
|
const [word = "", ...rest] = words;
|
|
1209
1222
|
const listing = rest.length === 0 && !word.startsWith("-");
|
|
1210
|
-
let { commands
|
|
1211
|
-
let command =
|
|
1223
|
+
let { commands } = scope;
|
|
1224
|
+
let command = lookup(scope, word);
|
|
1212
1225
|
if (listing || command === undefined) {
|
|
1213
|
-
|
|
1214
|
-
|
|
1226
|
+
const expanded = await expand(scope, [...before, ...words]);
|
|
1227
|
+
commands = expanded.commands;
|
|
1228
|
+
command = lookup(expanded, word);
|
|
1215
1229
|
}
|
|
1216
1230
|
if (listing) {
|
|
1217
1231
|
const own = await Promise.all(Object.entries(shown(commands)).map(async ([name, { summary }]) => `${name} ${await resolveSummary(summary)}`));
|
|
1218
1232
|
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
1219
1233
|
const values = fallback !== undefined && "run" in fallback ? await leafCandidates(fallback.run, [word]) : [];
|
|
1220
1234
|
const valueOf = (line) => line.slice(0, line.includes("\t") ? line.indexOf("\t") : undefined);
|
|
1221
|
-
return [...own, ...values.filter((line) => line !== FILES && !(valueOf(line)
|
|
1235
|
+
return [...own, ...values.filter((line) => line !== FILES && !Object.hasOwn(commands, valueOf(line)))];
|
|
1222
1236
|
}
|
|
1223
1237
|
if (command === undefined) {
|
|
1224
1238
|
const fallback = defaultCommand === undefined ? undefined : commands[defaultCommand];
|
|
@@ -1283,8 +1297,7 @@ function commandPath(scope, argv) {
|
|
|
1283
1297
|
const path = [];
|
|
1284
1298
|
let level = scope;
|
|
1285
1299
|
for (const word of argv.filter((each) => !each.startsWith("-"))) {
|
|
1286
|
-
const
|
|
1287
|
-
const command = Object.hasOwn(level.commands, name) ? level.commands[name] : undefined;
|
|
1300
|
+
const command = lookup(level, word);
|
|
1288
1301
|
if (command === undefined)
|
|
1289
1302
|
break;
|
|
1290
1303
|
path.push(word);
|
|
@@ -1313,11 +1326,23 @@ init_args();
|
|
|
1313
1326
|
// ../kit/src/index.ts
|
|
1314
1327
|
init_mode();
|
|
1315
1328
|
|
|
1329
|
+
// ../kit/src/port.ts
|
|
1330
|
+
function isListening(port) {
|
|
1331
|
+
try {
|
|
1332
|
+
Bun.listen({ hostname: "0.0.0.0", port, socket: { data: () => {
|
|
1333
|
+
return;
|
|
1334
|
+
} } }).stop(true);
|
|
1335
|
+
return false;
|
|
1336
|
+
} catch (error) {
|
|
1337
|
+
return error.code === "EADDRINUSE";
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1316
1340
|
// ../kit/src/report.ts
|
|
1317
1341
|
init_style();
|
|
1318
1342
|
init_text();
|
|
1319
1343
|
// ../kit/src/secrets.ts
|
|
1320
1344
|
var SECRET_WORDS = new Set(["key", "pass", "pwd", "auth", "salt", "cookie", "session", "jwt", "ssn", "iban", "card", "cvv", "otp"]);
|
|
1345
|
+
var NOT_PASSWORDS = new Set(["compass", "encompass", "bypass", "overpass", "underpass", "surpass", "trespass"]);
|
|
1321
1346
|
// ../kit/src/sessions.ts
|
|
1322
1347
|
var {Glob } = globalThis.Bun;
|
|
1323
1348
|
var SESSION_FILES = new Glob("*.json");
|
|
@@ -1347,16 +1372,6 @@ function stringToPort(name, min = MIN_PORT, max = DEFAULT_MAX_PORT) {
|
|
|
1347
1372
|
}
|
|
1348
1373
|
return min + hash % (max - min + 1);
|
|
1349
1374
|
}
|
|
1350
|
-
function isListening(port) {
|
|
1351
|
-
try {
|
|
1352
|
-
Bun.listen({ hostname: "0.0.0.0", port, socket: { data: () => {
|
|
1353
|
-
return;
|
|
1354
|
-
} } }).stop(true);
|
|
1355
|
-
return false;
|
|
1356
|
-
} catch (error) {
|
|
1357
|
-
return error.code === "EADDRINUSE";
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
1375
|
|
|
1361
1376
|
// src/commands/port.ts
|
|
1362
1377
|
var usage2 = `stack port <name>
|
|
@@ -1367,16 +1382,18 @@ Says on stderr when something already listens on it; the exit code stays 0.
|
|
|
1367
1382
|
Exits 0 when it printed the port, 2 on a usage error.`;
|
|
1368
1383
|
var options = {
|
|
1369
1384
|
min: { type: "string", description: `lowest port (${MIN_PORT}, nothing lower is allowed)` },
|
|
1370
|
-
max: { type: "string", description: `highest port (${DEFAULT_MAX_PORT}, up to ${MAX_PORT})` },
|
|
1385
|
+
max: { type: "string", description: `highest port (${DEFAULT_MAX_PORT}, or ${MAX_PORT} when --min reaches it, up to ${MAX_PORT})` },
|
|
1371
1386
|
json: jsonOption("{name, port, listening}")
|
|
1372
1387
|
};
|
|
1373
|
-
async function
|
|
1388
|
+
async function port2(args) {
|
|
1374
1389
|
const parsed = parse(args, usage2, options);
|
|
1375
1390
|
const [name] = parsed.positionals;
|
|
1376
1391
|
if (name === undefined)
|
|
1377
1392
|
fail("a name is needed");
|
|
1378
1393
|
const min = wholeNumber(parsed.values.min, "min", MIN_PORT, MIN_PORT);
|
|
1379
|
-
|
|
1394
|
+
if (min > MAX_PORT)
|
|
1395
|
+
fail(`--min goes up to ${MAX_PORT}, not ${min}`);
|
|
1396
|
+
const max = wholeNumber(parsed.values.max, "max", min < DEFAULT_MAX_PORT ? DEFAULT_MAX_PORT : MAX_PORT, min);
|
|
1380
1397
|
if (max > MAX_PORT)
|
|
1381
1398
|
fail(`--max goes up to ${MAX_PORT}, not ${max}`);
|
|
1382
1399
|
const number = stringToPort(name, min, max);
|
|
@@ -1397,6 +1414,6 @@ process16.exitCode = await runCli({
|
|
|
1397
1414
|
replaces: "picking a free host port by hand",
|
|
1398
1415
|
examples: ["stack port api", ["stack port api --json", "the port and whether something listens on it"]],
|
|
1399
1416
|
commands: {
|
|
1400
|
-
port: { run:
|
|
1417
|
+
port: { run: port2, summary: "the port a name always hashes to, with a warning when something already listens on it" }
|
|
1401
1418
|
}
|
|
1402
1419
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-stack",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.8.
|
|
4
|
+
"version": "0.8.3",
|
|
5
5
|
"description": "stack: a stable host port per service name, the same on every machine.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"registry": "https://git.kaynooo.fr/api/packages/kaynooo/npm/"
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "$npm_execpath build src/cli.ts --target bun --outdir dist",
|
|
27
|
+
"build": "$npm_execpath build src/cli.ts --target bun --outdir dist && $npm_execpath ../../scripts/shell-shebang.ts",
|
|
28
28
|
"stack": "$npm_execpath src/cli.ts"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@kaynooo/cli-kit": "0.46.
|
|
31
|
+
"@kaynooo/cli-kit": "0.46.4"
|
|
32
32
|
}
|
|
33
33
|
}
|