@theholocron/cli 5.0.0-alpha.24 → 5.0.0-alpha.240

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.mjs CHANGED
@@ -1,14 +1,17 @@
1
- import { CARDINALITY, ProviderApiError, REQUIRED_CAPABILITIES, WebhookVerificationError, isMulti } from "./plugin/capabilities.mjs";
1
+ import { CARDINALITY, ElevationRequiredError, ProviderApiError, REQUIRED_CAPABILITIES, WebhookVerificationError, isMulti } from "./plugin/capabilities.mjs";
2
2
  import { AuthError, createResolveToken as createResolveToken$1, createRestClient } from "@theholocron/http-client";
3
- import { createEnvLookup } from "@theholocron/env-utils";
3
+ import { createEnvLookup, withEnvPrefix } from "@theholocron/env-utils";
4
4
  import { Entry, findCredentials } from "@napi-rs/keyring";
5
- import { readFile } from "node:fs/promises";
6
5
  import { basename, dirname, join } from "node:path";
7
- import { execFile } from "node:child_process";
8
- import { promisify } from "node:util";
6
+ import { readPackageJson } from "@theholocron/pkg-utils";
9
7
  import { loadTasksConfig } from "@theholocron/astromech/config";
10
8
  import { ConfigFileError, ConfigFileError as ConfigFileError$1, loadConfigFile } from "@theholocron/datapad";
11
- createEnvLookup();
9
+ import { readGitRemote } from "@theholocron/git-utils";
10
+ import { lookup } from "node:dns/promises";
11
+ //#region src/env/env.ts
12
+ /** Singleton env for simple global lookups throughout the CLI. */
13
+ const env = createEnvLookup();
14
+ withEnvPrefix(env, "HOLOCRON");
12
15
  /**
13
16
  * Create an injectable env for commands that accept a fake env in tests.
14
17
  * Pass `input.env` when available, falls back to `process.env`.
@@ -17,7 +20,7 @@ function makeEnv(source) {
17
20
  return createEnvLookup(source);
18
21
  }
19
22
  //#endregion
20
- //#region src/auth/keyring.ts
23
+ //#region src/auth/keyring/keyring.ts
21
24
  /**
22
25
  * Keyring-backed bootstrap credential store.
23
26
  *
@@ -87,7 +90,7 @@ function listStoredProviders() {
87
90
  }
88
91
  }
89
92
  //#endregion
90
- //#region src/auth/auth-resolver.ts
93
+ //#region src/auth/auth-resolver/auth-resolver.ts
91
94
  /** Wraps `createResolveToken` from `@theholocron/http` and injects the
92
95
  * system keyring so plugins stay at a one-liner call site. */
93
96
  function createResolveToken(config) {
@@ -108,12 +111,12 @@ function createFeatureResolver(config) {
108
111
  const env = makeEnv(input.env);
109
112
  const keyring = input.keyring ?? getToken;
110
113
  const token = input.cliToken || env.get(config.envName) || keyring(config.keyringKey);
111
- if (!token) throw new AuthError(`no GitHub token found for this operation. Pass --token <PAT>, set ${config.envName}, or run: holocron auth set ${config.keyringKey} <PAT>`);
114
+ if (!token) throw new AuthError(`no ${config.vendor ?? "GitHub"} token found for this operation. Pass --token <token>, set ${config.envName}, or run: holocron auth set ${config.keyringKey} <token>`);
112
115
  return token;
113
116
  };
114
117
  }
115
118
  //#endregion
116
- //#region src/commands/setup/derived-properties.ts
119
+ //#region src/commands/setup/derived-properties/derived-properties.ts
117
120
  /**
118
121
  * Repo archetype, in priority order:
119
122
  * 1. `*-template` repo name → `template` (independent of package shape).
@@ -184,7 +187,7 @@ function deriveCapabilities(providers) {
184
187
  return Object.keys(providers ?? {}).sort();
185
188
  }
186
189
  /**
187
- * `source` + `ci` are the minimal baseline every repo `holocron setup`
190
+ * `source` + `ci` are the minimal baseline every repo `holocron repo setup`
188
191
  * touches wires — every `holocron.config.ts` in this org already satisfies
189
192
  * this, so `non-compliant` here means drift (a provider manually removed
190
193
  * after setup), not an unmet aspirational policy. Deliberately not a richer
@@ -213,14 +216,14 @@ function missingCapabilities(capabilities) {
213
216
  */
