@nmakarov/cli-toolkit 0.23.0 → 0.25.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.
@@ -2065,6 +2065,18 @@ var Params = class _Params {
2065
2065
  this._currentModule = prev;
2066
2066
  }
2067
2067
  }
2068
+ /**
2069
+ * Async variant of {@link runWithModule} for modules that await params.get().
2070
+ */
2071
+ async runWithModuleAsync(moduleName, fn) {
2072
+ const prev = this._currentModule;
2073
+ this._currentModule = moduleName;
2074
+ try {
2075
+ return await fn();
2076
+ } finally {
2077
+ this._currentModule = prev;
2078
+ }
2079
+ }
2068
2080
  /**
2069
2081
  * Infer module name from call stack: first caller outside params/index gives path like .../src/<moduleName>/...
2070
2082
  */
@@ -2593,38 +2605,71 @@ var KNEX_DEFAULTS = {
2593
2605
  };
2594
2606
  var Db = class {
2595
2607
  static async init(context, options = {}) {
2596
- const defs2 = {
2597
- dbName: "string",
2598
- dbConnectionString: "string",
2599
- dbProfile: "boolean default false"
2600
- };
2601
- const discovered = context?.params?.getAllForModule?.("db", defs2) ?? {};
2602
- const merged = { ...discovered, ...options };
2603
- let { dbName, dbConnectionString } = merged;
2604
- const { dbProfile } = merged;
2605
- if (!dbName && !dbConnectionString) {
2606
- dbName = "local";
2607
- }
2608
- if (dbName && /^(postgresql|mysql):\/\//.test(dbName)) {
2609
- dbConnectionString = dbName;
2610
- dbName = void 0;
2611
- }
2612
- if (dbName && !dbConnectionString) {
2613
- const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
2614
- dbConnectionString = await context.params.get(paramName, "string");
2608
+ const buildConfig = async () => {
2609
+ const defs2 = {
2610
+ dbName: "string",
2611
+ dbProfile: "boolean default false"
2612
+ };
2613
+ const discovered = context?.params?.getAllForModule?.("db", defs2) ?? {};
2614
+ const merged = { ...discovered, ...options };
2615
+ let { dbName, dbProfile } = merged;
2616
+ let dbConnectionString = options.dbConnectionString ?? options.connectionString;
2617
+ let connectionParam = dbConnectionString ? "options" : null;
2615
2618
  if (!dbConnectionString) {
2616
- throw new ParamError(
2617
- `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
2618
- );
2619
+ const src = context?.args?.getSource?.("dbConnectionString");
2620
+ if (src === "cli" || src === "overrides" || src === "config") {
2621
+ dbConnectionString = await context.params.get("dbConnectionString", "string");
2622
+ connectionParam = "dbConnectionString";
2623
+ }
2619
2624
  }
2620
- }
2621
- const config2 = {
2622
- ...KNEX_DEFAULTS,
2623
- connectionString: dbConnectionString,
2624
- name: dbName || merged.name || "default",
2625
- profile: !!dbProfile,
2626
- logger: context.logger
2625
+ if (!dbConnectionString && dbName && /^(postgresql|mysql):\/\//.test(dbName)) {
2626
+ dbConnectionString = dbName;
2627
+ dbName = void 0;
2628
+ }
2629
+ if (!dbConnectionString && dbName) {
2630
+ const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
2631
+ dbConnectionString = await context.params.get(paramName, "string");
2632
+ connectionParam = paramName;
2633
+ if (!dbConnectionString) {
2634
+ throw new ParamError(
2635
+ `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
2636
+ );
2637
+ }
2638
+ }
2639
+ if (!dbConnectionString) {
2640
+ dbConnectionString = await context.params.get("dbConnectionString", "string");
2641
+ if (dbConnectionString) {
2642
+ connectionParam = "dbConnectionString";
2643
+ }
2644
+ }
2645
+ if (!dbConnectionString) {
2646
+ if (!dbName) {
2647
+ dbName = "local";
2648
+ }
2649
+ const paramName = `dbConnectionString${capitalizeFirstLetter(dbName)}`;
2650
+ dbConnectionString = await context.params.get(paramName, "string");
2651
+ connectionParam = paramName;
2652
+ if (!dbConnectionString) {
2653
+ throw new ParamError(
2654
+ `Db: cannot find dbConnectionString for dbName="${dbName}" (looked for param "${paramName}")`
2655
+ );
2656
+ }
2657
+ }
2658
+ const displayName = resolveDbDisplayName(
2659
+ dbName,
2660
+ connectionParam,
2661
+ context?.args?.env,
2662
+ merged.name
2663
+ );
2664
+ return {
2665
+ ...KNEX_DEFAULTS,
2666
+ connectionString: dbConnectionString,
2667
+ name: displayName,
2668
+ profile: !!dbProfile,
2669
+ logger: context.logger
2670
+ };
2627
2671
  };
2672
+ const config2 = context?.params?.runWithModuleAsync ? await context.params.runWithModuleAsync("db", buildConfig) : await buildConfig();
2628
2673
  return dbConnect(context, config2);
2629
2674
  }
2630
2675
  constructor(config2) {
@@ -2641,7 +2686,6 @@ var Db = class {
2641
2686
  acquireConnectionTimeout: 1e4,
2642
2687
  ssl: { rejectUnauthorized: false },
2643
2688
  logger: console,
2644
- name: "default",
2645
2689
  ...config2
2646
2690
  };
2647
2691
  this.logger = this.config.logger;
@@ -2741,9 +2785,7 @@ var Db = class {
2741
2785
  await this.testConnection();
2742
2786
  }
2743
2787
  this.isConnected = true;
2744
- this.logger.debug?.(
2745
- `[Db] Connected to database "${this.config.name || this.config.connectionString}"`
2746
- );
2788
+ this.logger.debug?.(formatDbConnectMessage(this.config.name, this.config.connectionString));
2747
2789
  } catch (error) {
2748
2790
  if (error instanceof ParamError) {
2749
2791
  throw error;
@@ -2761,9 +2803,7 @@ var Db = class {
2761
2803
  this.knexInstance = null;
2762
2804
  this.isConnected = false;
2763
2805
  this.queriesLog = [];
2764
- this.logger.debug?.(
2765
- `[Db] Disconnected from database "${this.config.name || this.config.connectionString}"`
2766
- );
2806
+ this.logger.debug?.(formatDbDisconnectMessage(this.config.name, this.config.connectionString));
2767
2807
  } catch (error) {
2768
2808
  const errorMsg = this.getErrorMessage(error);
2769
2809
  this.logger.error?.(`[Db] Error disconnecting: ${errorMsg}`);
@@ -2892,15 +2932,74 @@ var Db = class {
2892
2932
  function capitalizeFirstLetter(str) {
2893
2933
  return str.charAt(0).toUpperCase() + str.slice(1);
2894
2934
  }
2935
+ function resolveDbDisplayName(dbName, connectionParam, argsEnv, mergedName) {
2936
+ if (dbName) {
2937
+ return dbName;
2938
+ }
2939
+ if (mergedName) {
2940
+ return mergedName;
2941
+ }
2942
+ if (connectionParam?.startsWith("dbConnectionString") && connectionParam.length > "dbConnectionString".length) {
2943
+ return connectionParam.slice("dbConnectionString".length).toLowerCase();
2944
+ }
2945
+ if (connectionParam === "dbConnectionString" && argsEnv) {
2946
+ return argsEnv;
2947
+ }
2948
+ return void 0;
2949
+ }
2950
+ function formatDbConnectMessage(name, connectionString) {
2951
+ const endpointSuffix = formatConnectionEndpointSuffix(connectionString);
2952
+ if (name) {
2953
+ return `[Db] Connected to database "${name}"${endpointSuffix}`;
2954
+ }
2955
+ return `[Db] Connected${endpointSuffix}`;
2956
+ }
2957
+ function formatDbDisconnectMessage(name, connectionString) {
2958
+ const endpointSuffix = formatConnectionEndpointSuffix(connectionString);
2959
+ if (name) {
2960
+ return `[Db] Disconnected from database "${name}"${endpointSuffix}`;
2961
+ }
2962
+ return `[Db] Disconnected${endpointSuffix}`;
2963
+ }
2964
+ function formatDbInstanceMessage(action, name) {
2965
+ if (name) {
2966
+ return `[Db] instance "${name}" ${action}`;
2967
+ }
2968
+ return `[Db] instance ${action}`;
2969
+ }
2970
+ function formatConnectionEndpointSuffix(connectionString) {
2971
+ const endpoint = formatConnectionEndpoint(connectionString);
2972
+ return endpoint ? ` (${endpoint})` : "";
2973
+ }
2974
+ function formatConnectionEndpoint(connectionString) {
2975
+ try {
2976
+ const url = new URL(connectionString);
2977
+ const host = url.hostname;
2978
+ if (!host) {
2979
+ return null;
2980
+ }
2981
+ let port = url.port;
2982
+ if (!port) {
2983
+ if (url.protocol === "postgresql:") {
2984
+ port = "5432";
2985
+ } else if (url.protocol === "mysql:") {
2986
+ port = "3306";
2987
+ }
2988
+ }
2989
+ return port ? `${host}:${port}` : host;
2990
+ } catch {
2991
+ return null;
2992
+ }
2993
+ }
2895
2994
  async function dbConnect(context, config2) {
2896
2995
  try {
2897
2996
  const db = new Db(config2);
2898
2997
  context.registerCleanup(async () => {
2899
2998
  await db.disconnect();
2900
- context.logger.debug?.(`[Db] instance "${config2.name}" disconnected`);
2999
+ context.logger.debug?.(formatDbInstanceMessage("disconnected", config2.name));
2901
3000
  });
2902
3001
  await db.connect();
2903
- context.logger.debug?.(`[Db] instance "${config2.name}" initialized`);
3002
+ context.logger.debug?.(formatDbInstanceMessage("initialized", config2.name));
2904
3003
  return db;
2905
3004
  } catch (error) {
2906
3005
  if (error instanceof ParamError) {