214
217
  async function readWorkspacePackageJsons(repoRoot, workspacePackages) {
215
218
  const results = [];
216
- for (const { slug, dir = "packages" } of workspacePackages) try {
217
- const raw = await readFile(join(repoRoot, dir, slug, "package.json"), "utf8");
218
- results.push(JSON.parse(raw));
219
- } catch {}
219
+ for (const { slug, dir = "packages", path } of workspacePackages) {
220
+ const pkg = await readPackageJson(join(repoRoot, path ?? join(dir, slug)));
221
+ if (pkg) results.push(pkg);
222
+ }
220
223
  return results;
221
224
  }
222
225
  //#endregion
223
- //#region src/config/config.ts
226
+ //#region src/config/config/config.ts
224
227
  var ConfigError = class extends Error {
225
228
  name = "ConfigError";
226
229
  };
@@ -324,11 +327,12 @@ function resolveConfig(raw) {
324
327
  docs,
325
328
  env: raw.env,
326
329
  log: raw.log,
327
- telemetry: raw.telemetry
330
+ telemetry: raw.telemetry,
331
+ eslint: raw.eslint
328
332
  };
329
333
  }
330
334
  //#endregion
331
- //#region src/config/compose.ts
335
+ //#region src/config/compose/compose.ts
332
336
  function taskName(e) {
333
337
  return typeof e === "string" ? e : e.name;
334
338
  }
@@ -378,7 +382,7 @@ function compose(...args) {
378
382
  let repo = {};
379
383
  for (const cap of caps) {
380
384
  if (!cap.repo) continue;
381
- const { properties, topics, teams, ...scalars } = cap.repo;
385
+ const { properties, topics, teams, variables, ...scalars } = cap.repo;
382
386
  repo = {
383
387
  ...repo,
384
388
  ...scalars
@@ -387,6 +391,18 @@ function compose(...args) {
387
391
  ...repo.properties,
388
392
  ...properties
389
393
  };
394
+ if (variables) repo.variables = {
395
+ ...repo.variables,
396
+ ...variables,
397
+ ...repo.variables?.repo || variables.repo ? { repo: {
398
+ ...repo.variables?.repo,
399
+ ...variables.repo
400
+ } } : {},
401
+ ...repo.variables?.org || variables.org ? { org: {
402
+ ...repo.variables?.org,
403
+ ...variables.org
404
+ } } : {}
405
+ };
390
406
  if (topics) {
391
407
  const merged = /* @__PURE__ */ new Set([...repo.topics ?? [], ...topics]);
392
408
  repo.topics = [...merged];
@@ -416,12 +432,12 @@ function compose(...args) {
416
432
  };
417
433
  }
418
434
  //#endregion
419
- //#region src/config/define-config.ts
435
+ //#region src/config/define-config/define-config.ts
420
436
  function defineConfig(config) {
421
437
  return config;
422
438
  }
423
439
  //#endregion
424
- //#region src/config/load-config.ts
440
+ //#region src/config/load-config/load-config.ts
425
441
  /**
426
442
  * `holocron.config.{ts,js,mjs,cjs,json}` loader.
427
443
  *
@@ -433,34 +449,39 @@ function defineConfig(config) {
433
449
  * Probe order is TS-first (`.ts` → `.js` → `.mjs` → `.cjs` → `.json`) —
434
450
  * see ADR-0010 / issue #75 / #81.
435
451
  */
436
- const execFileAsync = promisify(execFile);
437
452
  /**
438
- * Read + parse + resolve `holocron.config.*` from the given directory.
453
+ * Read + parse + resolve `holocron.config.*` starting from the given
454
+ * directory, walking up through ancestors to the nearest `.git` boundary
455
+ * when `cwd` itself has no match — every command is usable from any
456
+ * subdirectory of a repo, not only its exact root (see
457
+ * `@theholocron/datapad`'s own doc comment for the stop conditions).
439
458
  * Throws {@link ConfigFileError} when nothing is found or a file cannot
440
- * be loaded, or `ConfigError` when the config is invalid.
459
+ * be loaded, or `ConfigError` when the config fails schema checks.
441
460
  */
442
461
  async function loadConfig(cwd) {
443
462
  const found = await loadConfigFile({
444
463
  cwd,
445
- name: "holocron"
464
+ name: "holocron",
465
+ walkUp: true
446
466
  });
447
467
  if (!found) throw new ConfigFileError$1(`no holocron.config.{ts,js,mjs,cjs,json} found in ${cwd}. Create one — see the README for the schema.`);
448
468
  return {
449
- resolved: resolveConfig(await deriveDefaults(dirname(found.filepath), found.config)),
469
+ resolved: resolveConfig(await deriveDefaults(found)),
450
470
  filepath: found.filepath
451
471
  };
452
472
  }
453
- async function deriveDefaults(configDir, raw) {
454
- const result = { ...raw };
473
+ async function deriveDefaults(found) {
474
+ const configDir = dirname(found.filepath);
475
+ const result = { ...found.config };
455
476
  if (!result.name) result.name = await readPackageJsonName(configDir) ?? basename(configDir);
456
477
  if (result.repo && !result.repo.name) {
457
- const repoName = await readGitRemote(configDir);
458
- if (repoName) result.repo = {
478
+ const remote = await readGitRemote(configDir);
479
+ if (remote?.host === "github.com") result.repo = {
459
480
  ...result.repo,
460
- name: repoName
481
+ name: remote.slug
461
482
  };
462
483
  }
463
- const manifest = await loadTasksConfig(configDir);
484
+ const manifest = await loadTasksConfig(configDir, { holocron: found });
464
485
  if (manifest.tasks) result.tasks = manifest.tasks;
465
486
  if (manifest.syncScripts !== void 0) result.syncScripts = manifest.syncScripts;
466
487
  if (manifest.holocronScript !== void 0) result.holocronScript = manifest.holocronScript;
@@ -469,31 +490,146 @@ async function deriveDefaults(configDir, raw) {
469
490
  return result;
470
491
  }
471
492
  async function readPackageJsonName(dir) {
472
- try {
473
- const content = await readFile(join(dir, "package.json"), "utf8");
474
- const pkg = JSON.parse(content);
475
- return typeof pkg.name === "string" ? pkg.name.replace(/^@[^/]+\//, "") : void 0;
476
- } catch {
477
- return;
493
+ const name = (await readPackageJson(dir))?.name;
494
+ return name === void 0 ? void 0 : name.replace(/^@[^/]+\//, "");
495
+ }
496
+ //#endregion
497
+ //#region src/plugin/connection-probe/connection-probe.ts
498
+ const TLS_CODES = /* @__PURE__ */ new Set([
499
+ "UNABLE_TO_VERIFY_LEAF_SIGNATURE",
500
+ "SELF_SIGNED_CERT_IN_CHAIN",
501
+ "DEPTH_ZERO_SELF_SIGNED_CERT",
502
+ "CERT_HAS_EXPIRED",
503
+ "UNABLE_TO_GET_ISSUER_CERT_LOCALLY",
504
+ "ERR_TLS_CERT_ALTNAME_INVALID"
505
+ ]);
506
+ /** The code Node attaches to a network failure, with that error's message: on the error itself, or on its `cause` (where `fetch` puts it). */
507
+ function codeOf(err) {
508
+ for (let e = err, depth = 0; e && depth < 4; depth++) {
509
+ if (typeof e.code === "string") return {
510
+ code: e.code,
511
+ message: String(e.message ?? "")
512
+ };
513
+ e = e.cause;
478
514
  }
479
515
  }
480
- async function readGitRemote(dir) {
481
- try {
482
- const { stdout } = await execFileAsync("git", [
483
- "remote",
484
- "get-url",
485
- "origin"
486
- ], { cwd: dir });
487
- return parseGitRemoteUrl(stdout.trim());
488
- } catch {
489
- return;
516
+ /** A network failure in plain words, with what usually causes it. Never includes anything from a request. */
517
+ function describeNetworkError(err, timeoutMs) {
518
+ const found = codeOf(err);
519
+ const code = found?.code;
520
+ const name = err?.name;
521
+ if (name === "TimeoutError" || name === "AbortError" || code === "UND_ERR_CONNECT_TIMEOUT" || code === "ETIMEDOUT") return `no answer within ${timeoutMs / 1e3}s (${code ?? name}): a firewall, VPN or proxy may be dropping it`;
522
+ switch (code) {
523
+ case "ENOTFOUND":
524
+ case "EAI_AGAIN": return `${code}: the name did not resolve; check your network and DNS`;
525
+ case "ECONNREFUSED": return "ECONNREFUSED: the host refused the connection";
526
+ case "ECONNRESET":
527
+ case "UND_ERR_SOCKET": return `${code}: the connection was cut off; a proxy or firewall may be interfering`;
528
+ case "ENETUNREACH":
529
+ case "EHOSTUNREACH": return `${code}: no route to the host; check that you are online`;
530
+ default:
531
+ if (code && TLS_CODES.has(code)) return `${code}: the TLS certificate was not trusted; a corporate proxy that re-signs traffic needs NODE_EXTRA_CA_CERTS`;
532
+ return found ? `${found.code}: ${found.message}` : String(err?.message ?? err);
490
533
  }
491
534
  }
492
- function parseGitRemoteUrl(url) {
493
- const httpsMatch = url.match(/github\.com\/([^/]+\/[^/]+?)(?:\.git)?$/);
494
- if (httpsMatch) return httpsMatch[1];
495
- const sshMatch = url.match(/github\.com:([^/]+\/[^/]+?)(?:\.git)?$/);
496
- if (sshMatch) return sshMatch[1];
535
+ /**
536
+ * The `connection` probe every provider offers, for telling a network problem from a credential problem: does the name
537
+ * resolve, does the host answer, is there a token, and does the provider accept it. A step that cannot work because an
538
+ * earlier one failed is reported as not tried rather than failing a second time for the same reason.
539
+ */
540
+ function connectionProbe(input) {
541
+ const timeoutMs = input.timeoutMs ?? 1e4;
542
+ return {
543
+ summary: `Check the network path to ${input.provider} and that it accepts your token.`,
544
+ writes: false,
545
+ async run(probe) {
546
+ let reachable = true;
547
+ if (input.url) {
548
+ const host = new URL(input.url).hostname;
549
+ const lookup$1 = input.lookup ?? (input.fetch ? void 0 : (h) => lookup(h));
550
+ if (lookup$1) try {
551
+ const { address } = await lookup$1(host);
552
+ probe.say({
553
+ status: "ok",
554
+ label: `DNS ${host}`,
555
+ detail: address
556
+ });
557
+ } catch (err) {
558
+ reachable = false;
559
+ probe.say({
560
+ status: "fail",
561
+ label: `DNS ${host}`,
562
+ detail: describeNetworkError(err, timeoutMs)
563
+ });
564
+ }
565
+ if (reachable) {
566
+ const started = Date.now();
567
+ try {
568
+ const res = await (input.fetch ?? fetch)(input.url, {
569
+ method: "GET",
570
+ signal: AbortSignal.timeout(timeoutMs)
571
+ });
572
+ probe.say({
573
+ status: "ok",
574
+ label: `reach ${host}`,
575
+ detail: `HTTP ${res.status} in ${Date.now() - started} ms`
576
+ });
577
+ } catch (err) {
578
+ reachable = false;
579
+ probe.say({
580
+ status: "fail",
581
+ label: `reach ${host}`,
582
+ detail: describeNetworkError(err, timeoutMs)
583
+ });
584
+ }
585
+ } else probe.say({
586
+ status: "info",
587
+ label: `reach ${host}`,
588
+ detail: "not tried: the name did not resolve"
589
+ });
590
+ }
591
+ let token;
592
+ try {
593
+ token = await input.token();
594
+ } catch (err) {
595
+ probe.say({
596
+ status: "fail",
597
+ label: "token",
598
+ detail: err.message
599
+ });
600
+ probe.say({
601
+ status: "info",
602
+ label: "accepted",
603
+ detail: "not tried: there is no token"
604
+ });
605
+ return;
606
+ }
607
+ probe.secret(token);
608
+ probe.say({
609
+ status: "ok",
610
+ label: "token",
611
+ detail: "found"
612
+ });
613
+ if (!reachable) {
614
+ probe.say({
615
+ status: "info",
616
+ label: "accepted",
617
+ detail: "not tried: the host could not be reached"
618
+ });
619
+ return;
620
+ }
621
+ const result = await input.verify(token);
622
+ probe.say(result.ok ? {
623
+ status: "ok",
624
+ label: "accepted",
625
+ detail: result.subject
626
+ } : {
627
+ status: "fail",
628
+ label: "accepted",
629
+ detail: result.message
630
+ });
631
+ }
632
+ };
497
633
  }
498
634
  //#endregion
499
- export { AuthError, CARDINALITY, ConfigError, ConfigFileError, ProviderApiError, REQUIRED_CAPABILITIES, WebhookVerificationError, compose, createFeatureResolver, createResolveToken, createRestClient, defineConfig, deleteToken, deriveCapabilities, deriveCompliance, deriveProfile, deriveStack, getToken, isMulti, listStoredProviders, loadConfig, missingCapabilities, readWorkspacePackageJsons, resolveConfig, resolveEntry, resolvePluginPackage, setToken };
635
+ export { AuthError, CARDINALITY, ConfigError, ConfigFileError, ElevationRequiredError, ProviderApiError, REQUIRED_CAPABILITIES, WebhookVerificationError, compose, connectionProbe, createFeatureResolver, createResolveToken, createRestClient, defineConfig, deleteToken, deriveCapabilities, deriveCompliance, deriveProfile, deriveStack, describeNetworkError, getToken, isMulti, listStoredProviders, loadConfig, missingCapabilities, readWorkspacePackageJsons, resolveConfig, resolveEntry, resolvePluginPackage, setToken };